You want to send an email message, either an autogenerated one or one entered in by an end user.
First you need to turn the parts of the email message into a
single string, representing the whole message complete with headers
and/or attachments. You can construct the string manually or use a
number of libraries, including RubyMail, TMail, and ActionMailer. Since ActionMailer is one of the dependencies of Rails, I'll
use it throughout this recipe. ActionMailer uses TMail under the covers, and it's provided by the
Here, I use ActionMailer to construct a simple, single-part email message:
require 'rubygems' require 'action_mailer' class SimpleMailer < ActionMailer::Base def simple_message(recipient) from 'firstname.lastname@example.org' recipients recipient subject 'A single-part message for you' body 'This message has a plain text body.' end end
ActionMailer then makes two new methods available for generating
this kind of email message:
SimpleMailer. create_simple_message, which returns the
email message as a data structure, and
SimpleMailer. deliver_simple_message, which actually sends
puts SimpleMailer. create_simple_message('email@example.com') # From: firstname.lastname@example.org # To: email@example.com # Subject: A single-part message for you # Content-Type: text/plain; charset=utf-8 # # This message has a plain text body.
To deliver the message, call
deliver_simple_message instead of
create_simple_message. First, though, you'll ...