Difference between revisions of "Send mail using gmail"

From BubbaWiki
Jump to navigation Jump to search
m (typo in file name compared to previous step)
 
(6 intermediate revisions by one other user not shown)
Line 3: Line 3:
This how-to is not limited to gmail only, but also relevant for any other (TLS) mail service.<br>  
This how-to is not limited to gmail only, but also relevant for any other (TLS) mail service.<br>  


Below is the configuration I will be using during the how-to, please replace it with your own. <br>  
=== Configuration  ===
<pre>smtp server&nbsp;         : smtp.gmail.com
 
TLS port             : 587
Below are listed the parameters used in this how-to, please replace them with your own. <br>  
username&nbsp;             : eek
<pre>smtp server&nbsp;         &nbsp;: smtp.gmail.com
password&nbsp;             : thecat
TLS port             &nbsp;: 587
username&nbsp;           &nbsp;: eek
password&nbsp;           &nbsp;: thecat
mailuser at gmail.com&nbsp;: eek@gmail.com
mailuser at gmail.com&nbsp;: eek@gmail.com
(mail)user on bubba&nbsp; : eek@b3.localdomain
(mail)user on bubba&nbsp; &nbsp;: eek@b3.localdomain
</pre>  
</pre>  
=== Packages<br>  ===
=== Packages<br>  ===


The B3 comes with all necessary packages pre-installed.<br>  
No packages need to be installed as the B3 comes with all necessary packages pre-installed.<br>  


=== Setup  ===
=== Setup  ===


Login to the b3 using ssh and become the root user.  
Login to the B3 using ssh and become the root user.  
<pre>su -
<pre>su -
</pre>  
</pre>  
Line 24: Line 26:
cd /etc/postfix
cd /etc/postfix
cp /etc/postfix/main.cf /etc/postfix/main.cf.orig
cp /etc/postfix/main.cf /etc/postfix/main.cf.orig
</pre>
</pre>  
==== main.cf<br> ====
==== main.cf<br> ====


Replace the contents of '''/etc/postfix/main.cf''' with the following contents<br>
Replace the contents of '''/etc/postfix/main.cf''' with the following contents<br>  
<pre>smtpd_banner = $myhostname ESMTP $mail_name (Debian/GNU)
<pre>smtpd_banner = $myhostname ESMTP $mail_name (Debian/GNU)
biff = no
biff = no
Line 64: Line 66:
  reject_rbl_client bl.spamcop.net
  reject_rbl_client bl.spamcop.net
  permit
  permit
