Configure Postfix through cloud-init

Configure Postfix through cloud-init

I recently decided to set up Nagios for monitoring a small cluster of nodes I’m running, and set up alerts to be sent out through email; however, I didn’t want those emails being sent directly from the VPS itself. What I did was set up this cloud-config script to be executed in order to spin up the VPS with postfix configured to connect to a Google email account in order to send out notification emails. This should help with minimizing the possibility that the email will be seen as spam and I won’t have to add additional IP addresses to my SPF record. Here’s the script for you to try out if you’re interested in getting this set up:

#cloud-config

packages:
  - postfix
  - mailutils
  - libsasl2-2
  - ca-certificates
  - libsasl2-modules

write_files:
  - path: /etc/postfix/sasl_passwd
    content: |
      [smtp.gmail.com]:587    [email protected]:PASSWORD
    owner: root:root
    permissions: 0400

runcmd:
  - sed -i 's/relayhost =/relayhost = [smtp.gmail.com]:587/g' /etc/postfix/main.cf
  - echo "smtp_sasl_auth_enable = yes" >> /etc/postfix/main.cf
  - echo "smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd" >> /etc/postfix/main.cf
  - echo "smtp_sasl_security_options = noanonymous" >> /etc/postfix/main.cf
  - echo "smtp_tls_CAfile = /etc/postfix/cacert.pem" >> /etc/postfix/main.cf
  - echo "smtp_use_tls = yes" >> /etc/postfix/main.cf
  - postmap /etc/postfix/sasl_passwd
  - chmod 0400 /etc/postfix/sasl_passwd.db
  - cat /etc/ssl/certs/thawte_Primary_Root_CA.pem >> /etc/postfix/cacert.pem
  - systemctl restart postfix

package_upgrade: true

Now anytime that mail is sent using mail, it will be sent from your Gmail/Google Apps email account. You can test this out by executing:

echo "Testing Postfix configuration." | mail -s "Simple Postfix Test" [email protected]

If you’re running into issues and can’t send email, you’ll want to check out your mail log. In most cases, you just need to allow less secure apps to connect in order to send. More information on that can be seen here.

Popular posts from this blog

Install gdu

resize your LVM partition