Linuxaria

Everything about Linux
  • Home
  • Articles
    • News
  • Guides
  • Interview
  • Pills
  • Reviews
  • Information
    • Contribute
    • Contact me
    • Info
    • Privacy Policy
  • Links
    • GMStyle
    • Il Bloggatore
    • Linux
    • Linux Today
    • Linuxfeed
    • LinuxInsight
    • Lxer
    • ZioBudda

sponsored links

How to configure postfix as smtp relay for Gmail on Archlinux

 Guides, Pills  Add comments
Jan 032013
 

Original article by AlexioBash, posted in Italian on the website http://alexiobash.com.

It can be very convenient, for those who use services such as zoneminder, rtorrent, etc., to have a “Gmail Relay” to send maisl from your Gmail account.
In this guide we will see how to configure postfix to send email with our account [email protected].
The guide is based on ArchLinux, but apart the installation of the packages, the configuration is the same also on other distributions such as Debian or Centos.




As first thing we need to install the packages with :

$ sudo pacman -Sy postfix ca-certificates

$ sudo pacman -Sy postfix ca-certificates

Once the installation is complete go into the directory where postfix keep its configuration files :

$ cd /etc/postfix

$ cd /etc/postfix

And do a copy of the default configuration, just to be sure.

$ sudo cp main.cf main.cf-old

$ sudo cp main.cf main.cf-old

