Howto send mails from localhost through a smarthost on OSX

You’re getting weird errors like “Connection refused” from your local smtp library? You may have to reconfigure your local mailer.

Smarthost

Setting up your Mac to use a smarthost to send mails through is not very hard. I found a nice howto on this site: http://fuerstnet.de/en/sending-email-local-development-machine-using.

I’ve copied the central lines:

sudo postconf -e relayhost=your.isps.mailserver smtp_use_tls=yes smtp_sasl_auth_enable=yes smtp_sasl_password_maps=hash:/etc/postfix/smtp_auth tls_random_source=dev:/dev/urandom smtp_sasl_security_options=noanonymous
sudo sh -c 'echo "your.isps.mailserver username:password" >> /etc/postfix/smtp_auth'
sudo chown root:wheel /etc/postfix/smtp_auth
sudo chmod 600 /etc/postfix/smtp_auth
sudo postmap hash:/etc/postfix/smtp_auth

Problems

I noticed postfix is quitting after 60 seconds. So if you send yourself a test email using: echo "test" | mail -s "test" your@email the postfix daemon will quit after 60 seconds and you’ll get these errors when trying to send mails through localhost in Plone:

Traceback (innermost last):
  Module ZPublisher.Publish, line 119, in publish
  Module ZPublisher.mapply, line 88, in mapply
  Module ZPublisher.Publish, line 42, in call_object
  ...
  Module Products.SecureMailHost.SecureMailHost, line 166, in send
  Module Products.MailHost.MailHost, line 144, in send
  Module Products.SecureMailHost.SecureMailHost, line 276, in _send
  Module Products.SecureMailHost.mail, line 102, in send
  Module smtplib, line 244, in __init__
  Module smtplib, line 310, in connect
error: (61, 'Connection refused')

Solution

You need a persistent postfix! Open /System/Library/LaunchDaemons/org.postfix.master.plist and check the programm arguments. They contain -e 60 by default and that’s the exit time of the master process which is described in master’s man-page as follows:

-e exit_time
  Terminate the master process after exit_time seconds.
  Child processes terminate at their convenience.

So let’s modify this org.postfix.master.plist file:

sudo vim /System/Library/LaunchDaemons/org.postfix.master.plist

Remove these programm arguments and add the RunAtLoad statement to startup automatically (remove highlighted lines):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Label</key>
        <string>org.postfix.master</string>
        <key>Program</key>
        <string>/usr/libexec/postfix/master</string>
        <key>ProgramArguments</key>

        <array>
                <string>master</string>
                <!--
                <string>-e</string>
                <string>60</string>
                -->
        </array>
        <key>QueueDirectories</key>
        <array>
                <string>/var/spool/postfix/maildrop</string>
        </array>

        <key>RunAtLoad</key>
        <true />
</dict>
</plist>

Restart your postfix daemon using the commands below or reboot and it should not exit any more:

sudo launchctl stop org.postfix.master
sudo launchctl unload /System/Library/LaunchDaemons/org.postfix.master.plist
sudo launchctl load /System/Library/LaunchDaemons/org.postfix.master.plist
sudo launchctl start org.postfix.master

Comments

comments powered by Disqus