Rails Cookbook
Authors: Orsini R
Published year: 2007
Pages: 141-143/250
Buy this book on amazon.com >>

Chapter 9. Action Mailer

Section 9.0.  Introduction

Recipe 9.1.  Configuring Rails to Send Email

Recipe 9.2.  Creating a Custom Mailer Class with the Mailer Generator

Recipe 9.3.  Formatting Email Messages Using Templates

Recipe 9.4.  Attaching Files to Email Messages

Recipe 9.5.  Sending Email from a Rails Application

Recipe 9.6.  Receiving Email with Action Mailer



9.0. Introduction

Contributed by: Dae San Hwang

Most people receive dozens, if not hundreds, of emails every day. Many of those emails are not sent by real people. They are automatically generated and sent by computer programs. For example, when you sign up for a newsletter, that newsletter is sent by software; when you place an order online, your confirmation message is generated by the shopping application; if you need to reset a password, the operation probably involves several automatically generated email messages.

A full-fledged web application framework therefore needs the ability to generate and send email messages. In Rails, the Action Mailer framework has this responsibility. To send email with Action Mailer, you first need to create a custom mailer class. This mailer class contains constructor methods for the different messages your application needs to send. The layout of your email message is handled by Action View, in a manner similar to RHTML templates. Each constructor has a corresponding Action View template that determines the content of the email message.

Once your mailer class and template files are in place, it is trivial to compose and send email. You only need to provide some String values for the email headers, and some objects for populating the Action View template.

In addition to sending email messages, a web framework needs the ability to respond to incoming mail. Action Mailer can handle incoming email. No, it does not talk to POP3 or IMAP mail servers directly. It requires external helpers to fetch email and feed the raw email text into a receive method you define. The recipes in this chapter show the three different ways to retrieve emails and forward them to the receive method of your mailer class.



Recipe 9.1. Configuring Rails to Send Email

Problem

Contributed by: Dae San Hwang

You want to configure your Rails application to send email messages.

Solution

Add the following code to config/environment.rb :

ActionMailer::Base.server_settings = {
  :address        => "mail.yourhostingcompany.com",
  :port           => 25,
  :domain         => "www.yourwebsite.com",
  :authentication => :login,
  :user_name      => "username",
  :password       => "password"
}

Replace each hash value with proper settings for your Simple Mail Transfer Protocol (SMTP) server.

You may also change the default email message format. If you prefer to send email in HTML instead of plain text format, add the following line to config/environment.rb as well:

ActionMailer::Base.default_content_type = "text/html"

Possible values for ActionMailer::Base.default_content_type are "text/plain" , "text/html" , and "text/enriched" . The default value is "text/plain" .

Discussion

ActionMailer::Base.server_settings is a hash object containing configuration parameters to connect to the SMTP server. Here's what each parameter does:



:address

Address of your SMTP server.



:port

Port number of your SMTP server. The default port number for SMTP is 25.



:domain

Domain name used to identify your server to the SMTP server. You should use the domain name for the server sending the email to reduce the chance of your email being rejected as spam.



:authentication

This may be nil , :plain , :login , or :cram_md5 . The authentication value you choose here must match the authentication method expected by your SMTP server. If your SMTP server does not require authentication, set this value to nil .



:user_name , :password

Username and password to authenticate to the SMTP server. Required when :authentication is set to :plain , :login , or :cram_md5 . If :authentication is set to nil , then these values should be set to nil as well.

Action Mailer does not support SMTP over TLS or SSL as of version 1.2.1.

See Also

  • The Action Mailer documentation, http://api.rubyonrails.org/classes/ActionMailer/Base.html

  • Wikipedia reference for SMTP, http://en.wikipedia.org/wiki/Smtp

  • Section 9.2"


Rails Cookbook
Authors: Orsini R
Published year: 2007
Pages: 141-143/250
Buy this book on amazon.com >>

Similar books on Amazon