Ruby on Rails 发送邮件环境设置

运行环境:
ruby 1.8.6
rails 1.2.5
gem 1.0.1

这里用的是gmail的服务器,由于rails中的actionMailer不支持TLS(SSL)连接,但这却是Gmail SMTP服务器唯一的传输方式。网上已经有专门这个问题提供的plugin,下面总结一下配置的具体步骤:
1.安装插件:  打开cmd,进入rails项目的目录
输入 ruby script/plugin install http://svn.xlsuite.org/trunk/vendor/plugins/action_mailer_tls/

然后在项目目录下寻找"smtp_tls.rb",并将其放到项目的lib目录下

2.配置rails项目的环境:
打开项目config/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 => "yourname@gmail.com",                  #使用邮件服务器的帐号(这里是google,所以是goole邮箱的帐号)
:password => "yourpassword",                     #使用邮件服务器的密码
#注意:  这里我只是指定了邮件服务器,不是说我指定了google的邮件服务器,就非要使用google的邮箱发送邮件,也可以使用别的邮箱通过google的邮件服务器发送
}

3.重启项目server

如果你的rails版本较新:
在environment.rb文件Rails::Initializer.run do |config| 和 end之间添加如下的配置信息:

 # config/environments/development.rb
config.action_mailer.raise_delivery_errors = true #错误异常是事抛给应用程序

# set delivery method to :smtp, :sendmail or :test
config.action_mailer.delivery_method = :smtp # 发送邮件方式

# these options are only needed if you choose smtp delivery
config.action_mailer.smtp_settings = {
:address        
=> 'smtp.gmail.com',
:port           
=> 587,
:authentication 
=> :plain,
:user_name      
=> 'yourname@gmail.com'#你的gmail帐号
:password       => 'yourpassword' #你的gmail密码
}

posted @ 2008-03-18 12:38  Sink  阅读(2124)  评论(0编辑  收藏  举报