Friday, October 1, 2010

FreeBSD: Use Gmail for outgoing mail

Rather than configuring sendmail, it's pretty easy to use secure SMTP and let Google handle your routing and all that nonsense. You can also receive root's mail at your gmail account which is very convenient.


1. Disable sendmail by adding the following to /etc/rc.conf [reference]:

sendmail_enable="NO"
sendmail_submit_enable="NO"
sendmail_outbound_enable="NO"
sendmail_msp_queue_enable="NO"

2. Kill sendmail (or reboot):

# killall sendmail

3. Install ssmtp:

# pkg_add -r ssmtp

4. Configure ssmtp by adding the file /usr/local/etc/ssmtp/ssmtp.conf:

root=destinationemail@gmail.com
mailhub=smtp.gmail.com:587
AuthUser=serveremail@gmail.com
AuthPass=yourpassword
UseSTARTTLS=YES

For security reasons I use a separate gmail account from my normal gmail account. Use the separate account's address and password for AuthUser and AuthPass, but put your standard email address on the root line to redirect root mail there.


5. Test ssmtp. Enter this command and then type your message. Send Ctrl-D when finished:

# ssmtp -v youremail@gmail.com

6. Change /etc/mail/mailer.conf from:

sendmail    /usr/libexec/sendmail/sendmail
send-mail   /usr/libexec/sendmail/sendmail
mailq       /usr/libexec/sendmail/sendmail
newaliases  /usr/libexec/sendmail/sendmail
hoststat    /usr/libexec/sendmail/sendmail
purgestat   /usr/libexec/sendmail/sendmail

to:

sendmail    /usr/local/sbin/ssmtp
send-mail   /usr/local/sbin/ssmtp
mailq       /usr/libexec/sendmail/sendmail
newaliases  /usr/libexec/sendmail/sendmail
hoststat    /usr/libexec/sendmail/sendmail
purgestat   /usr/libexec/sendmail/sendmail

(there is also a script that comes with ssmtp to do this, see this guide)


7. Test "sendmail". Enter this command and then enter a message. Type a period on a new line when finished:

# mail -v -s "MySubject" youremail@gmail.com

8. If you are seeing a weird name (mine was "Charlie &" for some reason), make sure to change root's Full Name using the chpass command.


9. If you are paranoid or on a shared server, follow this guide to run ssmtp as a user instead of root. This might be a good idea anyway; ssmtp segfaulted when I had AuthPassword instead of AuthPass in the config file. Doesn't seem like safe code.


10. Test root's mail is really being redirected by running


# periodic daily

You should receive two status emails.


See also:


Be the first to reply!

Post a Comment

By submitting a comment you assert that it is your own original work and agree to grant a non-exclusive licence to Brandon Thomson to display it on log.bthomson.com.