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 smtp.gmail.com 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 [email@example.com] -f [firstname.lastname@example.org]
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 [email@example.com] -f [firstname.lastname@example.org] -au
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 smtp.gmail.com:587 -CAfile /path/to/some/ca/file’
This switch is intermingled with the rest of the command to get everything workihttp://mxr.mozilla.org/mozilla/source/security/nss/lib/ckfw/builtins/certdata.txt?raw=1ng like a charm. Now, in my environment I had to generate the CAfile by using the following Perl Script and the following certificate data