<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>postfix &amp;mdash; Anthony Takes Note</title>
    <link>https://aclarka2.writeas.com/tag:postfix</link>
    <description>I write stuff in here. I endeavour to be accurate and objectively truthful.</description>
    <pubDate>Mon, 22 Jun 2026 05:50:58 +0000</pubDate>
    <item>
      <title>Grabbing Email Addresses From Postfix Logs</title>
      <link>https://aclarka2.writeas.com/grabbing-email-addresses-from-postfix-logs?pk_campaign=rss-feed</link>
      <description>&lt;![CDATA[In a recent incident, I was asked to provide a list of destination addresses being delivered to a particular mail server. Here&#39;s an example line from a #Postfix log:&#xA;&#xA;Dec 29 05:00:51 mail01.test.example.com postfix/smtp[28704]: AF02145249: to=emailtest.address@gmail.com, relay=gmail-smtp-in.l.google.com[209.85.232.27]:25, delay=0.69, delays=0.01/0/0.16/0.52, dsn=2.0.0, status=sent (250 2.0.0 OK 1544545651 n189bb201234abc.123 - gsmtp)&#xA;&#xA;From that log line, you can see that the #email address is surrounded by the angled brackets, &#34;&#34; and &#34;&#34;. Those brackets are preceded by the text &#34; to=&#34;.&#xA;&#xA;From this fantastic stackoverflow page I found some very useful grep commands for use in compiling part of this answer.&#xA;&#xA;I decided to go with the fastest #grep answer, since I was dealing with multiple gigabytes of mail logs from the Postfix Mail Transfer Agent (MTA):&#xA;&#xA;grep -Po &#39; to=\K[^]&#39;&#xA;Now I need to explain what each bit of that means. (Using the GNU Grep 3.3 manual)&#xA;&#xA;      &#xA;When ran against the log line shown at the beginning, you get the following output:&#xA;$ cat tmp2.txt | grep -Po &#39; to=\K[^]&#39;&#xA;emailtest.address@gmail.com&#xA;&#xA;If you are interested in using regexes more, the RegExr site is a visual regex learning tool. You can use it to build up regexes slowly while understanding exactly what they do. You can also paste in a pre-existing regex to see if the site can describe what it does for you.]]&gt;</description>
      <content:encoded><![CDATA[<p>In a recent incident, I was asked to provide a list of destination addresses being delivered to a particular mail server. Here&#39;s an example line from a <a href="https://aclarka2.writeas.com/tag:Postfix" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">Postfix</span></a> log:</p>

<pre><code>Dec 29 05:00:51 mail01.test.example.com postfix/smtp[28704]: AF02145249: to=&lt;emailtest.address@gmail.com&gt;, relay=gmail-smtp-in.l.google.com[209.85.232.27]:25, delay=0.69, delays=0.01/0/0.16/0.52, dsn=2.0.0, status=sent (250 2.0.0 OK 1544545651 n189bb201234abc.123 - gsmtp)
</code></pre>

<p>From that log line, you can see that the <a href="https://aclarka2.writeas.com/tag:email" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">email</span></a> address is surrounded by the angled brackets, “&lt;” and “&gt;”. Those brackets are preceded by the text “ to=”.</p>

<p>From <a href="https://stackoverflow.com/questions/41872634/extract-email-addresses-from-log-with-grep-or-sed" rel="nofollow">this fantastic stackoverflow page</a> I found some very useful grep commands for use in compiling part of this answer.</p>

<p>I decided to go with the fastest <a href="https://aclarka2.writeas.com/tag:grep" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">grep</span></a> answer, since I was dealing with multiple gigabytes of mail logs from the Postfix Mail Transfer Agent (MTA):</p>

<pre><code>grep -Po &#39; to=&lt;\K[^&gt;]*&#39;
</code></pre>

<p>Now I need to explain what each bit of that means. (Using the <a href="https://www.gnu.org/software/grep/manual/grep.html" rel="nofollow">GNU Grep 3.3 manual</a>)</p>
<ul><li><code>-P</code> Interpret the <a href="https://aclarka2.writeas.com/tag:regex" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">regex</span></a> as a Perl-compatible regular expression (a “<a href="https://www.pcre.org/" rel="nofollow">PCRE</a>”). This can also be written as <code>--perl-regexp</code>. PCREs are very powerful regexes that are used in a very wide variety of open source applications. <a href="https://www.pcre.org/current/doc/html/pcre2syntax.html" rel="nofollow">Their syntax is covered in their documentation</a>.</li>
<li><code>-o</code> aka <code>--only-matching</code>. Only print the matching part of the content when you find a match, as opposed to the default behaviour of printing the entire line. This is useful because it means you don&#39;t have to then pipe your output to “cut”, “awk” or other tools to get what you want, but it does mean you need to be more precise with your matches.</li>
<li><code>&#39; to=&lt;\K[^&gt;]*&#39;</code>
<ul><li><code>to=&lt;</code> Matches a space, followed by the characters “t”, “o”, “=”, “&lt;“.</li>
<li><code>\K</code> Report that the match starts here. In other words, you match with the characters “ to=&lt;” but then discard those characters when printing the match. Again, a neat way to only show what you want, rather than having to pipe your output to another application to chop up the output.</li>
<li><code>[^&gt;]*</code> This matches anything that isn&#39;t a “&gt;” symbol, or in other words the match is ended when a “&gt;” symbol is encountered. The square brackets are a <em>character class</em> and the caret (“^”) symbol negates that character class. The asterisk at the end means “match zero or more of the preceding thing”. <em>(It&#39;s OK to end the match before the “&gt;” symbol because in an email address, the domain name portion (that comes after the “@” symbol) may not contain that character.)</em></li></ul></li></ul>

<p>When ran against the log line shown at the beginning, you get the following output:</p>

<pre><code>$ cat tmp2.txt | grep -Po &#39; to=&lt;\K[^&gt;]*&#39;
emailtest.address@gmail.com
</code></pre>

<p>If you are interested in using regexes more, the <a href="https://regexr.com/" rel="nofollow">RegExr site</a> is a visual regex learning tool. You can use it to build up regexes slowly while understanding exactly what they do. You can also paste in a pre-existing regex to see if the site can describe what it does for you.</p>
]]></content:encoded>
      <guid>https://aclarka2.writeas.com/grabbing-email-addresses-from-postfix-logs</guid>
      <pubDate>Sun, 27 Jan 2019 22:27:04 +0000</pubDate>
    </item>
  </channel>
</rss>