在开发环境中使用 Rails 3 发送邮件 [英] Sending mail with Rails 3 in development environment
问题描述
我确定这之前已经被问过一百万次,但我找不到任何适合我的东西,所以我再问一次!
I'm sure this has been asked a million times before but I can't find anything that works for me so I'm asking again!
我只需要一种在 rails 3 中使用 ActionMailer 发送电子邮件的方法.我已经学习了许多教程,包括关于新 ActionMailer 的 Railscasts 教程,我可以看到正在生成的邮件,但我没有收到.
I just need a way of sending emails using ActionMailer in rails 3. I have followed numerous tutorials including the Railscasts tutorial on the new ActionMailer and I can see the mails being generated but I don't receive them.
我尝试了很多不同的方法,但它们通常相当于配置以下设置
I have tried a bunch of different ways but they generally amount to configuring the following settings
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "gmail.com",
:user_name => "xxx@gmail.com",
:password => "yyy",
:authentication => "plain",
:enable_starttls_auto => true
}
我已经在我的 config/environment.rb、config/environments/development.rb 中尝试了上述代码(当然还有有效的 gmail 详细信息),并且目前在它自己的初始化程序 config/initialisers/setup_mail.rb 中有它
I have tried the above code (with valid gmail details of course) in my config/environment.rb, config/environments/development.rb and currently have it in its own initialiser config/initialisers/setup_mail.rb
我还尝试了一些不同的 smtp 服务器,包括 Gmail 和 Sendgrid,相应地调整了 smtp 设置,但仍然没有.我可以在终端中看到邮件和开发日志,仅此而已.
I have also tried with a few different smtp servers including Gmail and Sendgrid, adjusting the smtp settings accordingly but still nothing. I can see the mail in the terminal and the development log and that's it.
有没有人知道我可能错过的任何其他问题,需要设置 ActionMailer 才能工作?如果失败,有没有办法获得有关为什么没有发送邮件的更多信息?我有
Does anyone know of any other gotcha's that I may have missed that need to be setup for ActionMailer to work? Failing that is there a way of getting more information about why the mails aren't being sent? I have
config.action_mailer.raise_delivery_errors = true
在我的 config/development.rb 中设置,但开发日志仍然与我在终端中看到的一样.
set in my config/development.rb but the development log still just shows the same as I see in the terminal.
无论如何,我正在 Ubuntu 10.04 笔记本电脑上进行开发,以防万一需要进行任何特定设置.
For what it's worth, I am developing on a Ubuntu 10.04 laptop just in case there's any specific setup needed for that.
非常感谢
推荐答案
嗯,我已经解决了这个问题,但是为什么这有效而其他方法没有,我不知道.
Well I have resolved the issue, but quite why this works and the other methods did not, I don't know.
解决方案是在 config/initialisers/setup_mail.rb 中创建一个包含以下内容的初始化程序
The solution was to create an initialiser in config/initialisers/setup_mail.rb containing the following
if Rails.env != 'test'
email_settings = YAML::load(File.open("#{Rails.root.to_s}/config/email.yml"))
ActionMailer::Base.smtp_settings = email_settings[Rails.env] unless email_settings[Rails.env].nil?
end
然后我添加了包含开发和生产电子邮件帐户详细信息的 config/email.yml
I then added config/email.yml containing the details of the dev and production email accounts
development:
:address: smtp.gmail.com
:port: 587
:authentication: plain
:user_name: xxx
:password: yyy
:enable_starttls_auto: true
production:
:address: smtp.gmail.com
:port: 587
:authentication: plain
:user_name: xxx
:password: yyy
:enable_starttls_auto: true
就像我说的,不知道为什么,但这似乎奏效了.谢谢大家指点
Like I say, no idea why, but this seemed to do the trick. Thanks all for the pointers
这篇关于在开发环境中使用 Rails 3 发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!