Invent

HOWTO: Configure sendmail to use gmail as SMTP mail relay


Many server based applications on linux need to send email from the localhost. Unfortunately “out-of-the-box”, linux sendmail doesn’t work as expected. Additionally, if you are hosting a server on an in-house machine and use a dynamically assigned IP address e.g. a Joomla website using dynDNS.org, but you use gmail (or google apps) to handle your email, you need to setup sendmail on your local machine so that it will relay email through gmail. This article describes the basic steps you need to carry out to get sendmail working so that applications on your localhost can send email using gmail.

To setup sendmail on redhat, centos or fedora so that mail can be dispatched using local smtp that relays through gmail (e.g. PHP mail() function for Joomla, Trac, sugarCRM etc) you need to perform the following steps.

Step 1 – Install all the packages

Use yum to install all the packages that you are going to need. If they are already installed yum will tell you so, if they are not already installed, then yum will install them for you.

 
# yum install sendmail sendmail-devel sendmail-cf bind bind-chroot

Step 2 – Setup sendmail SMTP AUTH to use gmail

sendmail client-side SMTP AUTH allows us to authenticate in order to relay all outgoing mail to our ISP’s SMTP mail server and have them send the mail on our behalf via SMART_HOST. Note that you can have SMART_HOST work just fine without SMTP AUTH if your ISP’s SMTP server doesnt require authentication.

Now edit /etc/mail/sendmail.mc to contain the following. I have removed all lines that are commented out to make this more human readable.

divert(-1)dnl include(`/usr/share/sendmail-cf/m4/cf.m4')dnl VERSIONID(`setup for linux')dnl OSTYPE(`linux')dnl define(`SMART_HOST',`smtp.gmail.com')dnl define(`RELAY_MAILER_ARGS', `TCP $h 587') define(`ESMTP_MAILER_ARGS', `TCP $h 587') define(`confDEF_USER_ID', ``8:12'')dnl define(`confTO_CONNECT', `1m')dnl define(`confTRY_NULL_MX_LIST', `True')dnl define(`confDONT_PROBE_INTERFACES', `True')dnl define(`PROCMAIL_MAILER_PATH', `/usr/bin/procmail')dnl define(`ALIAS_FILE', `/etc/aliases')dnl define(`STATUS_FILE', `/var/log/mail/statistics')dnl define(`UUCP_MAILER_MAX', `2000000')dnl define(`confUSERDB_SPEC', `/etc/mail/userdb.db')dnl define(`confPRIVACY_FLAGS', `authwarnings,novrfy,noexpn,restrictqrun')dnl define(`confAUTH_OPTIONS', `A p')dnl define(`confTO_IDENT', `0')dnl FEATURE(`authinfo',`hash -o /etc/mail/authinfo.db')dnl dnl # dnl # Rudimentary information on creating certificates for sendmail TLS: dnl # dnl # cd /etc/pki/tls/certs; dnl # make sendmail.pem dnl # define(`confAUTH_OPTIONS', `A p')dnl  define(`confCACERT_PATH', `/etc/pki/tls/certs')dnl define(`confCACERT', `/etc/pki/tls/certs/ca-bundle.crt')dnl define(`confSERVER_CERT', `/etc/pki/tls/certs/sendmail.pem')dnl define(`confSERVER_KEY', `/etc/pki/tls/certs/sendmail.pem')dnl FEATURE(`no_default_msa', `dnl')dnl FEATURE(`smrsh', `/usr/sbin/smrsh')dnl FEATURE(`mailertable', `hash -o /etc/mail/mailertable.db')dnl FEATURE(`virtusertable', `hash -o /etc/mail/virtusertable.db')dnl FEATURE(redirect)dnl FEATURE(always_add_domain)dnl FEATURE(use_cw_file)dnl FEATURE(use_ct_file)dnl FEATURE(local_procmail, `', `procmail -t -Y -a $h -d $u')dnl FEATURE(`access_db', `hash -T -o /etc/mail/access.db')dnl FEATURE(`blacklist_recipients')dnl EXPOSED_USER(`root')dnl FEATURE(`accept_unresolvable_domains')dnl FEATURE(`relay_based_on_MX')dnl LOCAL_DOMAIN(`localhost.localdomain')dnl MASQUERADE_AS(`yourdomain.com')dnl FEATURE(masquerade_envelope)dnl FEATURE(masquerade_entire_domain)dnl MASQUERADE_DOMAIN(`yourdomain.com')dnl MAILER(smtp)dnl MAILER(procmail)dnl MAILER(cyrusv2)dnl