Now open the file main.cf with your favorite editor and delete everything (with vi just type 1000 dd and copy this configuration:

daemon_directory = /usr/lib/postfix
 
myorigin = myrealdomain.com  # substitute myrealdomain.com with your domain name
mydomain = myrealdomain.com
myhostname = myrealdomain.com
 
smtp_generic_maps = hash:/etc/postfix/generic
header_checks = regexp:/etc/postfix/header_checks
 
relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd_gmail
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/postfix/cacert.pem
smtp_use_tls = yes
smtp_tls_security_level = encrypt
smtp_enforce_tls = yes
 
disable_dns_lookups = yes
 
message_size_limit = 35651584
 
smtp_destination_rate_delay = 60
relay_destination_rate_delay = 60
 
mynetworks = 127.0.0.1
 
alias_maps = hash:/etc/aliases

daemon_directory = /usr/lib/postfix myorigin = myrealdomain.com # substitute myrealdomain.com with your domain name mydomain = myrealdomain.com myhostname = myrealdomain.com smtp_generic_maps = hash:/etc/postfix/generic header_checks = regexp:/etc/postfix/header_checks relayhost = [smtp.gmail.com]:587 smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd_gmail smtp_sasl_security_options = noanonymous smtp_tls_CAfile = /etc/postfix/cacert.pem smtp_use_tls = yes smtp_tls_security_level = encrypt smtp_enforce_tls = yes disable_dns_lookups = yes message_size_limit = 35651584 smtp_destination_rate_delay = 60 relay_destination_rate_delay = 60 mynetworks = 127.0.0.1 alias_maps = hash:/etc/aliases

Save the file as main.cf and don’t forget to substitute myrealdomain.com with your real domain name, for me this would be linuxaria.com.

Now create the file /etc/postfix/sasl_passwd_gmail with your favorite editor and insert in it this line:

[smtp.gmail.com]:587             username@gmail.com:mypassword

[smtp.gmail.com]:587             [email protected]:mypassword

Substitute username with your real username and mypassword with your real password.
Now add at the end of the file /etc/postfix/header_checks the following lines:

/^To:.*@hostname.localnet/ REDIRECT username@gmail.com 
/^From:(.*@myrealdomain.com[>]*)/ PREPEND Reply-To:$1

/^To:.*@hostname.localnet/ REDIRECT [email protected] /^From:(.*@myrealdomain.com[>]*)/ PREPEND Reply-To:$1

As usual substitute username with your real username and myrealdomain with your real domain name already used in the file main.cf.
Now add at the end of the file /etc/postfix/generic the following lines:

root@myrealdomain.com         username@gmail.com
root@hostname.localnet        username@gmail.com
sudoer@hostname.localnet    username@gmail.com

[email protected] [email protected] [email protected] [email protected] [email protected] [email protected]

Guess what ?
Yes you have to substitute as usual username and myrealdomain.

Now open a terminal as root and run these commands:

$ postmap /etc/postfix/generic
$ postmap /etc/postfix/sasl_passwd_gmail
$ cat /etc/ssl/certs/Equifax_Secure_CA.pem >/etc/postfix/cacert.pem
$ ln -s /etc/postfix/aliases.db /etc/aliases.db
$ postconf -e alias_maps=hash:/etc/aliases

$ postmap /etc/postfix/generic $ postmap /etc/postfix/sasl_passwd_gmail $ cat /etc/ssl/certs/Equifax_Secure_CA.pem >/etc/postfix/cacert.pem $ ln -s /etc/postfix/aliases.db /etc/aliases.db $ postconf -e alias_maps=hash:/etc/aliases

Start postfix with the command:

$ systemctl start postfix

$ systemctl start postfix

Enable it at boot time with the command:

$ systemctl enable postfix

$ systemctl enable postfix

At this point, we can test the whole configuration by sending an email with the following command:

$ echo "body of the message" | mail -r "[email protected]" -s "Subject of the mail" destination@email.com

$ echo "body of the message" | mail -r "[email protected]" -s "Subject of the mail" [email protected]

This command will send a mailwith from [email protected] to [email protected] directly from your ArchLinux.
To verify the correct transmission we can at any time view the log located at /var/log/mail.log and keep track of sent emails.

If you want to send mail remotely, for example from another PC of the same network, you first need to put in the file main.cf in the directive mynetworks the cilent ip, this will enable it to use this machine as relay, restart postfix and on the client issue the command:

$ echo "body of the message" | mail -r "[email protected]" -s "Subject of the mail" -S smtp=IPPOSTFIX destination@email.com

$ echo "body of the message" | mail -r "[email protected]" -s "Subject of the mail" -S smtp=IPPOSTFIX [email protected]

Replacing “IPPOSTFIX” with the IP of the machine where we have configured postfix.

If you want to enable an entire network, it is possible to include in the directive mynetworks the entire subnet, eg 192.168.0.0/24.

Popular Posts:

    None Found

Flattr this!

 Posted by linuxari at 23:36  Tagged with: ArchLinux, ca certificates, gmail account, linux, pacman, postfix, relay

  8 Responses to “How to configure postfix as smtp relay for Gmail on Archlinux”

  1. Mike says:
    Tuesday January 8th, 2013 at 11:43 PM

    What about passwords that use special characters? Is there a way to quote or escape them in the sasl_passwd_gmail file?
    When I follow this I get: (SASL authentication failed; server smtp.gmail.com[173.194.79.108] said: 535-5.7.1 Username and Password not accepted. Learn more at?535 5.7.1 http://support.google.com/mail/bin/answer.py?answer=14257 is3sm40319265pbc.6)

    I know I have the correct password. 🙂

    Reply
    • linuxari says:
      Wednesday January 9th, 2013 at 01:23 AM

      Hello Mike

      I’ve took a look at the man page of postmap:

      …

      The key and value are processed as is, except that surrounding white space is stripped off. Unlike with Postfix alias databases, quotes cannot be used to protect lookup keys that contain special characters such as `#’ or whitespace.

      …

      The format posted worked for me, but I’ve found another post : http://fastmail.wikia.com/wiki/ConfiguringPostfix that sugegst to substitute the @ with a # :

      Don’t forget to replace with your Fastmail username and with the domain of your Fastmail account (eg fastmail.fm, eml.cc, etc), and with your Fastmail password. The ‘#’ seems to be important, using the regular ‘@’ symbol caused authentication errors.

      Can you also do a test changing your password to one without that particular special character ?

      I hope these links can help you.
      Best regards

      Reply
  2. Aditya Raj Bhatt says:
    Monday March 4th, 2013 at 12:07 PM

    There is a small typo. You have written systemct instead of systemctl after “Start postfix with the command : “.

    Reply
    • linuxari says:
      Monday March 4th, 2013 at 06:52 PM

      Thanks Aditya, typo fixed

      Reply
  3. alexiobash says:
    Wednesday March 5th, 2014 at 01:48 PM

    Please, update the dark-linux link in alexiobash.com!
    nice day!

    Reply
  4. Isaias says:
    Tuesday January 27th, 2015 at 06:45 PM

    Excelente, funcionó en 3 minutos, quisiera agregar que para certificados me funciono entrando como ROOT, aunque estaba con sudo solo me funcionó como ROOT.

    FUNCIóNa…

    Reply
  5. Mauricio says:
    Friday January 22nd, 2016 at 10:00 AM

    Postfix got an update… needed to do this change:

    /etc/postfix/main.cf
    daemon_directory = /usr/lib/postfix/bin

    Hope it helps someone

    Reply
  6. vhumphrey says:
    Sunday May 29th, 2016 at 08:24 PM

    Thank you! I have been trying to get this to work for a week.

    Reply

 Leave a Reply Cancel reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

CAPTCHA
Refresh

*

  Configuring 2 mirrored disks on Ubuntu Linux   How to easily encrypt a file with GPG on Linux

Language:

  • English
  • Italiano
Facebook

Sponsor

buy tablet pc
ssd vps
apk monk

Follow Me

RSS Twitter Facebook Google+

Popular posts

    None Found

Subscribe by Email

Subscribe to RSS» English by mail

Iscriviti agli RSS» Italiano via Email

Recent Comments

  • Ashwin on What You Don’t Know About Linux Open Source Could Be Costing to More Than You Think
  • frann on gEdit, an easy to use text editor with many advanced features
  • greg125 on The best way to move data
  • Ashwin on Top Five Mobile Devices That Run Linux
  • Ashwin on Paas and continuos integration

RSS My scoop.it feed

© 2013 Linuxaria All site content, except where otherwise noted, is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Suffusion theme by Sayontan Sinha
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.Accept Read More
Privacy & Cookies Policy