Send mail using gmail

From BubbaWiki
Jump to navigation Jump to 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.

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