0

I would like to run a script over email received by a virtual mailbox. I was hoping to do this with .forward in the virtual mailbox directory but it doesn't seem to be executing the script.

The idea is to extract attachments via munpack into another directory, but only when sent to that mailbox.

Mail Server is Postfix.

Ablue
  • 1,140
  • 1
  • 12
  • 32
  • You need to specify a lot more: what mail server, how it is setup, etc.. – totaam Jan 04 '12 at 09:03
  • sorry, I thought the tags would indicate that, `Postfix`. There is a lot of config, where should I start? – Ablue Jan 04 '12 at 11:49
  • since your question is about virtual mailbox, pretty much anything related to that – totaam Jan 04 '12 at 12:10
  • 1
    possible duplicate of [Run script on incoming email message to postfix](http://serverfault.com/questions/132576/run-script-on-incoming-email-message-to-postfix) – mailq Jan 04 '12 at 21:55
  • @mailq I think you are right. I missed that one. – Ablue Jan 04 '12 at 22:25
  • @mailq the link you mention allows to trigger custom script after an email is received by the MTA, not neccesarily after the email is delivered to the mailbox, e.g., when the user doesn't exist in the local system, the content filter would still be executed, although no email would be delivered. – Jaime Hablutzel Jan 07 '18 at 22:18

2 Answers2

1

One option I have used before is to define your own delivery wrapper (I use maildrop for actual delivery - but this should work with any) then you can just do your stuff there. Something like this in master.cf:

maildrop unix -     n   n   -   -   pipe
  flags=Rhu user=vmail:vmail argv=/usr/bin/maildrop-wrapper 
  -d ${user}@${nexthop} -f ${sender} -A X-Delivery:maildrop

In maildrop-wrapper, do your stuff then just call the real maildrop:

#!/bin/bash
#do your stuff
#and/or:
exec /usr/bin/maildrop $@

You can run it before or after calling maildrop, important note: the script will receive the email via stdin, so you will have to buffer it if you intend to pass it to maildrop later (which the example above does not do). I would probably recommend using perl for this as there are more mail handling libraries there than in bare shell.

Edit: If you only want to do this for a single user then this is overkill, have a look at maildrop - in particular the section about "external commands" via backticks. Define a maildroprc for this user with the rules required.

totaam
  • 191
  • 4
  • 15
  • In order to use this approach and apply the filtering to only a specific mailbox I would need to write in a condition for the recipient... It seems messy for what I want to achieve. – Ablue Jan 04 '12 at 23:46
0

I have something like that running for many years. I added a redirection in the /etc/aliases file.

The mails that need processing by munpack are sent to robot@example.com.

So I wrote in the aliases file this line robot : |"/home/robot/munpack-private.sh" user

so the mail gets processed by the munpack script and sent to the user mailbox

  • Interesting thread more detail: https://serverfault.com/questions/258469/how-to-configure-postfix-to-pipe-all-incoming-email-to-a-script – minish Feb 13 '20 at 02:00