使用rails收发邮件
2007-11-14 14:57
232 查看
第一步
在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的邮件服务器发送
}
到这一步我们的邮件就差不多配好了!开始第二步,创建一个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
先别着急,我们现在来完成action
def send_mail
OrderMailer.deliver_confirm() //deliver_confirm(这里的confirm是模型中的confirm方法,deliver是固 定的写法)
render :text=> "scuuess!"
end
现在来建一个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
ok,大家现在可以发送邮件了.输入地址htto://localhost:3000/controllerName/send_mail,rails就会把confirm.rhtml的内容发送到邮箱
在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的邮件服务器发送
}
到这一步我们的邮件就差不多配好了!开始第二步,创建一个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
先别着急,我们现在来完成action
def send_mail
OrderMailer.deliver_confirm() //deliver_confirm(这里的confirm是模型中的confirm方法,deliver是固 定的写法)
render :text=> "scuuess!"
end
现在来建一个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
ok,大家现在可以发送邮件了.输入地址htto://localhost:3000/controllerName/send_mail,rails就会把confirm.rhtml的内容发送到邮箱
相关文章推荐
- 使用SMTP 和POP3 协议实现收发邮件(C/C++) (转)
- 使用Ruby收发邮件
- 在vs2005中如何设置使用Jmail收发邮件
- 使用TELNET手工操作 SMTP/POP 收发邮件
- 如何使用Jmail收发邮件
- 使用SQL Mail收发和自动处理邮件中的扩展存储过程简介
- 怎样使用outlook exprees收发外网邮件
- 如何使用outlook express 收发邮件
- 使用TELNET手工操作 SMTP/POP 收发邮件
- Exchange2007使用POP3/SMTP协议收发邮件
- VC下使用JMail实现邮件收发
- 使用java mail进行简单的邮件收发
- 使用JavaMail收发邮件
- 使用SMTP 和POP3 协议实现收发邮件(C/C++)
- 使用 Indy 的 TIdSMTP, TIdPOP3收发邮件在验证账户的时候出现错误乱码解决方法
- Exchange2007使用POP3/SMTP协议收发邮件
- 使用SQL Mail收发和自动处理邮件中的扩展存储过程简介
- 用Delphi实现邮件附件收发功能(使用Pop3 和Smtp)
- 使用Delphi收发GMail的邮件