|
Rails Cookbook Authors: Orsini R Published year: 2007 Pages: 141-143/250 |
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. IntroductionContributed 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 EmailProblemContributed by: Dae San Hwang You want to configure your Rails application to send email messages. SolutionAdd 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" . DiscussionActionMailer::Base.server_settings is a hash object containing configuration parameters to connect to the SMTP server. Here's what each parameter does:
Action Mailer does not support SMTP over TLS or SSL as of version 1.2.1. See Also
|
|
Rails Cookbook Authors: Orsini R Published year: 2007 Pages: 141-143/250 |
![]() Ruby on Rails 3 Tutorial: Learn Rails by Example (Addison-Wesley Professional Ruby Series) | ![]() Advanced Rails | ![]() The Ruby Programming Language | ![]() Ruby Cookbook (Cookbooks (O'Reilly)) | ![]() Ajax on Rails |