Step 3 – Setup the authinfo file

Now edit /etc/mail/authinfo to contain the following.

# if you use google apps replace gmail.com with your own domain name for U: AuthInfo:gmail.com "U:user @ gmail.com" "P:password" "M:PLAIN" AuthInfo: "U:user @ gmail.com" "P:password" "M:PLAIN" # chmod 660 /etc/mail/authinfo # makemap hash /etc/mail/authinfo < /etc/mail/authinfo

Step 4 – Setup the access file

Now edit the /etc/mail/access file so that it contains the following.

# by default we allow relaying from localhost... Connect:localhost.localdomain RELAY Connect:localhost RELAY Connect:127.0.0.1 RELAY # chmod 660 /etc/mail/access # makemap hash /etc/mail/access < /etc/mail/access

Step 5 – Install bind to get sendmail to work

For some reason sendmail does not use /etc/hosts for local name resolution, so it always fails (and is unbearably slow on system boot as it cannot find localhost.localdomain). To solve this problem you need to run bind on your local machine.

Now edit the /etc/named.conf file so that it contains the following.

 
options { listen-on port 53 { 127.0.0.1 }; };

As we are using bind-chroot we need to copy the named.conf to where it expects t to be.

 
# cp -f /etc/named.conf /var/named/chroot/etc/

Now edit /etc/resolv.conf so that it contains the following. Change 192.168.2.50 to be the IP address of your primary DNS

 
nameserver 127.0.0.1 nameserver 192.168.2.50 domain localdomain

Step 6 – Start the required services

 
# chkconfig named on # chkconfig saslauthd on # chkconfig sendmail on # service named restart # service saslauthd restart # service sendmail restart

Test that it works

To check that sendmail is working properly, login to your server and fire up a terminal window.

 
# mailq

The mailq command will display a list of messages that are held in the outgoing mail queue. It should be empty. Now send an email, and then check the mail queue using mailq.

You can also get better diagnostic information by manually sending a message like this:

 
# sendmail -Am -t -v to: user @ domain.com from:user @ domain.com .

 

8 Responses to HOWTO: Configure sendmail to use gmail as SMTP mail relay

  1. By Fabio Almeida, May 7, 2013 at 5:19 pm

    Mítico esse post. | Mythical post.

    Thanks

  2. By sde, July 2, 2013 at 6:50 am

    thanks. this worked for me.

  3. By Ansar Ahmed, July 11, 2013 at 6:22 am

    Thanks a lot for your valuable instruction and definetly it works fine. I really appreciate your efforts,

  4. By Daniel Craig Jallits, July 16, 2013 at 2:59 pm

    MINOR EDIT to /etc/named.conf

    Line 2: listen-on port 53 { 127.0.0.1; };

  5. By Duong Dinh Tho, September 29, 2013 at 2:38 pm

    Thank you very much. This helps me alot.

  6. By unas, December 12, 2013 at 3:09 am

    Interesting guide, with this steps maybe email from my webserver doesn’t go to bulk folder, thanks

  7. By RJ, February 6, 2014 at 5:39 pm

    I dont have the authinfo.db file in the /etc/mail directory. How do I add that?

    • By mavinerc, February 6, 2014 at 5:55 pm

      Hi RJ,

      If /etc/mail/access doesn’t exist you have to create it.

      To generate the authinfo.db you then do the following at the shell prompt:

      # makemap hash /etc/mail/authinfo < /etc/mail/authinfo

Leave a Reply

Your email address will not be published. Required fields are marked *

Tweets

Contact us!

We’d Love to hear from you!

  • user_white 100 Cummings Center, Beverly, MA, 01915, USA
  • speech_white Tel. 1-800-873-7443
  • mail_white E-mail: sales@mavinerc.com

Visit also our social profiles:

Scroll to top