Difference between revisions of "Gmail As Relay On Ubuntu"

From Amahi Wiki
Jump to: navigation, search
(Created page with " sudo apt-get postfix mailutils Select internet from the pop up and then OK. postconf -e 'relayhost=gmail-smtp-in.l.google.com' postconf -e 'smtp_sasl_auth_enable=yes' postcon...")
 
 
(18 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
tags: mail gmail ubuntu postfix relay exim4
  
 +
{{MessageBox|
 +
backgroundcolor = #f8d6d6|
 +
image =Warning.png|
 +
heading =WARNING|
 +
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>
 +
To receive emails meant for the root user, you have to [[Forward_System_Emails|Forward System Emails]] after finishing this tutorial.
  
sudo apt-get postfix mailutils
+
'''NOTE:'''  Recommend you do not use copy and paste as there are hidden special characters that sometimes cause issues.<br>
  
Select internet from the pop up and then OK.
+
== Option 1 (Postfix) ==
 +
sudo apt-get -y install postfix mailutils libsasl2-2 ca-certificates libsasl2-modules
  
postconf -e 'relayhost=gmail-smtp-in.l.google.com'
+
::'''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'''.
postconf -e 'smtp_sasl_auth_enable=yes'
 
postconf -e 'smtp_sasl_password_maps=hash:/etc/postfix/sasl_passwd'
 
postconf -e 'smtp_sasl_security_options='
 
  
echo "gmail-smtp-in.l.google.com  sahabcse@gmail.com:XXXXXX" > /etc/postfix/sasl_passwd
+
Add the following to the bottom of '''/etc/postfix/main.cf''':
 +
<pre>
 +
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</pre>
  
chown root:root /etc/postfix/sasl_passwd
+
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.):
chmod 600 /etc/postfix/sasl_passwd
+
[smtp.gmail.com]:587 user@gmail.com:password
  
postmap /etc/postfix/sasl_passwd
+
Create the password  database: <br>
 +
sudo postmap hash:/etc/postfix/sasl_passwd
  
/etc/init.d/postfix restart
+
Protect the files with your Gmail login data:
 +
sudo chown root:root /etc/postfix/sasl_passwd
 +
sudo chmod 600 /etc/postfix/sasl_passwd
  
echo "This is Content" | sudo mail -s "This is subject" user@home.com
+
Validating the certificate:
 +
cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem | sudo tee -a /etc/postfix/cacert.pem
  
tail -f /var/log/mail.log
+
Restart Postfix:
 +
sudo /etc/init.d/postfix restart
  
 +
== Option 2 (Exim4) ==
 +
sudo apt-get install exim4 mailx
  
REF[http://ubuntulinux.co.in/blog/ubuntu/set-up-postfix-for-relaying-emails-via-gmail-smtp-server  
+
Ensure you verify the hostname before proceeding.  You will need this later.
Set Up Postfix For Relaying Emails via gmail smtp server]
+
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):
 +
echo "This is Content" <nowiki>|</nowiki> 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:<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: