Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Postfix: Piping an email into a PERL script

by Zzenmonk (Sexton)
on Apr 26, 2013 at 13:45 UTC ( #1030830=CUFP: print w/ replies, xml ) Need Help??

I spend today reading about how to pipe a mail received by postfix into a perl script. I read a lot but could hardly find a solution for my setup. So I give one here.

The case

An application must be able to process email coming in from the net and it must be also able to respond to these emails.

The setup:

  • The machine is configured to send all emails over an MTA somewhere in the network
  • The MTA hosts a virtual mail domain for which it forwards all its email to the machine
  • The machine holds an application written in PERL
  • A PERL script īs used as delivery agent to process some mails
  • The tested platform: Linux Ubuntu 12.04 LTS, postfix 2.9.6

The postfix main.cf configuration created by dpkg-reconfigure postfix

myhostname = [host name] alias_maps = hash:/etc/aliases alias_database = hash:/etc/aliases myorigin = /etc/mailname mydestination = [list for local delivery] relayhost = [the MTA name for sending emails] mynetworks = [see posfix documentation] mailbox_size_limit = [you would rather set it!] recipient_delimiter = + inet_interfaces = all inet_protocols = ipv4 mail_spool_directory = /var/mail

My solution:

As usual TIMTOWTDI but a simple alias will NOT work! My solution works without:

  • A cryptic central definition of a local delivery agent in master.cf
  • Having pcre compiled in postfix. I have no use for this.
  • Creating OS users or virtual users on the system.
  • No other ugly things I read.

Now you have to:

  • Modify your main.cf to redirect the mail.
  • Create a redirect map for postfix
  • Create an alias which will fool postfix and deliver the mail to the script

Modifications to the main.cf configuration

To avoid creating whatsoever users, we will use a kind of redirect. So we add a line like the following to the main.cf file:

virtual_alias_maps = hash:/etc/postfix/redirect

Notice the hash: prefix to the file. This works well and avoid to re-compile postfix with pcre, in case it is not included.

Creation of the redirect map

Create the file defined above and enter a line with a regexp and a name in it:

/[name]@[machine domain]/ [alias name]

Notice you have to use a regexp as first argument on this line! The alias name does not matter. Only secure name@domain is the correct email address.

Creation of the alias

Now we create the alias which will pipe the email into our script. Add a line like the following to the /etc/aliases file:

[alias name]: "|/usr/bin/perl /[path to]/thescript.pl"

Notice you have to secure both alias name match!

Activate the whole thing

To activate all this you have to

  • Create your redirect map with postmap [path to ]/redirect
  • Activate your new aliases with newaliases
  • Load the new posfix configuration with posfix reload

The result

This is what you will get in the STDIN of your script

From [who ever]@[whatever].com Mon Apr 22 17:48:08 2013 Return-Path: <[who ever]@[whatever].com > X-Original-To: [name]@[machine domain] Delivered-To: [name]@[machine domain] Received: by [No matter to you] To: <[name]@[machine domain]> Subject: [What so ever] X-Mailer: mail (GNU Mailutils 2.2) Message-Id: <20130422154808.4100143A9@lap01> Date: Mon, 22 Apr 2013 17:48:08 +0200 (CEST) From: [who ever]@[whatever].com lkjahsdlfkjhlahsdf lkjahdsflkjhasdlfh lkjahsdflkjhsaldfhk

Notice: If you intent to open(OUT ">", $filename); in your PERL script it will fail with a missing privilege error. To avoid this you have to set default_privs = to an other user than nobody in the postfix main.cf. This has impact on the whole postfix setting. I did not analyse it until now. So if you do this, you do it at your own risk.

Enjoy!

K

The best medicine against depression is a cold beer!

Comment on Postfix: Piping an email into a PERL script
Select or Download Code
Re: Postfix: Piping an email into a PERL script
by MidLifeXis (Prior) on Apr 26, 2013 at 15:12 UTC

    May I suggest some sort of cryptographic signing of the request? Headers are very easy to spoof.

    --MidLifeXis

      This is definitely something one should think of. My solution is implemented behind the MTA of the network. The MTA takes care of security in my case. I mean as good as it can (DNSBL, spam filter and so on).

      The best medicine against depression is a cold beer!

        Is there a need to protect against internal users spoofing this? It is quite easy to speak SMTP.

        --MidLifeXis

Re: Postfix: Piping an email into a PERL script
by i_m_mahmoud (Initiate) on May 06, 2013 at 13:38 UTC
    In this example you will see how to send your emails to PERL script. http://e2mm10.blogspot.com/2013/05/postfix-change-time.html
Re: Postfix: Piping an email into a PERL script
by Anonymous Monk on May 28, 2013 at 19:29 UTC
    I tested that successfuly, but that line seem to be wrong :
    /[name]@[machine domain]/ [alias name]
    It is not a regexp, it is an email address. See http://www.postfix.org/VIRTUAL_README.html
      Indeed, this is true. Removing the slashes makes it work.
Re: Postfix: Piping an email into a PERL script
by jesuashok (Curate) on Nov 18, 2013 at 07:24 UTC
    Will I be able to use the same strategy to process emails which are coming to our shared email box and publish them to a webpage? Can I use this as base or do I have to take different approach?

    i m possible

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://1030830]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (10)
As of 2014-12-19 15:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (85 votes), past polls