第一步
在environment.rb 加上
require 'smtp_tls' //引用libsmtp_tls.rb
ActionMailer::Base.delivery_method = :smtp //使用smtp发送邮件
ActionMailer::Base.default_charset = "UTF-8" //指定发送邮件时使用的字符集
ActionMailer::Base.server_settings = {
:address => "smtp.gmail.com", //使用的邮件服务器
:port => 587, //邮件服务器的端口号
:domain => "xxx.com", //暂时忽略
:authentication => :login, //不是很清楚,照着写
:user_name => "long0428", //使用邮件服务器的帐号(这里是google,所以时goole邮箱的帐号)
:password => "63345133", //使用邮件服务器的密码(这里是google,所以时goole邮箱的密码)
//注意: 这里我只是指定了邮件服务器,不是说我指定了google的邮件服务器,就非要使用google的邮箱发送邮件,也可以使用别的邮箱通过google的邮件服务器发送
}
ActionMailer::Base.delivery_method = :smtp //使用smtp发送邮件
ActionMailer::Base.default_charset = "UTF-8" //指定发送邮件时使用的字符集
ActionMailer::Base.server_settings = {
:address => "smtp.gmail.com", //使用的邮件服务器
:port => 587, //邮件服务器的端口号
:domain => "xxx.com", //暂时忽略
:authentication => :login, //不是很清楚,照着写
:user_name => "long0428", //使用邮件服务器的帐号(这里是google,所以时goole邮箱的帐号)
:password => "63345133", //使用邮件服务器的密码(这里是google,所以时goole邮箱的密码)
//注意: 这里我只是指定了邮件服务器,不是说我指定了google的邮件服务器,就非要使用google的邮箱发送邮件,也可以使用别的邮箱通过google的邮件服务器发送
}
到这一步我们的邮件就差不多配好了!开始第二步,创建一个OrderMailser的模型
class OrderMailer < ActionMailer::Base //一定要继承ActionMailer::Base
def confirm(sent_at = Time.now)
@subject = 'helloWorld' //主题
@body = {'name' => 'dahsjkdh'} //'name'是变量名,在confirm.rhtml里面声明
如果confirm.rhtml里面没有变量可以不需要@body这个参数
@recipients = 'long0428@gmail.com' //接受人地址
@from = 'liverpool_long@tom.com' //发送人地址
@sent_on = sent_at //按时间发送sent_at = Time.now指明现在发送
@headers = {} //用于指定头信息
end
end
def confirm(sent_at = Time.now)
@subject = 'helloWorld' //主题
@body = {'name' => 'dahsjkdh'} //'name'是变量名,在confirm.rhtml里面声明
如果confirm.rhtml里面没有变量可以不需要@body这个参数
@recipients = 'long0428@gmail.com' //接受人地址
@from = 'liverpool_long@tom.com' //发送人地址
@sent_on = sent_at //按时间发送sent_at = Time.now指明现在发送
@headers = {} //用于指定头信息
end
end
先别着急,我们现在来完成action




现在来建一个rhtml,记住rhtml的名字也需要时confirm(action,模型,rhtml都是同步的)
HelloWorld
这是第一次发送那个邮件!
这是第一次发送那个邮件!
最后别忘了,我们在lib文件夹上加上个libsmtp_tls.rb文件
require "openssl"
require "net/smtp"
Net::SMTP.class_eval do
private
def do_start(helodomain, user, secret, authtype)
raise IOError, 'SMTP session already started' if @started
check_auth_args user, secret, authtype if user or secret
sock = timeout(@open_timeout) { TCPSocket.open(@address, @port) }
@socket = Net::InternetMessageIO.new(sock)
@socket.read_timeout = 60 #@read_timeout
@socket.debug_output = STDERR #@debug_output
check_response(critical { recv_response() })
do_helo(helodomain)
raise 'openssl library not installed' unless defined?(OpenSSL)
starttls
ssl = OpenSSL::SSL::SSLSocket.new(sock)
ssl.sync_close = true
ssl.connect
@socket = Net::InternetMessageIO.new(ssl)
@socket.read_timeout = 60 #@read_timeout
@socket.debug_output = STDERR #@debug_output
do_helo(helodomain)
authenticate user, secret, authtype if user
@started = true
ensure
unless @started
# authentication failed, cancel connection.
@socket.close if not @started and @socket and not @socket.closed?
@socket = nil
end
end
def do_helo(helodomain)
begin
if @esmtp
ehlo helodomain
else
helo helodomain
end
rescue Net::ProtocolError
if @esmtp
@esmtp = false
@error_occured = false
retry
end
raise
end
end
def starttls
getok('STARTTLS')
end
def quit
begin
getok('QUIT')
rescue EOFError
end
end
end
require "net/smtp"
Net::SMTP.class_eval do
private
def do_start(helodomain, user, secret, authtype)
raise IOError, 'SMTP session already started' if @started
check_auth_args user, secret, authtype if user or secret
sock = timeout(@open_timeout) { TCPSocket.open(@address, @port) }
@socket = Net::InternetMessageIO.new(sock)
@socket.read_timeout = 60 #@read_timeout
@socket.debug_output = STDERR #@debug_output
check_response(critical { recv_response() })
do_helo(helodomain)
raise 'openssl library not installed' unless defined?(OpenSSL)
starttls
ssl = OpenSSL::SSL::SSLSocket.new(sock)
ssl.sync_close = true
ssl.connect
@socket = Net::InternetMessageIO.new(ssl)
@socket.read_timeout = 60 #@read_timeout
@socket.debug_output = STDERR #@debug_output
do_helo(helodomain)
authenticate user, secret, authtype if user
@started = true
ensure
unless @started
# authentication failed, cancel connection.
@socket.close if not @started and @socket and not @socket.closed?
@socket = nil
end
end
def do_helo(helodomain)
begin
if @esmtp
ehlo helodomain
else
helo helodomain
end
rescue Net::ProtocolError
if @esmtp
@esmtp = false
@error_occured = false
retry
end
raise
end
end
def starttls
getok('STARTTLS')
end
def quit
begin
getok('QUIT')
rescue EOFError
end
end
end