Sendmail command line to gmail

Sendmail command line to gmail

Had a requirement to send an email to myself from a script. Was surprised to find out that it is quite possible and not necessarily 100% intuitive but works.

Sendmail command line to gmail – Details

To start, make sure that you have a newer version of sendmail that supports the -H option. You will need the following information to continue:

Username – the username of an active gmail account to send from
Password – the corresponding password of the gmail account to send from
Host – this is using port 587

You will need openssl installed on the machine and preferably a recent version.

Minimalist Version ( does not work with gmail )

To send an email you can either pipe directly to the sendmail command with the following minimally.

echo -e “Subject: test\\n\\nThis is a test” | sendmail -t [] -f []

Which assumes that your to address is valid, your from address is valid, and your isp mail settings are set up correctly.

Why won’t this work for gmail?

There are several reasons why this doesn’t work for gmail and at the most basic level, we don’t have authentication and we are not using ssl. So in order to get the authentication switches – we can type sendmail with no parameters at the command line to get them. I will leave that as an exercise for the reader and state that -au and -ap are what we are looking for.

So… our new command looks like this:

echo -e “Subject: test\\n\\nThis is a test” | sendmail -t [] -f [] -au -ap

Without spaces and the angle brackest of course.

Using openssl with sendmail is pretty much the final key to getting this working properly and can be implemented with the -H command line argument.

The syntax is as follows for my configuration:

-H ‘exec openssl s_client -quiet -tls1 -starttls smtp -connect -CAfile /path/to/some/ca/file’

This switch is intermingled with the rest of the command to get everything worki like a charm. Now, in my environment I had to generate the CAfile by using the following Perl Script and the following certificate data


About ttessier

Professional Developer and Operator of SwhistleSoft
This entry was posted in Bash Scripting, Server Development. Bookmark the permalink.

Leave a Reply

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