Pages

2.19.2013

Postfix configured to send mail through gmail as relay host

This is going to be my first blog post so cut me some slack. These posts will become more professional as the blog matures.

PURPOSE:
Why do this? I wanted my home Linux server to send me logwatch logs, alerts and shit to my personal gmail account. I wanted to use postfix since it’s the resident smtp server and didn’t feel the need to install any additional garbage.

ENVIRONMENT:
  • Centos 6
  • 2.6.32-279.22.1.el6.i686
  • postfix-2.6.6-2.2.el6_1.i686
PROCEDURES:
First, make sure the following lines exist in your /etc/postfix/main.cf (the most important lines are bold):
queue_directory = /var/spool/postfix
command_directory = /usr/sbin
daemon_directory = /usr/libexec/postfix
data_directory = /var/lib/postfix
mail_owner = postfix
inet_interfaces = localhost
inet_protocols = all
myhostname = kong.example.com
mydestination = kong.example.com
unknown_local_recipient_reject_code = 550
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
 
debug_peer_level = 2
debugger_command =
     PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin
     ddd $daemon_directory/$process_name $process_id & sleep 5
sendmail_path = /usr/sbin/sendmail.postfix
newaliases_path = /usr/bin/newaliases.postfix
mailq_path = /usr/bin/mailq.postfix
setgid_group = postdrop
html_directory = no
manpage_directory = /usr/share/man
sample_directory = /usr/share/doc/postfix-2.6.6/samples
readme_directory = /usr/share/doc/postfix-2.6.6/README_FILES

##########
# Forward mail through Gmail
relayhost = [smtp.gmail.com]:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/pki/tls/cert.pem


Next, create the /etc/postfix/sasl_passwd file with the following one liner:
[smtp.gmail.com]:587 crazyemailaddress@gmail.com:cr@zypas$word

Next, run this command: sudo postmap /etc/postfix/sasl_passwd
That will create the sasl_passwd.db file.


IMPORTANT SECURITY TIP: Make sure you give full ownership to root with 600 permissions because your email credentials will be stored in cleartext.

Restart your mail server: service postfix restart

Extra step for root mail forwarding using aliases:
http://systemstuds.blogspot.com/2013/02/send-roots-mail-to-someone-else-using.html

Finally, test your configuration:
mail someemailaddress@gmail.com

If you didn’t receive the email check your /var/log/maillog for problems. I had a certificate issue my first go round. Then I had to check that gmail didn’t lock my account out for sending too much spam :P
Good Luck!


No comments:

Post a Comment