-
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 variablemailtext
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 callsenable_tls
on theNet::SMTP
module. This method is provided by thetlsmail
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 thestart
method with the SMTP info of our Google Mail server account. This includes the server, which will besmtp.gmail.com
, the port, which is587
, the HELO, which isgmail.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.
-
Ruby on Rails, Varnish and user dependent content
Ruby on Rails performance is a topic that has been widely discussed. Whichever the conclusion you want to make about all the resources out there, the chances you'll be having to use a cache server in front of your application servers are pretty high. Varnish is a nice option when having to deal with this architecture: it has lots of options and flexibility, and its performance is really good, too.
However, adding a cache server in front of your application can lead to problems when the page you are serving has user dependent content. Let's see what can we do to solve this problem.
-
A Ruby implementation of the FizzBuzz test using the Enumerator class
Some days ago I learnt about The FizzBuzz Test and did a simple implementation in Ruby. The FizzBuzz test is a simple algorithm that is supposed to do the following:
For each number from 1 to 100:
- If the number is divisible by 3, print "Fizz"
- If the number is divisible by 5, print "Buzz"
- If the number is divisible by both 3 and 5, print "FizzBuzz"
- Otherwise print the number
I was just reading about how you can use the Enumerator class to have generators in the Programming Ruby 1.9 book, and thought that a good implementation could be done using just an Enumerator, so here it is, along with a simple RSpect test:
You can also find the code in its GitHub repository: https://github.com/brafales/ruby-fizzbuzz.
-
Push git tags to a remote repository
If you ever wondered how to push the tags you set locally to a remote repository in git, you can do it with the following simple command:
-
Give your Ruby console a dash of colour
When you're developing an application in Rails (or Ruby), you spend lots of time in the IRB, the Interactive Ruby Shell. Usually just to test some Ruby code, start an application console or debug something going on inside the project. Yesterday, looking at a coworker screen, I saw he had his console with lots of color hints, and I thought it was pretty nice. I asked him about that and he told me he was using a special gem for that.
The gem is called wirble. It has some nice defaults and allows you to configure the colors as you wish. To use it in your consoles, just add this lines to your
~/.irbrc
file:Of course, you'll need to install the gem. Just issue a
gem install wirble
and you'll be good to go!