{"id":2692,"date":"2018-07-08T18:17:30","date_gmt":"2018-07-08T23:17:30","guid":{"rendered":"http:\/\/osric.com\/chris\/accidental-developer\/?p=2692"},"modified":"2018-07-09T09:40:47","modified_gmt":"2018-07-09T14:40:47","slug":"postfix-altering-the-subject-line-of-outgoing-messages","status":"publish","type":"post","link":"https:\/\/osric.com\/chris\/accidental-developer\/2018\/07\/postfix-altering-the-subject-line-of-outgoing-messages\/","title":{"rendered":"Postfix: altering the subject line of outgoing messages"},"content":{"rendered":"<p>Motivation: I&#8217;m changing my ticketing system so that messages with a friendlier subject line. Instead of &#8216;Subject: [SNAFU #1] summary&#8217;, I&#8217;ll change it to use &#8216;Subject: [HELPDESK #1] summary&#8217;.<\/p>\n<p>Another colleague suggested we use <code>procmail<\/code>. As unixgeeks.org says:<\/p>\n<blockquote><p>Is procmail right for me?<br \/>\nProcmail is a serious unix hack. On the other hand, it does a pretty good job.<\/p><\/blockquote>\n<p>(<a href=\"http:\/\/unixgeeks.org\/security\/newbie\/unix\/procmail\/procmail.html\">http:\/\/unixgeeks.org\/security\/newbie\/unix\/procmail\/procmail.html<\/a>)<\/p>\n<p>I think we can do better than a serious Unix hack. I think Postfix can handle this.<\/p>\n<p>How to do it? <!--more--><\/p>\n<p>First, I created a test environment: two Debian 9 virtual machines:<\/p>\n<ul>\n<li>foo.osric.net<\/li>\n<li>bar.osric.net<\/li>\n<\/ul>\n<p>I&#8217;m not going to add DNS entries, though, so I&#8217;ll be relying on their IP addresses on my internal network:<\/p>\n<ul>\n<li>192.168.0.18<\/li>\n<li>192.168.0.19<\/li>\n<\/ul>\n<p>I installed <code>postfix<\/code> and <code>mutt<\/code> on each:<\/p>\n<pre><code># apt-get install postfix mutt<\/code><\/pre>\n<p>As an initial test, I tried to send a message from one machine to the other:<\/p>\n<pre><code># sendmail root@192.168.0.18 &lt; test.eml<\/code><\/pre>\n<p>But this resulted in an error from the mailer-daemon:<\/p>\n<pre><code>&lt;root@192.168.0.18&gt;: bad address syntax<\/code><\/pre>\n<p>The solution? Enclose the IP address in square brackets:<\/p>\n<pre><code># sendmail root@[192.168.0.18] &lt; test.eml<\/code><\/pre>\n<p>(Thanks to <a href=\"https:\/\/www.quora.com\/Is-there-a-way-to-send-an-email-to-an-IP-address-directly-If-not-what-else-would-one-need-to-know-besides-the-IP-address\">Is there a way to send an email to an IP address directly?<\/a>, which I believe is the first time I have ever seen anything useful on the Quora web site.)<\/p>\n<p>The two test systems can send messages to each other. Great!<\/p>\n<p>Now, to change the subject line. A ServerFault post, <a href=\"https:\/\/serverfault.com\/questions\/156906\/postfix-header-checks-to-rename-email-subject\">postfix header_checks to rename email subject<\/a>, looked like exactly what I was looking for (and a similar scenario motivated that question).<\/p>\n<p>Following the answer there, I added the following to <code>\/etc\/postfix\/main.cf<\/code>:<\/p>\n<pre><code>header_checks = pcre:\/etc\/postfix\/header_checks.pcre<\/code><\/pre>\n<p>And I created <code>\/etc\/postfix\/header_checks.pcre<\/code>, containing the following:<\/p>\n<pre><code>\/^Subject: \\[SNAFU #(\\d+)\\](.*)$\/    REPLACE Subject: [HELPDESK #$1]$2<\/code><\/pre>\n<p>Don&#8217;t forget to reload Postfix, required by just about any Postfix change:<\/p>\n<pre><code># postfix reload<\/code><\/pre>\n<p>I sent a test message from <code>bar<\/code> to <code>foo<\/code>, and an error message arrived in root&#8217;s mailbox on <code>foo<\/code>:<\/p>\n<pre><code>Subject: Postfix SMTP server: errors from bar.osric.net[192.168.0.19]<\/code><\/pre>\n<p>I checked the logs at <code>\/var\/log\/mail.log<\/code> and found the following:<\/p>\n<pre><code>Jul  8 16:47:29 foo postfix\/cleanup[10927]: error: unsupported dictionary type: pcre<\/code><\/pre>\n<p>I reproduced this problem using <code>postmap<\/code>:<\/p>\n<pre><code># postmap -q 'Subject: [SNAFU #1]' pcre:\/etc\/postfix\/header_checks.pcre\r\npostmap: fatal: unsupported dictionary type: pcre<\/code><\/pre>\n<p>I changed the filename (not strictly necessary, but good for my sanity) and tried <code>regexp<\/code> instead:<\/p>\n<pre><code># postmap -q 'Subject: [SNAFU #1]' regexp:\/etc\/postfix\/header_checks.regexp<\/code><\/pre>\n<p>This produced no output, no error message, and a return code of 1. Why didn&#8217;t it match?<\/p>\n<p>It turned out I needed to modify the regular expression, since <code>\\d<\/code> is a Perl-ism:<\/p>\n<pre><code>\/^Subject: \\[SNAFU #([0-9]+)\\](.*)$\/    REPLACE Subject: [HELPDESK #$1]$2<\/code><\/pre>\n<p>(Using <code>[[:digit:]]<\/code> instead of <code>[0-9]<\/code> would also work.)<\/p>\n<p>I ran a couple successful tests using <code>postmap<\/code>:<\/p>\n<pre><code># postmap -q 'Subject: [SNAFU #1]' regexp:\/etc\/postfix\/header_checks.regexp\r\nREPLACE Subject: [HELPDESK #1]\r\n\r\n~# postmap -q 'Subject: [SNAFU #1] more text' regexp:\/etc\/postfix\/header_checks.regexp\r\nREPLACE Subject: [HELPDESK #1] more text<\/code><\/pre>\n<p>Now to test an actual e-mail message.<\/p>\n<p>First, I updated <code>\/etc\/postfix\/main.cf<\/code> to point to the updated filename with the corresponding dictionary type:<\/p>\n<pre><code>header_checks = regexp:\/etc\/postfix\/header_checks.regexp<\/code><\/pre>\n<p>Don&#8217;t forget:<\/p>\n<pre><code># postfix reload<\/code><\/pre>\n<p>I tested it from <code>bar.osric.net<\/code> (192.168.0.19), using the following file, <code>test.eml<\/code>:<\/p>\n<pre><code>Subject: [SNAFU #1] test message\r\n\r\nThis is a test.<\/code><\/pre>\n<pre><code># sendmail root@[192.168.0.18] &lt; test.eml<\/code><\/pre>\n<p>It appeared in root&#8217;s inbox on <code>foo.osric.net<\/code> (192.168.0.18) with the updated subject as expected:<\/p>\n<pre><code>Date: Sun,  8 Jul 2018 17:03:56 -0500 (CDT)\r\nFrom: root &lt;root@bar.osric.net&gt;\r\nSubject: [HELPDESK #1] test message\r\n\r\nThis is a test.<\/code><\/pre>\n<p>I sent another message with a different subject line just to confirm it was unchanged by the <code>header_check<\/code>.<\/p>\n<p>Other useful resources:<\/p>\n<ul>\n<li>Postfix documentation: <a href=\"http:\/\/www.postfix.org\/header_checks.5.html\">header_checks<\/a><\/li>\n<li>Postfix documentation: <a href=\"http:\/\/www.postfix.org\/postmap.1.html\">postmap<\/a><\/li>\n<li>Postfix documenation: <a href=\"http:\/\/www.postfix.org\/regexp_table.5.html\">regexp table<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>I wanted to change the subject line of messages sent from my ticketing (queueing) system from [SNAFU #1] to the friendlier [HELPDESK #1]. Using Postfix header_checks I was able to accomplish this.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[362],"class_list":["post-2692","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-postfix"],"_links":{"self":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/2692","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/comments?post=2692"}],"version-history":[{"count":9,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/2692\/revisions"}],"predecessor-version":[{"id":2704,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/2692\/revisions\/2704"}],"wp:attachment":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/media?parent=2692"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/categories?post=2692"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/tags?post=2692"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}