使用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 目录. ...
klog2, 使用 Rails4 & angular.js 创建的博客应用程序 Klog使用 Rails 4.x 和 Angular.js 创建的博客应用程序屏幕截图 更多截图我的博客地址 http://chaoskeh.com演示版地址 http://klog-
Ruby on Rails:Rails与JavaScript集成.docx
Ruby on Rails:部署Rails应用至Heroku.docx
- **Michael Koziarski**:作为 Rails 核心团队成员,他指出本书是一本全面且详尽的指南,对于希望深入理解 Rails 3 的开发者来说非常有价值。 - **Alex Graven**:他赞赏本书的可读性和吸引力,认为它既提供了实际...
从给定的文件信息来看,这是一本名为《Simply Rails 2》的书籍,第二版于2008年5月出版,作者是Patrick Lenz。本书旨在为初学者提供一个全面的Ruby on Rails入门指南,涵盖了从Ruby编程语言的基础到Rails框架的深入...
本书旨在通过实际开发一个面向Ruby on Rails社区的社交网络应用——RailsSpace,来教授读者如何使用Ruby on Rails进行Web开发。从静态页面到动态功能丰富的网站,本书将带领读者完成一系列实用的开发任务。 #### 二...
( [应用Rails进行敏捷Web开发(第4版)].(Agile.Web.Development.with.Rails.4th.Edition).S.Ruby&D.Thomas&D.H.Hansson.原版
Ruby on rails开发从头学.CHM
Ruby_on_Rails_rails.zip Ruby_on_Rails_rails.zip Ruby_on_Rails_rails.zip Ruby_on_Rails_rails.zipRuby_on_Rails_rails.zip Ruby_on_Rails_rails.zip Ruby_on_Rails_rails.zip Ruby_on_Rails_rails.zipRuby_on_...
这篇博客文章“Rails中应用Ext.tree:以中国的省市地区三级联动选择为例”提供了一个实用的示例,教我们如何利用Ext.js库中的Tree组件来实现这种功能。 首先,让我们了解Rails和Ext.js的基本概念。Rails是基于Ruby...
ruby on rails 开发指南
minitest-rails, Rails的Minitest集成 minitestRails 5的Minitest集成 安装gem install minitest-rails这将安装以下宝石:minitest配置创建一个新的Rail
《Head First Rails中文版.2011.12》这本书是针对初学者设计的一本深入浅出的Rails教程,旨在帮助读者快速掌握Ruby on Rails框架。Ruby on Rails(简称Rails)是由David Heinemeier Hansson开发的一个开源Web应用...
《敏捷Web开发:Rails 3版》是一本深入探讨使用Ruby on Rails进行敏捷Web开发的专业书籍,其Beta版提供了一次独特的预览学习机会。Rails是Ruby编程语言的一个强大框架,它极大地简化了Web应用程序的构建过程,尤其...
Rails.application.config.middleware.use OmniAuth::Builder do provider :developer unless Rails.env.production? provider :twitter, ENV['TWITTER_KEY'], ENV['TWITTER_SECRET'] end 标签:安全相关...