Postfix – Relay via Gmail

Setting up Sendmail to relay public SMTP server such as Gmail is quite difficult. In the past, i just kept looking for the blog posts in Google and found that people have different ways to setup Sendmail, but not all of them could be repeated successfully in my setup. After so many trials and errors, i made the relay works but i couldn’t fully understand what’s going on in the setup.

So this time, no more Sendmail, i would like to use Postfix instead.
System:

  • OS: Ubuntu Lucid (Ubuntu 10.04)

 

1. Install the Postfix package. Select No configuration in the installation.

  • apt-get install postfix

 

2. Copy the config file to /etc/postfix

  • cp /usr/share/postfix/main.cf.debian /etc/postfix/main.cf

 

3. Install other required packages but they should be installed by default in Ubuntu Lucid.

  • libsasl2-2
  • libsasl2-modules
  • ca-certificates

 

4. Append the following configuration to /etc/postfix/main.cf.

relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/postfix/cacert.pem
smtp_use_tls = yes

 

5. Create the /etc/postfix/sasl_passwd.

[smtp.gmail.com]:587    <username>@gmail.com:<password>

 

6. Change the file permission of the /etc/postfix/sasl_passwd.

  • chmod 400 /etc/postfix/sasl_passwd

 

7. Translate the /etc/postfix/sasl_passwd to Postfix lookup tables.

  • postmap /etc/postfix/sasl_passwd

 

8. Create the /etc/aliases.db

  • postalias hash:/etc/aliases

 

9. Create the /etc/postfix/cacert.pem.

  • cat /etc/ssl/certs/Equifax_Secure_CA.pem >> /etc/postfix/cacert.pem

 

10. Restart Postfix.

  • /etc/init.d/postfix restart

 

11. If you don’t have the mail command, install the mailutils package

  • apt-get install mailutils

 

12. Send your first email with the our relay setting.

  • mail -s “Hello World” <recipient’s email address>

 

Done =)

Reference: Relaying Postfix SMTP via smtp.gmail.com

40 thoughts on “Postfix – Relay via Gmail”

  1. You are a savior! This is likely the 6th guide on setting up postfix I have read, and the only one that has done me any good.

    Like

  2. Worked perfectly for me, thank you. I had no experience setting up MTAs and tried a number of different tutorials with no success before I came across this one.

    Like

    1. you are welcome~ email configuration is really hard to be understood~
      here just shows the basic usage but i still got many unknowns. =.=

      anyway, good to know that i could help =D

      Like

  3. Works great, only one question to see if anyone solves.
    How do I can not even bring up the gmail account?
    shows the name of the account that postfix shipping, but the e-mail is Gmail.

    regards

    Like

    1. i think that could not be done since we are really using that gmail account to deliver the email.

      see if other ppl could answer this question.

      and thanks for your comment. =)

      Like

  4. Great tutorial. Only had one problem. I couldn’t complete the cat command. The error was: “/etc/postfix/cacert.pem: Permission denied”
    I had to chmod the file to 666, and then I was able to complete the cat command. I did chmod it back to 644.

    Like

  5. arghh

    i got a problem at the last moment…
    the mail command tells me to either download mailutils or heirloom-mailx but apt-get doesn’t find any candidate version, aptitude doesn’t either.
    any comment ?

    Like

    1. of course found the solution right after asking…
      aptitude install mailx

      Tried that after looking into the dependencies needed when instaling with .deb package.

      Like

    1. Sorry, i couldn’t get what you mean. After you have setup the relay, you can send email using the mail command or php mail() function.

      You can also use cron job the send email.

      Like

      1. I am a library professional from India. I shared your post in Koha (koha-community.org) discussion forum. Many librarians made use your solution to send notices from Koha library management system. I express my thanks in favor of library professionals.

        Like

  6. As many others have said, thank you so much for putting this up! Worked like a charm.

    One question – right now when I get the email it shows that it’s from “root” (with the email address specified in sasl_passwd). What would I change to make that say something else like say, “Automated Server Message”?

    Like

  7. Hello, on ubuntu 16.04 while configuring postfix for Koha Library management system, at this step,
    cat /etc/ssl/certs/Equifax_Secure_CA.pem >> /etc/postfix/cacert.pem, it shows. cat: /etc/ssl/certs/Equifax_Secure_CA.pem: No such file or directory.
    Is there anyway, to fix the issue and make postfix to send emails?

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.