Send Email on OS X Lion through GMail

This guide has been superseded by Send Email on OS X Yosemite through GMail

This guide covers how to utilise the installed postfix program so that it relays local email through google mail. I predominately use this for notifications such as logs of cron tasks that have run. This guide borrows heavily from the references listed at the end.

Configuration

Launch configuration

Edit /System/Library/LaunchDaemons/org.postfix.master.plist to include OnDemand key. The resulting file should look like:

<?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>/Library/Server/Mail/Data/spool/maildrop</string>
  </array>
  <key>AbandonProcessGroup</key>
  <true/>
  <key>OnDemand</key>
  <true/>
</dict>
</plist>

Postfix main configuration

Edit /etc/postfix/main.cf to include necessary configuration. Add the following to the end of the file:

relayhost = smtp.gmail.com:587
smtp_generic_maps = hash:/etc/postfix/generic
smtp_tls_loglevel=1
smtp_tls_security_level=encrypt
smtp_sasl_auth_enable=yes
smtp_sasl_password_maps=hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous

Postfix aliases

First, note the username of the user you wish to receive root’s email. If logged in as the user, you can check the value by running:

whoami

Edit /etc/postfix/aliases enabling root email to be aliased to your user. Change the following line:

#root:    you

by uncommenting and changing you to your username, e.g.:

root:    username

You need to then run newaliases to pick up the modifications:

cd /etc/postfix
sudo newaliases

Postfix local email to gmail mappings

In order to configure correctly, note the user and hostname you want to map:

whoami
hostname

Edit /etc/postfix/generic adding mappings of local email to gmail accounts, substituting username, hostname and gmailaccount as appropriate:

username@hostname gmailaccount
@hostname gmailaccount

You need to then run postmap to pick up the modifications:

cd /etc/postfix
sudo postmap generic

SMTP password

Ensure that the password file exists:

cd /etc/postfix
sudo touch sasl_passwd

Edit /etc/postfix/sasl_passwd adding the details for gmail:

smtp.gmail.com:587 gmailaccount:gmailpassword

Rebuild the postmap database file and delete the raw file containing the raw password:

sudo postmap sasl_passwd
sudo rm sasl_passwd

Forwarding using .forward

Individual users can use sendmail forwarding by creating ~/.forward containing only the email address to forward to. This will override the accounts configured above, allowing someone without sudo permissions to configure their desired email address.

Run postfix

Run the following commands to ensure postfix is running with the latest configuration:

sudo launchctl unload -w /System/Library/LaunchDaemons/org.postfix.master.plist
sudo launchctl load -w /System/Library/LaunchDaemons/org.postfix.master.plist

Testing

Sending to root

Try the following at a command prompt, filling in the prompts:

mail root
Subject: Test root
<CTRL+D>

Sending to username

Try the following at a command prompt, filling in the prompts:

mail username
Subject: Test username
<CTRL+D>

Sending an external email

Try the following at a command prompt, filling in the prompts:

mail user@example.com
Subject: Test email
<CTRL+D>

Problems

If email is not working, the following may help.

Tailing the mail logs

You can look at and tail the mail log for details of what is happening.

tail -f /var/log/mail.log

Spool directory not found

If you get the following error:

sendmail: fatal: chdir /Library/Server/Mail/Data/spool: No such file or directory

Run the following commands to fix it:

sudo mkdir -p /Library/Server/Mail/Data/spool
sudo gzip /usr/share/man/man1/{postalias.1,postcat.1,postconf.1,postdrop.1,postfix.1,postkick.1,postlock.1,postlog.1,postmap.1,postmulti.1,postqueue.1,postsuper.1,sendmail.1}
sudo gzip /usr/share/man/man5/{access.5,aliases.5,bounce.5,canonical.5,cidr_table.5,generic.5,header_checks.5,ldap_table.5,master.5,mysql_table.5,nisplus_table.5,pcre_table.5,pgsql_table.5,postconf.5,postfix-wrapper.5,regexp_table.5,relocated.5,tcp_table.5,transport.5,virtual.5}
sudo gzip /usr/share/man/man8/{anvil.8,bounce.8,cleanup.8,discard.8,error.8,flush.8,local.8,master.8,oqmgr.8,pickup.8,pipe.8,proxymap.8,qmgr.8,qmqpd.8,scache.8,showq.8,smtp.8,smtpd.8,spawn.8,tlsmgr.8,trivial-rewrite.8,verify.8,virtual.8}
sudo /usr/sbin/postfix set-permissions

References

Comments

blog comments powered by Disqus
Fork me on GitHub