在开发环境中使用Rails 3发送邮件 [英] Sending mail with Rails 3 in development environment

查看:65
本文介绍了在开发环境中使用Rails 3发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定这已经被问了一百万遍了,但是我找不到对我有用的任何东西,所以我再问一次!

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
}

我尝试了上面的代码(带有有效的Gmail详细信息)我的config / environment.rb,config / environments / development.rb,目前在它自己的初始化程序中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

我也尝试过包括Gmail和Sendgrid在内的几种不同的smtp服务器,可以相应地调整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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