Send mail using gmail

From BubbaWiki
Jump to: navigation, search

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.


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

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


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


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

su -

Stop postfix and backup your original /etc/postfix/ file

/etc/init.d/postfix stop
cd /etc/postfix
cp /etc/postfix/ /etc/postfix/

Replace the contents of /etc/postfix/ 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 = [::ffff:]/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 =

# 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_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 = []: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 file.


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

# Contents of /etc/postfix/sasl_passwd


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

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


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

# Contents of /etc/postfix/tls_per_site         MUST


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

# Contents of /etc/postfix/generic 
# local mail user           real user to send mail as

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


Now make the created files usable to postfix

postmap generic
postmap tls_per_site
postmap transport
postmap sasl_passwd

Search the file /etc/postfix/ 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


So let's know see if mail can be send

echo "testing sending mail" | mail -s "send mail using gmail"

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[]:587
Jul 21 21:20:39 b3 postfix/smtp[18503]: Verified TLS connection established to[]:587: TLSv1 with cipher RC4-SHA (128/128 bits)
Jul 21 21:20:41 b3 postfix/smtp[18503]: E607520A8A: to=<>,[]: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


The default syslog settings create 4 log files for the mail process: mail.err, mail.warn, 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 and mail.warn. Then reload rsyslog process (/etc/init.d/rsyslog reload)