Sending emails using Google Mail with Ruby
It's no secret that Google Mail has become, over the last years, the most widely used email server and client on the world. Not only it's basically free, but with the use of Google Apps you can even use it on your own domains.
Because so many people use it, even system administrators, it may be good to know how to use it to send system emails. Also, because Ruby is actually the only scripting language I feel comfortable with, I'll show you a simple library to send emails using Google SMTP server as an outgoing server, so you don't have to configure your server with sendmail, postfix or another typical UNIX mail server.
The first thing we will need is to include (and install) two gems:
- net/smtp (actually this comes from the Standard Library on Ruby 1.9)
- tlsmail
With those two libraries, we can already send a simple email, using the standard SMTP format:
You can see here that the SMTP info is stored in a variable @smtp_info
. We will take care of that later. Also, the variable mailtext
passed to the method also needs a special format. More on that later as well. The really important fragment of code here is the one that calls enable_tls
on the Net::SMTP
module. This method is provided by the tlsmail
gem and will allow our SMTP connection to use TLS as the authentication method. The other part of the code is pretty straightforward: we simply call the start method with a block, in which we actually send the email with send_message. Note that we have to provide the start
method with the SMTP info of our Google Mail server account. This includes the server, which will be smtp.gmail.com
, the port, which is 587
, the HELO, which is gmail.com
if using a standard account or your domain FQDN if using your own domain, and finally your username and password. For the authentication parameter we have to provide :plain
(TLS will be used on top of that).
Now let's see how the mailtext
string is built. In this case I'll be using a plain text format with two different variants: a simple text email, or an email with an attachment.
To send a simple text email, we have to follow the SMTP standard. I took the info from this tutorialspoint post. Here's the pattern we have to follow to build a compliant string:
Note the importance of the indenting here, as the from/to/subject lines must start at the first text column. With this simple method, you can then simply call the method we programmed before with the resulting string as a parameter and the email will be sent. Pretty easy.
Sending an attachment is a bit more complicated. As SMTP email is send in plain text, attachments are encoded in base64 strings, and are added to the message string in a special way. Here's how to do it in Ruby:
As you can see, in the first place the file is read and converted to a base64 string. After that, the message is generated. SMTP uses a special unique marker to delimit the attachment from the rest of the text. In here we use the line (0...50).map{ ('a'..'z').to_a[rand(26)] }.join
(extracted from StackOverflow) to generate a 50 char length random string. Although it's very unlikely to happen, we should check that this random string does not appear anywhere else in the message body or the base64 converted attached file before using it as a delimiter.
After that, the rest of the message is built, specifying it has an attachment and its delimiter in the following lines:
The file is actually attached some lines below. After that, we can pass this new string to the method that sends the email, and all done.
Now, because our SMTP info can be sensitive (it contains our username and our password), it might not be a good idea to just hardcode this info in the email sending script. That's why I've used a yaml serialized hash to store this info, so we can load it at any time. Doing this is really easy with the yaml
gem:
An example file would look like this:
Now that we have all the parts programmed, we should only pack it a little so it can be of use as a library. The following code contains a simple script with a class to send the emails and a little program that reads parameters from the command line:
And that's all! You can use the script as a standalone command to send an email with some command line arguments or just require it in your ruby script and use the class to send the messages.