Delay the mails with Postfix

Some days ago I commented in Catalan that I wanted to delay the mails using Postfix. Why? To have a period of time when I could cancel (or “unsend”) some mails. Google has done it in GMail (see Labs option). It can be useful for these moments like “Ops! I sent the mail to the mailing list instead of sending it to a person” or “ops, I sent to A instead of B”. It doesn’t happen often, but can happen.

My setup: mutt is sending the mails to a local Postfix using the sendmail program, Then Postfix sends the all mails (relay) to another SMTP server. Technically I could change both SMTP, but I only wanted to change the local one since it’s “my one” and closer to the MUA.

Of course, searching in Google for “delay mails with Postfix” gives answers to the question “Why my mails are delayed” and not “I want to delay the mails”. I used what in Postfix is called Advanced content filter example.

You can follow the instructions of the Postfix webpage with a new script that I’ll show you later.

In /etc/postfix/master.cf you will need to change the pickup configuration to:
pickup fifo n - - 60 1 pickup
-o content_filter=scan:localhost:10025

So, it adds a content_filter “scan” (does mainly nothing) and sends the mail to the port 10025.
At the end of the /etc/postfix/master.cf you should add something like:
scan unix - - n - 10 smtp
-o smtp_send_xforward_command=yes
-o disable_mime_output_conversion=yes
-o smtp_generic_maps=
localhost:10025 inet n n n - 1 spawn
user=filter argv=/usr/local/bin/mail_delay.sh localhost 10026
localhost:10026 inet n - n - 1 smtpd
-o content_filter=
-o receive_override_options=no_unknown_recipient_checks,no_header_body_checks,no_milters
-o smtpd_helo_restrictions=
-o smtpd_client_restrictions=
-o smtpd_sender_restrictions=
-o smtpd_recipient_restrictions=permit_mynetworks,reject
-o mynetworks=127.0.0.0/8
-o smtpd_authorized_xforward_hosts=127.0.0.0/8

What it does?:
master process will be listening on port 10025. Whatever will be received to that port it will be sent using the stdin to /usr/local/bin/mail_delay.sh . This program/script will process the mail and send it to the port 10026. 10026 port will receive the mail and carry on the process to send the email.

The script /usr/local/bin/mail_delay.sh is a very simple script (I have some debugging information that I’ve strip it out here):
#!/bin/bash
sleep 60
nc localhost 10026
exit $?

“nc” (Debian package netcat) reads from the stdin and send the output to localhost, port 10026. Of course, before doing that it waits 60 seconds.

Disclaimer:

  • I use Postfix only as a hobby, I’m not a Postfix expert. Apologises for the problems or mistakes of this configuration. However, it works for me.
  • I’ve only tested it on my personal system. If this Postfix needed to handle thousands of mails per minute the above configuration would have problems.
  • If you are curious, you have seen that I’ve limited the simultaneous processes to one. So, my mails are sent one after another, that helps a bit because I’m using Gandi as a final SMTP and I should send only 5 mails/minute. I don’t guarantee that the system will work if you increase the limit (7th column in master.cf). Actually I cannot guarantee that this would work even without changing the configuration.
  • This is tested using Postfix 2.6.5-3 on a Debian system

1 comment to Delay the mails with Postfix

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>