Difference between revisions of "Gmail As Relay On Ubuntu"

From Amahi Wiki
Jump to: navigation, search
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
tags: mail gmail ubuntu postfix relay
+
tags: mail gmail ubuntu postfix relay exim4
  
 
{{MessageBox|
 
{{MessageBox|
Line 7: Line 7:
 
message = This is recommended only for advanced users, proceed with caution.}}
 
message = 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.<br>
 
This can be used to send mails from your HDA and to receive system emails.<br>
To receive emails meant for the root user, you have to [[Forward_System_Emails|Forward System Emails]] after finishing this tutorial. Recommend you do not use copy and paste as there are hidden special characters that sometimes cause issues.<br>
+
To receive emails meant for the root user, you have to [[Forward_System_Emails|Forward System Emails]] after finishing this tutorial.  
  
Install postfix and remove sendmail: <br>
+
'''NOTE:'''  Recommend you do not use copy and paste as there are hidden special characters that sometimes cause issues.<br>
{{Code|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'''.
+
== 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''':
 
Add the following to the bottom of '''/etc/postfix/main.cf''':
{{Text|relayhost<nowiki> = </nowiki>[smtp.gmail.com]:587
+
<pre>
smtp_sasl_auth_enable<nowiki> = </nowiki>yes
+
relayhost = [smtp.gmail.com]:587
smtp_sasl_password_maps<nowiki> = </nowiki>hash:/etc/postfix/sasl_passwd
+
smtp_sasl_auth_enable = yes
smtp_sasl_security_options<nowiki> = </nowiki>noanonymous
+
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_tls_CAfile<nowiki> = </nowiki>/etc/postfix/cacert.pem
+
smtp_sasl_security_options = noanonymous
smtp_use_tls<nowiki> = </nowiki>yes}}
+
smtp_tls_CAfile = /etc/postfix/cacert.pem
 +
smtp_use_tls = yes</pre>
  
 
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.):
 
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.):
{{Text|Text=[smtp.gmail.com]:587 user@gmail.com:password}}
+
[smtp.gmail.com]:587 user@gmail.com:password
  
 
Create the password  database: <br>
 
Create the password  database: <br>
{{Code|sudo postmap hash:/etc/postfix/sasl_passwd}}
+
sudo postmap hash:/etc/postfix/sasl_passwd
  
 
Protect the files with your Gmail login data:
 
Protect the files with your Gmail login data:
{{Code|sudo chown root:root /etc/postfix/sasl_passwd
+
sudo chown root:root /etc/postfix/sasl_passwd
sudo chmod 600 /etc/postfix/sasl_passwd}}
+
sudo chmod 600 /etc/postfix/sasl_passwd
  
 
Validating the certificate:
 
Validating the certificate:
{{Code|cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem <nowiki>|</nowiki> sudo tee -a /etc/postfix/cacert.pem}}
+
cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem | sudo tee -a /etc/postfix/cacert.pem
  
 
Restart Postfix:
 
Restart Postfix:
{{Code|sudo /etc/init.d/postfix restart}}
+
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: <code>mail sent by smarthost; received via SMTP or fetchmail</code>
 +
* The next question asks for the system mail name: <code>Set to same as hostname</code>
 +
* Now it asks you what IP addresses should be allowed to use the server. <code>Leave as is (127.0.0.1 ; ::1)</code>
 +
* Other destinations for which mail is accepted: <code>Set to same as hostname</code>
 +
* Machines to relay mail for: <code>Leave blank</code>
 +
* IP address or host name of outgoing smarthost: <code>smtp.gmail.com::587</code>
 +
* Hide local mail name in outgoing mail: <code>No</code>
 +
* Keep number of DNS-queries minimal: <code>No</code>
 +
* Delivery method for local mail: <code>mbox format in /var/mail/</code>
 +
* Split configuration into small files: <code>No</code>
 +
<br />
 +
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.
 +
<pre>gmail-smtp.l.google.com:YOU@gmail.com:PASSWORD
 +
*.google.com:YOU@gmail.com:PASSWORD
 +
smtp.gmail.com:YOU@gmail.com:PASSWORD</pre>
 +
 
 +
::'''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.
 +
:<code>$ ls -al /etc/exim4/passwd.client</code>
 +
:<code>-rw-r----- 1 root Debian-exim 252 Mar 25 17:24 /etc/exim4/passwd.client</code>
 +
 
 +
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 <code>-qff</code> parameter forces a delivery attempt for every message, whether frozen or not.
  
 +
== Test Email ==
 
Now try sending a mail, it should reach your Gmail account (change user to match your account name):
 
Now try sending a mail, it should reach your Gmail account (change user to match your account name):
{{Code|echo "This is Content" <nowiki>|</nowiki> sudo mail -s "This is subject" user@gmail.com}}
+
echo "This is Content" <nowiki>|</nowiki> sudo mail -s "This is subject" user@gmail.com
  
 
Check the mail log:
 
Check the mail log:
{{Code|tail -f /var/log/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.
 
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: [http://yaui.me/postfix-gmail-smtp-server-relay-ubuntu Postfix + Gmail SMTP server relay in Ubuntu]
+
Reference:<br />
 +
* [http://yaui.me/postfix-gmail-smtp-server-relay-ubuntu Postfix + Gmail SMTP server relay in Ubuntu]<br />
 +
* [http://linuxcommando.blogspot.com/2014/04/how-to-setup-exim4-on-debian-to-use.html How to setup exim4 on Debian to use Gmail]<br />
 +
* [http://www.sbprojects.com/projects/raspberrypi/exim4.php Prepare Your Pi To Send Mail Through Gmail]

Latest revision as of 02:53, 5 December 2014

tags: mail gmail ubuntu postfix relay exim4

Warning.png 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.

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: