在开发环境中使用Rails 3发送邮件

我相信这个问题已经被提出了一百万次,但是我找不到任何对我有用的东西,所以我再次提问!

我只需要在rails 3中使用ActionMailer发送电子邮件。我遵循了许多教程,包括新的ActionMailer的Railscasts教程,我可以看到正在生成的邮件,但是我没有收到它们。

我已经尝试了一堆不同的方法,但他们通常相当于configuration以下设置

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细节),并且目前已经在它自己的初始化configuration/ initialisers / setup_mail.rb

我也尝试了几个不同的SMTP服务器,包括Gmail和Sendgrid,相应地调整SMTP设置,但什么也没有。 我可以在terminal和开发日志中看到邮件,就是这样。

有没有人知道任何其他问题,我可能已经错过了需要安装的ActionMailer工作? 如果没有办法获得关于邮件为什么不被发送的更多信息? 我有

 config.action_mailer.raise_delivery_errors = true 

在我的config / development.rb中设置,但是开发日志仍然显示与我在terminal中看到的相同。

对于它的价值,我正在开发一个Ubuntu 10.04笔记本电脑,以防万一有任何具体的设置需要的。

非常感谢

那么我已经解决了这个问题,但为什么这个工作,其他方法没有,我不知道。

解决方法是在config / initialisers / setup_mail.rb中创build一个包含以下内容的初始化程序

 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

 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 

就像我说的,不知道为什么,但这似乎是伎俩。 感谢所有的指针

我有以下在config/environments/development.rb

 config.action_mailer.raise_delivery_errors = true config.action_mailer.perform_deliveries = true 

实际的邮件configurationconfig.actionmailer.*我已经放在config\application.rb

希望这可以帮助 :)

尝试使用“sendmail”而不是“smtp”。

 ActionMailer::Base.delivery_method = :sendmail ActionMailer::Base.sendmail_settings = { :address => "smtp.gmail.com", :port => "587", :domain => "gmail.com", :user_name => "xxx@gmail.com", :password => "yyy", :authentication => "plain", :enable_starttls_auto => true } 

三件事。

首先,端口是一个整数,不需要引号,就像你的第一个例子。 (但我认为一个string应该仍然有效。)

其次,不要忘记每次修改这个(或任何)初始化文件时重新启动服务器。 这可以解释为什么添加后没有看到错误:

config.action_mailer.raise_delivery_errors = true

没有这个错误信息,很难确定为什么邮件不会但现在是。 一种可能是你使用密码周围的双引号。 如果您使用的是强密码,并且密码中有一个未被转义的令牌,则可能已被重新解释。 (即"P@ssw\0rd"将成为P@ssrd )。 因为这个原因,我总是在我的代码中使用单引号,除非我特别需要语法糖。

最后, enable_starttls_auto: true是默认的,不必要的。

只要把所有的configuration:config / environments / development.rb

我的意思是

 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.action_mailer.raise_delivery_errors = true config.action_mailer.perform_deliveries = true 

它为我工作。

ActionMailer::Base.delivery_method = :sendmail

config.action_mailer.perform_deliveries = true

是解决这个问题的两个必要步骤

除此之外,你的gmail用户名不会被别名。

参考: https : //support.google.com/mail/answer/12096?hl = zh_CN