Every Rubyist will tell you that the basic way to send email with Ruby is to use the Net::SMTP class. Also, he or she will add that you shouldn’t go this way. Why? Because there are a few notable gems that let you handle email activities simply and effectively. And today we’ll check out the most popular ones including Action Mailer, Ruby Mail, and Pony.
ActionMailer
This is the most popular gem if you need to give effect to email delivery on a Ruby on Rails app. It lets you send emails using mailer classes and views. Here’s how a simple email built with ActionMailer may look:
class TestMailer < ActionMailer::Base default from: 'test@sender.com' def simple_message(recipient) mail( to: test@recipient.com, subject: 'Subject of the message', body: 'Body text of the message' ) end end
How to create and send an email with ActionMailer?
It’s unlikely that you’ll be OK with a simple message. In most cases, your Ruby app will send transactional emails and they need to be refined. So, check out the following guide on how to create an advanced message and send it using the SMTP server.
-
Start with creating a mailer model because your app will use it to send emails:
Also, you need helpers to generate an electronic message. They are defined in the mailer model. You can set up variables in the mailer views, options like :to address, and so on. Here are some helpers you can make use of:
attachments['filename.png'] = File.read('path/to/attachment.pdf')
– to add attachmentsattachments.inline['filename.png'] = File.read('path/to/attachment.pdf')
– to add an inline attachmentheaders['X-No-Spam'] = 'True'
– to specify a header fieldheaders({'X-No-Spam' => 'True', 'In-Reply-To' => '1234@message.id'})
– to specify multiple headersmail
– to specify the email to be sent
Here is what we’ve got:
In this mailer class, we’ve got HTML text, as well as an attached PNG file. Now, we can create a corresponding view.
Ruby Mail
Now, let’s check out how to manage all email-related activities using Ruby Mail. And we’ll start with a code example of a simple email:
mail = Mail.new do from 'test@sender.com' to 'test@recipient.com' subject 'Subject of the message' body 'Body text' end
SMTP is a default mail delivery method with a local host port 25. But you can customize the SMTP settings:
Mail.defaults do delivery_method :smtp, address: "localhost", port: 1025 end
and change the delivery method:
mail.delivery_method :sendmail mail.deliver
How to send an HTML email with attachment in Ruby Mail?
We already know how to send multi-part emails with ActionMailer. Now, let’s see how to do this in Ruby Mail:
mail = Mail.new do from 'test@sender.com' to 'test@recipient.com' subject 'Subject of the message' text_part do body 'Body text' end html_part do content_type 'text/html; charset=UTF-8' body '<h2>HTML text</h2>' end add_file '/path/to/file.png' end
Quite simple, isn’t it? And there is more to come. The next Ruby gem lets you send an email in one command.
Pony
It’s not a fairy tale and you really can use one command to send messages. And here is the example:
Pony.mail(:to => 'test@recipient.com', :from => 'test@sender.com', :subject => 'Subject of the message', :body => 'Body text'
If necessary, the script can be cut to having only :to option key. Also, you can specify a delivery method:
Pony.mail(:to => 'test@recipient.com', :via => :smtp)
or
Pony.mail(:to => 'test@recipient.com', :via => :sendmail)
And that’s how the SMTP server configuration looks like. We’ll take Gmail SMTP as an example:
Pony.mail(:to => 'test@recipient.com', :via => :smtp, :via_options => { :address => 'smtp.gmail.com', :port => '587', :user_name => 'user', :password => 'pass', :authentication => :plain, :domain => "example.com" })
Authorization options include Plain, Login, and Cram_MD5. By default, no authorization is set.
Unfortunately, Pony won’t let you send a message containing the HTML text or attached with a file.
Wrapping Up
The major part of Rubyists opts for ActionMailer. But it doesn’t mean that you should do either. Pick an option that fits best for your project needs. For some reason, even the simplest way provided by Pony gem can be the perfect one. Good luck!