Translations of this page?:

Mailers

Action Mailer

Action Mailer is a framework used to send and receive email in your Ruby on Rails application. Action Mailer uses familiar concepts - models and views - to handle email.

Creating a Mailer

To begin, create a new mailer model.

$ script/generate mailer confirmation

This generates a new model named Confirmation. Inside of this model, methods for each email can be defined, and in each method, different email attributes defined. For example, a typical email method may look like:

# /app/models/confirmation.rb
def thanks(email, name)
    recipients email
    from "no-reply@your_rails_app.com"
    subject "Thanks for signing up!"
    sent_on Time.now
    body :name => name
end

If you need to access variables in your mailer view, they should be passed to the body method with a symbol. In our example, the value of our name variable will be accessible in the view through an instance variable, @name.

Attachments are easily added to email with Action Mailer. Inside our mailer model, simply use the attachment method.

# /app/models/confirmation.rb
def thanks(email, name)
    recipients email
    from "no-reply@your_rails_app.com"
    subject "Thanks for signing up!"
    sent_on Time.now
    body :name => name
 
    attachment :content_type => "application/pdf", :body => File.read("path.to.pdf")
end
  • Be sure to replace application/pdf with the correct MIME type of file being attached.
  • There are a number of other attributes that can be configured. See the Action Mailer API docs for more information.

Along with the creation of a mailer model, a new directory is created under views. Your mailer uses views to create the content for your emails and each method in your mailer model should have a corresponding view.

# /views/confirmation/thanks.text.html.erb
Hello <%= @name %>,
Your account has been created.

Sending the email is equally as simple.

Confirmation.deliver_thanks("recpient@host", "John")

Before email will actually get sent out, Action Mailer needs to know how exactly to send it and configured for different delivery methods.

Configuring Action Mailer

Action Mailer can be configured to send mail through many methods, such as Sendmail and SMTP.

Sendmail

Configuring Action Mailer to send email with Sendmail is a simple process. Create a new file in /config/initializers called mailer.rb.

# /config/initializers/mailer.rb
ActionMailer::Base.delivery_method = :sendmail

By default, Action Mailer looks for Sendmail in /usr/sbin/sendmail. If Sendmail is not in this location, or you would like to specify any command line paramenters, they can be specified as well.

# /config/initializers/mailer.rb
ActionMailer::Base.delivery_method = :sendmail
ActionMailer::Base.sendmail_settings = {
  :location => "path/to/sendmail",
  :arguments => "-i"
}

SMTP

Like sending email with Sendmail, sending mail through SMTP is simple. Again, create a new file in /config/intializers named mailer.rb. It will contain the configuration so Action Mailer can talk to the mail server.

# /config/initializers/mailer.rb
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
  :address        => 'smtp.example.com',
  :port           => 25,
  :domain         => 'your.email.server',
  :authentication => :login,
  :user_name      => 'user',
  :password       => 'password'
}

Additional Resources

 
howtos/mailers.txt · Last modified: 2009/02/05 12:49 by ropiku
 
Recent changes RSS feed Creative Commons License