Ruby on Rails 2.1 - 发送电子邮件

ActionMailer 是Rails组件,可让应用程序发送和接收电子邮件.在本章中,我们将了解如何使用Rails发送电子邮件.

让我们先使用以下命令创建电子邮件项目.

C:\ruby> rails -d mysql emails

这里我们使用 -d mysql 选项来指定我们对使用MySQL数据库的兴趣.我们可以使用 -d 选项指定任何其他数据库名称,如 oracle postgress .默认情况下,Rails使用 SQLite 数据库.

设置数据库

即使我们没有使用数据库我们的应用程序,但Rails需要它继续.所以让我们执行这些额外的步骤.

以下是创建数据库的方法 :

mysql> create database emails;
Query OK, 1 row affected (0.01 sec)

mysql> grant all privileges on emails.*
 to 'root'@'localhost' identified by 'password';
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

要指示Rails找到数据库,请编辑配置文件〜\upload \ config \database.yml并将数据库名称更改为cookbook.当你完成时,它应该看起来如下 :

development:
   adapter: mysql
   encoding: utf8
   database: emails
   username: root
   password: password
   host: localhost
test:
   adapter: mysql
   encoding: utf8
   database: emails
   username: root
   password: password
   host: localhost
production:
   adapter: mysql
   encoding: utf8
   database: emails
   username: root
   password: password
   host: localhost

动作邮件程序 - 配置

以下是在继续实际工作之前,您必须遵循以完成配置的步骤.  :

转到电子邮件项目的config文件夹并打开 environment.rb 文件,并在此文件的底部添加以下行.

 
 ActionMailer :: Base.delivery_method =:smtp

它通知ActionMailer您要使用SMTP服务器.如果您使用的是基于Unix的操作系统(如Mac OS X或Linux),也可以将其设置为:sendmail.

在环境底部添加以下代码行. rb以及.

ActionMailer::Base.smtp_settings = {
   :address => "smtp.it1352.com",
   :port => 25,
   :domain => "it1352.com",
   :authentication => :login,
   :user_name => "username",
   :password => "password",
}

用适当的简单邮件传输协议(SMTP)服务器设置替换每个哈希值.如果您还不知道,可以从Internet服务提供商处获取此信息.如果使用标准SMTP服务器,则无需更改端口号25和身份验证类型.

您还可以更改默认电子邮件格式.如果您希望以HTML而不是纯文本格式发送电子邮件,请将以下行添加到config/environment.rb以及减去;

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

ActionMailer :: Base.default_content_type可以设置为"text/plain","text/html" ,和"文字/丰富".默认值为"text/plain".

下一步是创建邮件程序.

生成邮件程序

使用以下命令生成邮件如下 :

C:\ruby\> cd emails
C:\ruby\emails> ruby script/generate mailer Emailer

它将在app/models目录中创建一个文件emailer.rb.检查此文件的内容如下 :

class Emailer < ActionMailer::Base
end

现在让我们在ActionMailer :: Base类中创建一个方法,如下所示 :

class Emailer < ActionMailer::Base
   def contact(recipient, subject, message, sent_at = Time.now)
      @subject = subject
      @recipients = recipient
      @from = 'no-reply@yourdomain.com'
      @sent_on = sent_at
      @body["title"] = 'This is title'
      @body["email"] = 'sender@yourdomain.com'
      @body["message"] = message
      @headers = {}
   end
end

contact方法有四个参数:收件人,主题,消息和sent_at,它们定义发送电子邮件的时间.该方法还定义了六个标准参数,它们是每个ActionMailer方法的一部分 :

  • @subject定义电子邮件主题.

  • @body是一个Ruby哈希,包含可用于填充邮件模板的值.您创建了三个键值对:标题,电子邮件和消息

  • @recipients是要向其发送消息的人员列表.

  • @from定义了电子邮件的来源.

  • @sent_on接受sent_at参数并设置电子邮件的时间戳.

  • @headers是另一种可以修改电子邮件标题的哈希.例如,如果要发送纯文本或HTML电子邮件,则可以设置电子邮件的MIME类型.

创建控制器

现在,我们将为此应用程序创建一个控制器,如下所示 :

C:\ruby\emails> ruby script/generate controller Emailer

让我们在app/controllers/emailer_controller.rb中定义一个控制器方法 sendmail ,它将调用发送实际电子邮件的模型方法如下 :

class EmailerController < ApplicationController
   def sendmail
      recipient = params[:email]
      subject = params[:subject]
      message = params[:message]
      Emailer.deliver_contact(recipient, subject, message)
      return if request.xhr?
      render :text => 'Message sent successfully'
   end
end

使用邮件程序发送电子邮件联系方式,您必须将交付 _添加到方法名称的开头.如果request.xhr?你添加一个return,那么如果浏览器不支持JavaScript,你可以转到Rails Java Script(RJS),然后指示该方法呈现文本消息.

除了准备一个屏幕,你将获得用户信息发送电子邮件,你几乎已经完成了.让我们在控制器中定义一个屏幕方法索引,然后在下一节中,我们将定义所有必需的视图 :

在emailer_controller.rb文件中添加以下代码.

def index
   render :file => 'app\views\emailer\index.html.erb'
end

定义视图

在app \views\emails\index.html.erb中定义一个视图.这将被称为应用程序的默认页面,并允许用户输入消息并发送所需的电子邮件 :

<h1>Send Email</h1>
<% form_tag :action => 'sendmail' do %>
<p><label for="email_subject">Subject</label>:
<%= text_field 'email', 'subject' %></p>
<p><label for="email_recipient">Recipient</label>:
<%= text_field 'email', 'recipient' %></p>
<p><label for="email_message">Message</label><br/>
<%= text_area 'email', 'message' %></p>
<%= submit_tag "Send" %>
<% end %>

除上述视图外,我们还需要一个模板,发送消息时,Emailer的联系方式将使用该模板.这只是带有标准Rails的文本<%=%>占位符分散在各处.

只需将以下代码放在 app/views/contact.html.erb 文件中.

Hi!
You are having one email message from <%= @email %> with a title 
<%= @title %>
and following is the message:
<%= @message %>
Thanks

休息测试

在测试之前,请确保您的机器已连接到互联网并且您的电子邮件服务器和Web服务器已启动并正在运行.

现在,使用http://127.0.0.1:3000/Emailer/index测试您的应用程序.它显示以下屏幕,通过使用此屏幕,您可以将消息发送给任何人.

Send Email

发送邮件后,它会显示短信 - "邮件发送成功".

发送使用Rails的HTML电子邮件

要将邮件作为HTML发送,请确保您的视图(.erb文件)生成HTML并在电子邮件/app/models中将内容类型设置为html/emailer.rb 文件如下 :

class Emailer < ActionMailer::Base
   def contact(recipient, subject, message, sent_at = Time.now)
      @subject = subject
      @recipients = recipient
      @from = 'no-reply@yourdomain.com'
      @sent_on = sent_at
      @body["title"] = 'This is title'
      @body["email"] = 'sender@yourdomain.com'
      @body["message"] = message
      @headers = {content_type => 'text/html'}
   end
end

有关 ActionMailer 的完整详细信息,请查看标准的Ruby文档.