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 |
Once the installation is complete go into the directory where postfix keep its configuration files :
$ cd /etc/postfix |
And do a copy of the default configuration, just to be sure.
$ 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 |
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 |
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 |
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 |
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 |
Start postfix with the command:
$ systemctl start postfix |
Enable it at boot time with the command:
$ 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 |
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 |
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
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. 🙂
Hello Mike
I’ve took a look at the man page of postmap:
…
…
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 # :
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
There is a small typo. You have written systemct instead of systemctl after “Start postfix with the command : “.
Thanks Aditya, typo fixed
Please, update the dark-linux link in alexiobash.com!
nice day!
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…
Postfix got an update… needed to do this change:
/etc/postfix/main.cf
daemon_directory = /usr/lib/postfix/bin
Hope it helps someone
Thank you! I have been trying to get this to work for a week.