使用Rails来发送Email. Rails Mailer概貌
1. script/generate mailer postoffice
2. 对你的邮件程序生成一个方法(models/postoffice.rb)
3.生成你自己的邮件template, 使用welcome.text.html.erb 和welcome.text.plain.erb (views/postoffice)
4. 发送信息
5.如果你进行本地php?name=%B2%E2%CA%D4" onclick="tagshow(event)" class="t_tag">测试,确定postfix在运行。
打开你的终端:
add3-imac: jon$ rails mailer_example
-- output truncated --
add3-imac: jon$ cd mailer_example/
add3-imac:mailer_example jon$ script/generate mailer postoffice
exists app/models/
create app/views/postoffice
exists test/unit/
create test/fixtures/postoffice
create app/models/postoffice.rb
create test/unit/postoffice_test.rb
现在我们为邮件程序生成一个方法:
class Postoffice < ActionMailer::Base
# located in models/postoffice.rb
# make note of the headers, content type, and time sent
# these help prevent your email from being flagged as spam
def welcome(name, email)
@recipients = "jon@addthree.com"
@from = params[:contact][:email]
headers "Reply-to" => "#{email}"
@subject = "Welcome to Add Three"
@sent_on = Time.now
@content_type = "text/html"
body[:name] = name
body[:email] = email
end
end
现在我们已经产生了我们的方法,需要改变template:
# located in views/postoffice
# we can access the variables we declared in models/postoffice.rb
# body[:name] = name is accessed by @name
# body[:email] = email is accessedby @email
# welcome.text.html.erb
# note the HTML
<p>Welcome to AddThree <i><%= @name %></i>. </p>
<p>The address we have on file for you is <b><%= @email %></b>, please let us know if this is incorrect.</p>
# welcome.text.plain.erb
Welcome to AddThree <%= @name %>. The address we have on file for you is <%= @email %>, please let us know if this is incorrect.
现在我们的邮件和template都已经准备好了,让我们发送邮件吧:
class Registration < ApplicationController
# controllers/registration_controller.rb
# assume the Registration controller already existed
# assume @user.name and @user.email have been declared
def send_welcome_email
# triggered via:
# http://localhost:3000/registration/send_welcome_email
# note the deliver_ prefix, this is IMPORTANT
Postoffice.deliver_welcome(@user.name, @user.email)
# optional, but I like to keep people informed
flash[:notice] = "You've successfuly registered. Please check your email for a confirmation!"
# render the default action
render :action => 'index'
end
end
如果您进行本地测试,确定postfix在运行
add3-imac:mailer_example jon$ sudo postfix start
Password:
postfix/postfix-script: starting the Postfix mail system
Sorry, no time to modify the codes to text style
thanks to http://www.jonathansng.com/ruby- ... end-email-tutorial/
by jonathan, and thanks to jonathan
分享到:
相关推荐
2-94街机外星风格射击游戏源码On Rails Shooter Template 1.202-94街机外星风格射击游戏源码On Rails Shooter Template 1.202-94街机外星风格射击游戏源码On Rails Shooter Template 1.202-94街机外星风格射击游戏...
With this fully revised new edition, take a holistic view of full-stack development to create usable, high-performing applications with Rails 5. Rails is a great tool for building web applications, ...
rails.vim提供了常用的一些命令,可以帮助开发,例如:Rgenerate, Rake, Rfind,RTview等,很方便,也很实用。 安装方法: 拷贝 autoload/rails.vim, plugin/rails.vim, 和 doc/rails.txt 到 ~/.vim 目录. ...
Addison.Wesley.Rails.AntiPatterns.Best.Practice.Ruby.on.Rails.Refactoring
klog2, 使用 Rails4 & angular.js 创建的博客应用程序 Klog使用 Rails 4.x 和 Angular.js 创建的博客应用程序屏幕截图 更多截图我的博客地址 http://chaoskeh.com演示版地址 http://klog-
SitePoint[1].Simply.Rails.2.2nd.Edition.May.2008.pdf
Rails 4 Test Prescriptions.pdf
rails 多图上传.txt
Agile.Web.Development.with.Rails.3nd.Edition.Beta
RailsSpace teaches you Ruby on Rails by developing a real-world application: RailsSpace, a social networking website aimed at the Rails community itself. We take you step by step, from the virtually...
Rails.Recipes.Rails.3.Edition.和源码
Ruby.on.Rails.Tutorial,Learn.Web.Development.with.Rails,Third.Edition-中文版 文字版.pdf 个人收集电子书,仅用学习使用,不可用于商业用途,如有版权问题,请联系删除!
Rails 4 Application Development.pdf
( [应用Rails进行敏捷Web开发(第4版)].(Agile.Web.Development.with.Rails.4th.Edition).S.Ruby&D.Thomas&D.H.Hansson.原版
CoffeeScript Programming with jQuery, Rails, and Node.js.pdf 下载
Ruby on rails开发从头学.CHM
This concise teaches you how to use the power of Docker and Kubernetes to deploy your Rails applications easily and efficiently. Docker and Kubernetes are increasing in popularity every day, but what ...
Ruby on Rails Enterprise Application Development.pdf Ruby Rails企业应用程序开发
ruby on rails 开发指南
minitest-rails, Rails的Minitest集成 minitestRails 5的Minitest集成 安装gem install minitest-rails这将安装以下宝石:minitest配置创建一个新的Rail