Getting Postfix sending email on your Mac

I have recently been setting up my MacBook (running Leopard) to send email using the local email delivery system, i.e. Postfix. This means modifying the default installation to relay email through my ISP/email service - in my case; FastMail. I use FastMail because they have a nice secure email setup with both IMAP and SMTP access encrypted using SSL, but the instructions below will also work with Google Mail. The advantage of having your Apple Mac set up like this is that it enables command line scripts and various unix programs to send email. In my case, it allows me to use Mutt and Emacs as my email client.

The first step is to modify the main.cf file in the /etc/postfix directory. Use your favourite editor to do this, but you will need to sudo in order to modify the file. If you are using vi, type ‘sudo vi /etc/postfix/main.cf’ in a terminal window. Then search for a commented out “relayhost =” line and add the following:

relayhost = [mail.messagingengine.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = 
smtp_sasl_local_domain = yourdomain.com
#smtpd_sasl_application_name = smtpd
broken_sasl_auth_clients = yes
smtpd_pw_server_security_options = noanonymous
smtp_use_tls=yes
smtp_tls_security_level=encrypt
tls_random_source=dev:/dev/urandom

Now I am relaying my email through the server mail.messagingengine.com and Postfix will attempt to connect to the server on port 587. The square brackets around “mail.messagingengine.com” tell Postfix not to do an MX lookup on the name, but just got to that IP address. You will want to modify these values to suit your ISP/email service. For GMail you can use the line:

relayhost=smtp.gmail.com:587

The next step is to create the file “/etc/postfix/sasl_passwd”. This file contains the information you need to use to authenticate yourself against the relaying mail server (in other words, your GMail username and password). Add the following lines to the /etc/postfix/sasl_passwd file:

[mail.messagingengine.com]:587 *username*:*password*

Or if you use GMail:

smtp.gmail.com:587 *username*@gmail.com:*password*

Remember to replace *username* with your actual username and *password* with your password.

From the Terminal command line, run the following commands:

sudo postmap hash:/etc/postfix/sasl_passwd
sudo chown root:wheel /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
sudo chmod 600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db

These commands create the “/etc/postfix/sasl_passwd.db” file from the “/etc/postfix/sasl_passwd” file, and then change the permissions on the file so that they can only be read by the root user.

Congratulations! You should now be good-to-go. Try sending an email from the command line by typing “mail -s ‘Test Email’ yourname@yourisp.com” at the command line. Type some words and then type “Ctrl-D” to finish. You can monitor the log file at /var/log/mail.log in order to see what is happening, and use the “mailq” program to see the state of the mail queue.