padrino 是一款 ruby for web 框架, 相比ruby on rails 更为轻巧。
接下来,我将一步一步来介绍用 resque gem 来队列发送邮件
首先必不可少的就是安装redis数据库,redis 是一款 高性能的 Nosql 数据库
安装,启动方法很简单,参考 http://redis.io/
普通 邮件发送 可以参考这里
http://www.padrinorb.com/guides/padrino-mailer
这里介绍了最简易的方式
post :create do email(:from => "[email protected]", :to => "[email protected]", :subject => "Welcome!", :body=>"Body") end
但是简易往往是死板,最不可用的。
而且我发现除了 gmail之外,qq邮箱, qq企业邮箱等国内邮箱似乎都很难送达。
自定义邮箱发送(其实也不是很难),只需在终端输入指令,自动生成代码框架,剩下你要做的就是复制黏贴而已。
终端输入
padrino g mailer Sample registration_email
我们会得到 一个sample.rb文件 和sample渲染模板目录
sample.rb文件是这样的 大致的说明 和范例都在这个文件中了,是不是很方便
## # Mailer methods can be defined using the simple format: # # email :registration_email do |name, user| # from '[email protected]' # to user.email # subject 'Welcome to the site!' # locals :name => name # content_type 'text/html' # optional, defaults to plain/text # via :sendmail # optional, to smtp if defined, otherwise sendmail # render 'registration_email' # end # # You can set the default delivery settings from your app through: # # set :delivery_method, :smtp => { # :address => 'smtp.yourserver.com', # :port => '25', # :user_name => 'user', # :password => 'pass', # :authentication => :plain, # :plain, :login, :cram_md5, no auth by default # :domain => "localhost.localdomain" # the HELO domain provided by the client to the server # } # # or sendmail (default): # # set :delivery_method, :sendmail # # or for tests: # # set :delivery_method, :test # # or storing emails locally: # # set :delivery_method, :file => { # :location => "#{Padrino.root}/tmp/emails", # } # # and then all delivered mail will use these settings unless otherwise specified. #
于是我们接下来就是复制黏贴罢了。 于是写下如是代码
Xxxx::App.mailer :sample do email :registration_email do |info| # Your mailer goes here ... from '[email protected]' to info[:email] subject info[:subject] locals :content => info[:content] render 'sample/registration_email' content_type :html # optional, defaults to :plain via :smtp # optional, to smtp if defined otherwise sendmail end end
看到上方的&block 区域,其中 render 表示 需要渲染的模板,对 registration_email就是我们要添加的模板文件
locals 是模板中的变量
接下来 我们要新建模板文件 registration_email.erb
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" /> </head> <body > <div> <%=content%></div> </body> </html>
这样就可以了。
那么我们如何调用呢?
首先要配置
XXX::App.controllers :user do set :delivery_method, :smtp => { :address => "smtp.***.com", :port => 25, :user_name => '****', :password => '***' :authentication => :plain, :enable_starttls_auto => true } get :test do info = {:content => 'this is content', :email => '[email protected], [email protected]', :subject => 'subject' } deliver_msg = deliver(:sample, :registration_email, info) end
OK,以上就是一般方法来发邮件,虽然说可以多email同时发送,但是如果你要是大批量发送邮件或者是不同用户发送不同信件的话,难道你要在test页面等待很久时间么?
我不是一个喜欢等待的人,所以我更愿意把任务交给队列来做,我从github参考了很多做法,总结了一个流程,希望对你有帮助。
首先我们要安装 resque
https://github.com/resque/resque
gem install resque
自动生成配置文件等
padrino g plugin resque
然后新建一个文件 lib/email_resque.rb
require 'mail' module Emailer class Send @queue = :issue_mailer def self.perform(address, subject, body) Mail.defaults do delivery_method :smtp, { :address => "smtp.***.com", :port => 25, :user_name => '***', :password => '***', :authentication => :plain, :enable_starttls_auto => true } end mail = Mail.new do from "***" to address subject subject content_type "text/html; charset=UTF-8" body body end mail.deliver! end end end
在控制器中,
XXX::App.controllers :user do get :test do subject = 'Hi, subject' @content = 'this is content' Resque.enqueue(Emailer::Send, '[email protected],[email protected]', subject, render('mailers/sample/registration_email.erb')) end
因为content是全局变量,所以 registration_email.erb
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" /> </head> <body > <div> <%=@content%></div> </body> </html>
一切ok后 启动redis
Moks-MBP:redis-2.8.17 moksteven$ src/redis-server
启动resque 队列
padrino rake resque:work QUEUE='*'
或后台运行
nohup padrino rake resque:work QUEUE='*' &
好了,以上就是 padrino 发送邮件全部内容,如果有什么纰漏的。烦请指正!