the perspicacious ramblings of droo

Setup sendmail to use Gmail’s SMTP server

Well, I did this on Fedora 12, so I’ll be basing everything off of Fedora packages and yum; however, this should work on any distro.

Things you’ll need: sendmail, sendmail-cf, cyrus-sasl

Might need something more, but if so, I’ve overlooked it…

The first thing we’re going to do is setup our authinfo. Do the following:

mkdir /etc/mail/auth/
cd /etc/mail/auth/
vim client-info

In the client-info file you’ve open in your text editor, insert the following line:

AuthInfo:smtp.gmail.com “U:root” “I:username@gmail.com” “P:password” “M:PLAIN”
AuthInfo:smtp.gmail.com:587 “U:root” “I:username@gmail.com” “P:password” “M:PLAIN”

Now, save it, quit your editor, and run the following in the same directory.

makemap -r hash client-info.db < client-info
chmod 600 *
cd ../
chmod 700 auth

Now, let’s move on to making our certs. Do the following:

mkdir /etc/mail/certs/
cd /etc/mail/certs/
openssl req -new -x509 -keyout cakey.pem -out cacert.pem -days 3650
openssl req -nodes -new -x509 -keyout sendmail.pem -out sendmail.pem -days 3650
cp  /etc/pki/tls/certs/ca-bundle.crt /etc/mail/certs

And finally, let’s edit our sendmail.mc. Do the following:

cd /etc/mail/
vim sendmail.mc

And, add the following to sendmail.mc:

FEATURE(`authinfo’,`hash /etc/mail/auth/client-info.db’)dnl
define(`RELAY_MAILER_ARGS’, `TCP $h 587′)
define(`ESMTP_MAILER_ARGS’, `TCP $h 587′)
define(`CERT_DIR’, `/etc/mail/certs’)
define(`confCACERT_PATH’, `CERT_DIR’)
define(`confCACERT’, `CERT_DIR/ca-bundle.crt’)
define(`confCRL’, `CERT_DIR/ca-bundle.crt’)
define(`confSERVER_CERT’, `CERT_DIR/sendmail.pem’)
define(`confSERVER_KEY’, `CERT_DIR/sendmail.pem’)
define(`confCLIENT_CERT’, `CERT_DIR/sendmail.pem’)
define(`confCLIENT_KEY’, `CERT_DIR/sendmail.pem’)

Now run:

m4 sendmail.mc > sendmail.cf
/etc/init.d/sendmail restart

I think it’s all self explanatory for the most part. Happy mailing :-)


In the above configuration files, a user reported having issues while copying the above information into their config files. The issue was regarding the quotes around various options. So, if you experience any trouble, please try replacing the quotes. (e.g. delete the ones that are there and add them back within your text editor)

, ,

11 thoughts on “Setup sendmail to use Gmail’s SMTP server

  • Thennarasu says:

    i do all the things u specified… is it possible to send mail from root@localhost to someone@gmail.com… i cant send using this

    • Drew Dahl says:

      No, GMail won’t allow for that with this method. All mail will appear like it came from the gmail user you’re authenticating as. My friend uses this for his domain and has gmail setup to handle his domain mail. So, he has an admin@domain.tld account on gmail that he uses that sends all outgoing mail.

      The only time you should really need to do something like this, in my opinion at least, is if your ISP is blocking port 25 outgoing. We just switched to a new ISP that doesn’t do this, so I no longer have to use the route above. In any case, if your ISP does block port 25 outgoing, they will usually give you an SMTP server of their own to use. In my case it was mail.cableone.net… If you relay all mail to the SMTP server your ISP provides, you should be able to get mail to be sent from “root@localhost”.

      So, just contact your ISP if they block port 25… If they don’t block port 25, you can setup your own mail server on your box. I will be posting an article shortly about setting up Postfix to do this.

  • Kevin says:

    Got this to work but ran into a few problems. They were copy past problems, possibly due to I used gedit instead of vim.
    The AuthInfo….. lines did not work when I copied and pasted. This was due to the quotes kept some formatting and thus caused an error in the system-log-viewer maillog (…..authinfo failed
    When running
    # m4 sendmail.mc > sendmail.cf

    m4:sendmail.mc:190: ERROR: end of file in string
    This was again due to the quotes keeping some formatting.
    The last error I got from
    # m4 sendmail.mc > sendmail.cf

    *** ERROR: FEATURE() should be before MAILER()
    This was due to I put the inserted lines at the end of the file, as error states moved to before MAILER()
    Hope this info keeps others from making the same mistakes.

    • Drew Dahl says:

      I appreciate the feedback, Kevin. I imagine the problem wasn’t with gedit, but with the quotes being copied. Sometimes there are issues with quotes being copied from web pages and pasted into config files.

      In any case, thanks for the information! I’ll make a note in the article.

  • Techlook says:

    thanks.. but in virtual free hosting if provider not provides you sendmail feature, if any way can i get sentmail() through google stmp server??

    • Drew Dahl says:

      I’d take a look at a mail library for the language you’re using. I know PHP has a commonly used mail library that will allow you to provider an alternate smtp server with authentication that should work with GMail. (Sorry for the late reply!)

  • […] NOTE: This assumes you already set up your mail server configured in this server, if not you will have to do that first, here it’s a link on how to do that with sendmail. […]

  • Chakri says:


    I am trying to use multiple gmail accounts for multiple departments for outgoing mails. With your article, i am able to send outgoing mail through only one mail account.

    Can you please suggest how to configure multiple gmail accounts ?


  • Drew Dahl says:

    Chakri, I apologize for the belated reply.

    Unfortunately, I’ve never run into that use-case and my sendmail config knowledge is very rusty, so I’m unable to offer you any good advice on that.

    One potential to look into would be to use postfix instead of sendmail. I’ve found that to be a much easier to configure option. Unfortunately, (again) my postfix config knowledge is very rusty as well.

    I hope you were able to find the answer you were looking for. If I find some free time, I’ll investigate that use-case and see if I can’t write a new post about it.

    Thanks for the comment,

  • Clay says:

    The following directory doesn’t exist on my server:

    What package/program am I missing?

Leave a Reply to Drew Dahl Cancel reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>