某个手上的项目是一路从Rails 2.x.x升级到了Rails 3.1.1。在老版本的Rails中,如果需要通过Gmail的SMTP来发送邮件,需要给Ruby的Net::SMTP打个smtp-tls的补丁,或者通过类似的插件如smtp-tls来解决问题。
通过bundle升级过程中发现gem无法找到smtp-tls插件,因为这个插件并没有host在 http://rubygems.org上,而是host在 http://gems.github.com。在这个插件的homepage上说如果Rails >= 2.2 并且Ruby的版本在v1.8.7以上,只需要在你的ActionMailer的config中配置enable_starttls_auto即可。
1
2
3
4
5
6
7
8
9
| ActionMailer::Base.smtp_settings = {
:enable_starttls_auto => true,
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'example.com',
:authentication => :plain,
:user_name => 'user',
:password => 'secret'
}
|
但是读ActionMailer源码发现,Rails的默认已经打开了enable_starttls_auto,这里有一些相关的讨论。所以如果你用上了Rails 3.1.1或者以上版本,你无需作任何特别的设置即可工作。
1
2
3
4
5
6
7
8
| $ rails c
Loading development environment (Rails 3.1.1)
>> include ActionMailer::DeliveryMethods
=> Object
>> smtp_settings
=> {:address=>"localhost", :port=>25, :domain=>"localhost.localdomain", :user_name=>nil, :password=>nil, :authentication=>nil, :enable_starttls_auto=>true}
>> smtp_settings[:enable_starttls_auto]
=> true
|