One of the needs that came up was to be able to use separate relay configurations based on the sender email address, using sendmail. The problem is that sendmail is missing support for most parts of that sentence.
At the end the solution involved a combination of sendmail, smarttable, procmail and msmtp
The idea is the following:
- Use smarttable to implement sender based rules
- Use the procmail mailer support to use procmail to deliver the emails
- Use procmailrc to pipe messages to msmtp
- Use msmtp to relay via external hosts
Sender based rules
In order to be able to have sender-based rules I used smarttable.m4 from here.
Download the smarttable.m4 and (assuming sendmail config is under /etc/mail) place it under /etc/mail/m4/. Normally it should be placed along the rest of the sendmail features (/usr/share/sendmail/cf/features) but I don’t like polluting system dirs. Then use the following config in sendmail.mc:
dnl Change the _CF_DIR for a bit to load the feature from /etc/mail/m4 dnl then change it back define(`_CF_DIR_OLD', _CF_DIR_)dnl define(`_CF_DIR_', `/etc/mail/m4/')dnl dnl This has to be a hash. I.e. not text. FEATURE(`smarttable',`hash -o /etc/mail/smarttable')dnl define(`_CF_DIR_', _CF_DIR_OLD)dnl
Then configure smarttable (/etc/mail/smarttable) like this:
You can add as many lines as you like, one for each sender. See smarttable’s web page for more information on the supported sender formats. Dont’ forget to generate the hashed version (smarttable.db)
Configure sendmail for procmail mailer like this:
define(`PROCMAIL_MAILER_ARGS', `procmail -Y -t -m $h $f $u')dnl MAILER(`procmail')dnl
You have to override the default procmail parameters in order to add the -t switch. This way delivery errors will be interpreted as softfails, otherwise mails will be rejected on the first failure.
Create /etc/mail/persource and put the procmail configs in there (nice and tidy). In this example create /etc/mail/persources/test.test.com.procmailrc as follows:
:0w |/usr/bin/msmtp -C /etc/mail/persource/test.test.com.msmtprc -a firstname.lastname@example.org -t
The ‘w’ flag is essential in order to feed failures back to sendmail.
Create the msmtp config file (/etc/mail/persource/test.test.com.msmtprc) as follows:
defaults syslog on # logfile /email@example.com account firstname.lastname@example.org host smtp.gmail.com from email@example.com user firstname.lastname@example.org password xxx auth on tls on tls_trust_file /etc/ssl/certs/ca-certificates.crt
Your mileage may vary. They above is good for gmail accounts on a debian system.
And that’s it. Sending an email as email@example.com will cause sendmail to use smarttable. This will match the sender and use procmail with our config to deliver the email. Procmail will pipe the email to msmtp which will send the email via google’s email servers.