Gmail As Relay On Ubuntu
tags: mail gmail ubuntu postfix relay exim4
WARNING | |
---|---|
This is recommended only for advanced users, proceed with caution. |
This can be used to send mails from your HDA and to receive system emails.
To receive emails meant for the root user, you have to Forward System Emails after finishing this tutorial.
NOTE: Recommend you do not use copy and paste as there are hidden special characters that sometimes cause issues.
Option 1 (Postfix)
sudo apt-get -y install postfix mailutils libsasl2-2 ca-certificates libsasl2-modules
- NOTE: During install of postfix, a popup box may display. If it does, select OK and then Internet. Add your domain name (i.e. home.com) and then select OK.
Add the following to the bottom of /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
Create /etc/postfix/sasl_passwd file with your Gmail login credentials as shown below (change user and password to your Gmail user name and password.):
[smtp.gmail.com]:587 user@gmail.com:password
Create the password database:
sudo postmap hash:/etc/postfix/sasl_passwd
Protect the files with your Gmail login data:
sudo chown root:root /etc/postfix/sasl_passwd sudo chmod 600 /etc/postfix/sasl_passwd
Validating the certificate:
cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem | sudo tee -a /etc/postfix/cacert.pem
Restart Postfix:
sudo /etc/init.d/postfix restart
Option 2 (Exim4)
sudo apt-get install exim4 mailx
Ensure you verify the hostname before proceeding. You will need this later.
hostname
After installing exim4 we need to configure it. This is done by the following command:
sudo dpkg-reconfigure exim4-config
Now you need to answer some questions. Don't worry I'll give you the answers to those questions.
- General type of mail configuration:
mail sent by smarthost; received via SMTP or fetchmail
- The next question asks for the system mail name:
Set to same as hostname
- Now it asks you what IP addresses should be allowed to use the server.
Leave as is (127.0.0.1 ; ::1)
- Other destinations for which mail is accepted:
Set to same as hostname
- Machines to relay mail for:
Leave blank
- IP address or host name of outgoing smarthost:
smtp.gmail.com::587
- Hide local mail name in outgoing mail:
No
- Keep number of DNS-queries minimal:
No
- Delivery method for local mail:
mbox format in /var/mail/
- Split configuration into small files:
No
After answering all these questions exim4 will restart and we're halfway home. Now you'll have to enter your account details. As root, edit the file /etc/exim4/passwd.client and add the next three lines at the end of the file.
gmail-smtp.l.google.com:YOU@gmail.com:PASSWORD *.google.com:YOU@gmail.com:PASSWORD smtp.gmail.com:YOU@gmail.com:PASSWORD
- NOTE: You'll have to change YOU to your Gmail login name, and PASSWORD to your password on all three lines.
This password file contains sensitive account information. You should verify its file permissions and ownership.
$ ls -al /etc/exim4/passwd.client
-rw-r----- 1 root Debian-exim 252 Mar 25 17:24 /etc/exim4/passwd.client
If they are different from the above, use the following commands to set file permissions and ownership.
sudo chown root:Debian-exim /etc/exim4/passwd.client sudo chmod 640 /etc/exim4/passwd.client
After that you only have to update and restart exim and you're done! The next two lines will do that for you:
sudo update-exim4.conf sudo /etc/init.d/exim4 restart exim4 -qff
- NOTE: The
-qff
parameter forces a delivery attempt for every message, whether frozen or not.
- NOTE: The
Test Email
Now try sending a mail, it should reach your Gmail account (change user to match your account name):
echo "This is Content" | sudo mail -s "This is subject" user@gmail.com
Check the mail log:
tail -f /var/log/mail.log
If all goes well, you should not see any errors. When you check your account, it is possible the email will appear as SPAM. Be sure to check that folder for it.
Reference: