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密码
}

转载于:https://www.cnblogs.com/sinkzephyr/archive/2008/03/18/1111317.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值