# TLS parameters
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
#smtpd_use_tls=yes
smtpd_tls_session_cache_database = btree:${queue_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${queue_directory}/smtp_scache


# See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for
# See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for
Line 89: Line 84:
#
#
smtp_tls_CApath = /etc/ssl/certs
smtp_tls_CApath = /etc/ssl/certs
#smtp_tls_CAfile = /etc/postfix/cacert.pem
smtp_tls_session_cache_database = btree:${queue_directory}/smtp_scache
#smtp_tls_cert_file = /etc/postfix/FOO-cert.pem
#smtp_tls_key_file = /etc/postfix/FOO-key.pem
#smtp_tls_session_cache_database = btree:/var/run/smtp_tls_session_cache
smtp_use_tls = yes
smtp_use_tls = yes
smtpd_tls_CAfile = /etc/postfix/cacert.pem
smtpd_tls_CApath = /etc/ssl/certs
smtpd_tls_cert_file = /etc/postfix/FOO-cert.pem
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file = /etc/postfix/FOO-key.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_tls_received_header = yes
smtpd_tls_received_header = yes
#smtpd_tls_session_cache_database = btree:/var/run/smtpd_tls_session_cache
smtpd_tls_session_cache_database = btree:${queue_directory}/smtpd_scache
smtpd_use_tls = yes
smtpd_use_tls = yes
tls_random_source = dev:/dev/urandom
tls_random_source = dev:/dev/urandom
Line 110: Line 102:
smtpd_sasl_local_domain = $myhostname
smtpd_sasl_local_domain = $myhostname
smtp_sasl_security_options = noanonymous
smtp_sasl_security_options = noanonymous
#smtp_sasl_security_options =
smtp_sasl_tls_security_options = noanonymous
smtp_sasl_tls_security_options = noanonymous
smtpd_sasl_application_name = smtpd
smtpd_sasl_application_name = smtpd


## Gmail Relay
## Gmail Relay
relayhost = '''[smtp.gmail.com]:587'''
relayhost = [smtp.gmail.com]:587
   
   
## Good for Testing
## Good for Testing
Line 122: Line 113:
# Disable DNS Lookups
# Disable DNS Lookups
disable_dns_lookups = yes
disable_dns_lookups = yes
#
 
# Great New feature Address Mapping  
# Great New feature Address Mapping  
#  for example may mchirico@localhost to mchirico@gmail.com
smtp_generic_maps = hash:/etc/postfix/generic
smtp_generic_maps = hash:/etc/postfix/generic
#
#
transport_maps = hash:/etc/postfix/transport
transport_maps = hash:/etc/postfix/transport
</pre>
Now setup the necessary files mentioned in the main.cf file.<br>


</pre>
==== sasl_password<br> ====
Now setup the necessary files for password and email sender.<br>


==== sasl_password<br> ====
Create a file '''/etc/postfix/sasl_passwd''' with the following contents  
 
Create a file'''/etc/postfix/sasl_passwd''' with the following contents
<pre># Contents of /etc/postfix/sasl_passwd
<pre># Contents of /etc/postfix/sasl_passwd
[smtp.gmail.com]:587      eek@gmail.com:thecat
[smtp.gmail.com]:587      eek@gmail.com:thecat
</pre>
</pre>  
==== transport<br> ====
==== transport<br> ====


Create a file '''/etc/postfix/transport''' with the following contents<br>
Create a file '''/etc/postfix/transport''' with the following contents<br>  
<pre># Contents of /etc/postfix/transport
<pre># Contents of /etc/postfix/transport
# This sends mail to Gmail
# This sends mail to Gmail
Line 148: Line 135:
#  tape.squeezel.com        relay:[tape.squeezel.com]
#  tape.squeezel.com        relay:[tape.squeezel.com]
#  closet.squeezel.com      relay:[closet.squeezel.com]
#  closet.squeezel.com      relay:[closet.squeezel.com]
</pre>
</pre>  
==== tls_per_site<br> ====
==== tls_per_site<br> ====


Create a file '''/etc/postfix/tls_per_site''' with the following contents<br>
Create a file '''/etc/postfix/tls_per_site''' with the following contents<br>  
<pre># Contents of /etc/postfix/tls_per_site
<pre># Contents of /etc/postfix/tls_per_site
smtp.gmail.com        MUST
smtp.gmail.com        MUST
</pre>
</pre>  
==== generic<br> ====
==== generic<br> ====


Create a file /etc/postfix/generic with the following contents<br>
Create a file '''/etc/postfix/generic''' with the following contents<br>  
<pre># Contents of /etc/postfix/generic  
<pre># Contents of /etc/postfix/generic  
# local mail user          real user to send mail as
# local mail user          real user to send mail as
root@b3.localdomain        eek@gmail.com
root@b3.localdomain        eek@gmail.com
eek@b3.localdomain          eek@gmail.com
eek@b3.localdomain          eek@gmail.com
</pre>
</pre>  
Note: Every local b3 user that needs to send mail, needs to be listed in the generic file. Here postfix replaces the mail sender with the one given in the generic file. Otherwise gmail smtp server will drop the mail as it's send using an unknown email sender. In the example above, mail send as user eek on the b3 is send using the gmail smtp server as eek@gmail.com. <br>
Note: Every local B3 user that needs to send mail, needs to be listed in the generic file. Here postfix replaces the mail sender with the one given in the generic file. Otherwise gmail smtp server will drop the mail as it's send using an unknown email sender. In the example above, mail send as user eek on the B3 is send using the gmail smtp server as eek@gmail.com. <br>  


=== postmap<br> ===
==== postmap<br> ====


Now make the created files usable to postfix<br>
Now make the created files usable to postfix<br>  
<pre>postmap generic
<pre>postmap generic
postmap tls_per_site
postmap tls_per_site
postmap transport
postmap transport
postmap sasl_password
postmap sasl_passwd
</pre>
</pre>
==== master.cf<br> ====


Search the file '''/etc/postfix/master.cf''' for the following text<br>
==== master.cf<br>  ====
 
Search the file '''/etc/postfix/master.cf''' for the following text<br>  
<pre>smtp      unix  -      -      -      -      -      smtp
<pre>smtp      unix  -      -      -      -      -      smtp
# When relaying mail as backup MX, disable fallback_relay to avoid MX loops
# When relaying mail as backup MX, disable fallback_relay to avoid MX loops
Line 181: Line 169:
         -o smtp_fallback_relay=
         -o smtp_fallback_relay=
#      -o smtp_helo_timeout=5 -o smtp_connect_timeout=5
#      -o smtp_helo_timeout=5 -o smtp_connect_timeout=5
</pre>
</pre>  
and replace this text with the following contents<br>
and replace this text with the following contents<br>  
<pre>smtp      unix  -      -      n      -      -      smtp
<pre>smtp      unix  -      -      n      -      -      smtp
# When relaying mail as backup MX, disable fallback_relay to avoid MX loops
# When relaying mail as backup MX, disable fallback_relay to avoid MX loops
Line 189: Line 177:
#      -o smtp_fallback_relay=
#      -o smtp_fallback_relay=
#      -o smtp_helo_timeout=5 -o smtp_connect_timeout=
#      -o smtp_helo_timeout=5 -o smtp_connect_timeout=
</pre>
</pre>  
=== Start postfix<br> ===
=== Start postfix<br> ===


Everything is done, so let's start postfix.<br>
Everything is done, so let's start postfix.<br>  
<pre>/etc/init.d/postfix start
<pre>/etc/init.d/postfix start
</pre>
</pre>  
=== Test<br> ===
=== Test<br> ===


So let's know see if mail can be send<br>
So let's know see if mail can be send<br>  
<pre>echo "testing sending mail" | mail -s "send mail using gmail" eek@gmail.com
<pre>echo "testing sending mail" | mail -s "send mail using gmail" eek@gmail.com
</pre>
</pre>  
You have mail!<br>
You have mail!<br>  


In the '''/var/log/mail.log''' there should be something like<br>
In the '''/var/log/mail.log''' there should be something like<br>  
<pre>Jul 21 21:20:38 b3 postfix/pickup[18136]: E607520A8A: uid=0 from=&lt;root&gt;
<pre>Jul 21 21:20:38 b3 postfix/pickup[18136]: E607520A8A: uid=0 from=&lt;root&gt;
Jul 21 21:20:38 b3 postfix/cleanup[18496]: E607520A8A: message-id=&lt;20110721192038.E607520A8A@b3.localdomain&gt;
Jul 21 21:20:38 b3 postfix/cleanup[18496]: E607520A8A: message-id=&lt;20110721192038.E607520A8A@b3.localdomain&gt;
Line 210: Line 198:
Jul 21 21:20:41 b3 postfix/smtp[18503]: E607520A8A: to=&lt;eek@gmail.com&gt;, relay=smtp.gmail.com[74.125.79.109]:587, delay=2.6, delays=0.01/0.06/0.59/1.9, dsn=2.0.0, status=sent (250 2.0.0 OK 1311276041 a48sm1289411eeg.60)
Jul 21 21:20:41 b3 postfix/smtp[18503]: E607520A8A: to=&lt;eek@gmail.com&gt;, relay=smtp.gmail.com[74.125.79.109]:587, delay=2.6, delays=0.01/0.06/0.59/1.9, dsn=2.0.0, status=sent (250 2.0.0 OK 1311276041 a48sm1289411eeg.60)
Jul 21 21:20:41 b3 postfix/qmgr[7782]: E607520A8A: removed
Jul 21 21:20:41 b3 postfix/qmgr[7782]: E607520A8A: removed
</pre>
</pre>  
<br>
=== Logging<br>  ===
 
The default syslog settings create 4 log files for the mail process: mail.err, mail.warn, mail.info and mail.log. <br>
 
You can limit the number of logs being generated by editing the '''/etc/rsyslog.conf''' and comment out the lines beginning with mail.info and mail.warn. Then reload rsyslog process (/etc/init.d/rsyslog reload)<br>
 
=== References<br>  ===
 
#http://souptonuts.sourceforge.net/postfix_tutorial.html
#http://www.postfix.org
#http://www.postfix.org/STANDARD_CONFIGURATION_README.html#fantasy<br>


<br>
<br>

Latest revision as of 14:54, 25 August 2012

This how-to describes what steps need to be taken in order to send mail using the gmail service.

This how-to is not limited to gmail only, but also relevant for any other (TLS) mail service.

Configuration

Below are listed the parameters used in this how-to, please replace them with your own.

smtp server           : smtp.gmail.com
TLS port              : 587
username              : eek
password              : thecat
mailuser at gmail.com : eek@gmail.com
(mail)user on bubba   : eek@b3.localdomain

Packages

No packages need to be installed as the B3 comes with all necessary packages pre-installed.

Setup

Login to the B3 using ssh and become the root user.

su -

Stop postfix and backup your original /etc/postfix/main.cf file

/etc/init.d/postfix stop
cd /etc/postfix
cp /etc/postfix/main.cf /etc/postfix/main.cf.orig

main.cf

Replace the contents of /etc/postfix/main.cf with the following contents

smtpd_banner = $myhostname ESMTP $mail_name (Debian/GNU)
biff = no
# The command_directory parameter specifies the location of all
# postXXX commands.
#
command_directory = /usr/sbin
daemon_directory = /usr/lib/postfix

mydestination = b3.localdomain, localhost.localdomain, localhost, /etc/postfix/bubbadomains

unknown_local_recipient_reject_code = 550
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128

alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases

recipient_delimiter = +

home_mailbox = Mail/
mailbox_size_limit = 0

disable_vrfy_command = yes
smtpd_helo_required = yes
smtpd_recipient_restrictions =
 permit_mynetworks
 reject_unauth_destination
 reject_unauth_pipelining
 reject_invalid_hostname
 reject_non_fqdn_sender
 reject_unknown_sender_domain
 reject_non_fqdn_recipient
 reject_unknown_recipient_domain
 reject_rbl_client dnsbl.njabl.org
 reject_rbl_client dnsbl.sorbs.net
 reject_rbl_client bl.spamcop.net
 permit

# See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for
# information on enabling SSL in the smtp client.

myhostname = b3.localdomain
myorigin = /etc/mailname
inet_interfaces = all

## TLS Settings
#
# For no logs set = 0
smtp_tls_loglevel = 1
# 
# smtp_enforce_tls = yes
# Above is commented because doing it site by site below
smtp_tls_per_site = hash:/etc/postfix/tls_per_site
#
smtp_tls_CApath = /etc/ssl/certs
smtp_tls_session_cache_database = btree:${queue_directory}/smtp_scache
smtp_use_tls = yes
smtpd_tls_CApath = /etc/ssl/certs
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_tls_received_header = yes
smtpd_tls_session_cache_database = btree:${queue_directory}/smtpd_scache
smtpd_use_tls = yes
tls_random_source = dev:/dev/urandom

##  SASL Settings
# This is going in to THIS server
smtpd_sasl_auth_enable = no
# We need this
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtpd_sasl_local_domain = $myhostname
smtp_sasl_security_options = noanonymous
smtp_sasl_tls_security_options = noanonymous
smtpd_sasl_application_name = smtpd

## Gmail Relay
relayhost = [smtp.gmail.com]:587
 
## Good for Testing
# sender_bcc_maps = hash:/etc/postfix/bcc_table

# Disable DNS Lookups
disable_dns_lookups = yes

# Great New feature Address Mapping 
smtp_generic_maps = hash:/etc/postfix/generic
transport_maps = hash:/etc/postfix/transport

Now setup the necessary files mentioned in the main.cf file.

sasl_password

Create a file /etc/postfix/sasl_passwd with the following contents

# Contents of /etc/postfix/sasl_passwd
[smtp.gmail.com]:587       eek@gmail.com:thecat

transport

Create a file /etc/postfix/transport with the following contents

# Contents of /etc/postfix/transport
# This sends mail to Gmail
gmail.com               smtp:[smtp.gmail.com]:587
# Except mail going to the tape and closet server
#  tape.squeezel.com        relay:[tape.squeezel.com]
#  closet.squeezel.com      relay:[closet.squeezel.com]

tls_per_site

Create a file /etc/postfix/tls_per_site with the following contents

# Contents of /etc/postfix/tls_per_site
smtp.gmail.com         MUST

generic

Create a file /etc/postfix/generic with the following contents

# Contents of /etc/postfix/generic 
# local mail user           real user to send mail as
root@b3.localdomain         eek@gmail.com
eek@b3.localdomain          eek@gmail.com

Note: Every local B3 user that needs to send mail, needs to be listed in the generic file. Here postfix replaces the mail sender with the one given in the generic file. Otherwise gmail smtp server will drop the mail as it's send using an unknown email sender. In the example above, mail send as user eek on the B3 is send using the gmail smtp server as eek@gmail.com.

postmap

Now make the created files usable to postfix

postmap generic
postmap tls_per_site
postmap transport
postmap sasl_passwd

master.cf

Search the file /etc/postfix/master.cf for the following text

smtp      unix  -       -       -       -       -       smtp
# When relaying mail as backup MX, disable fallback_relay to avoid MX loops
relay     unix  -       -       -       -       -       smtp
        -o smtp_fallback_relay=
#       -o smtp_helo_timeout=5 -o smtp_connect_timeout=5

and replace this text with the following contents

smtp      unix  -       -       n       -       -       smtp
# When relaying mail as backup MX, disable fallback_relay to avoid MX loops
relay     unix  -       -       n       -       -       smtp
        -o smtp_generic_maps=
#       -o smtp_fallback_relay=
#       -o smtp_helo_timeout=5 -o smtp_connect_timeout=

Start postfix

Everything is done, so let's start postfix.

/etc/init.d/postfix start

Test

So let's know see if mail can be send

echo "testing sending mail" | mail -s "send mail using gmail" eek@gmail.com

You have mail!

In the /var/log/mail.log there should be something like

Jul 21 21:20:38 b3 postfix/pickup[18136]: E607520A8A: uid=0 from=<root>
Jul 21 21:20:38 b3 postfix/cleanup[18496]: E607520A8A: message-id=<20110721192038.E607520A8A@b3.localdomain>
Jul 21 21:20:38 b3 postfix/qmgr[7782]: E607520A8A: from=<root@b3.localdomain>, size=370, nrcpt=1 (queue active)
Jul 21 21:20:39 b3 postfix/smtp[18503]: setting up TLS connection to smtp.gmail.com[74.125.79.109]:587
Jul 21 21:20:39 b3 postfix/smtp[18503]: Verified TLS connection established to smtp.gmail.com[74.125.79.109]:587: TLSv1 with cipher RC4-SHA (128/128 bits)
Jul 21 21:20:41 b3 postfix/smtp[18503]: E607520A8A: to=<eek@gmail.com>, relay=smtp.gmail.com[74.125.79.109]:587, delay=2.6, delays=0.01/0.06/0.59/1.9, dsn=2.0.0, status=sent (250 2.0.0 OK 1311276041 a48sm1289411eeg.60)
Jul 21 21:20:41 b3 postfix/qmgr[7782]: E607520A8A: removed

Logging

The default syslog settings create 4 log files for the mail process: mail.err, mail.warn, mail.info and mail.log.

You can limit the number of logs being generated by editing the /etc/rsyslog.conf and comment out the lines beginning with mail.info and mail.warn. Then reload rsyslog process (/etc/init.d/rsyslog reload)

References

  1. http://souptonuts.sourceforge.net/postfix_tutorial.html
  2. http://www.postfix.org
  3. http://www.postfix.org/STANDARD_CONFIGURATION_README.html#fantasy