Actionmailer在Rails 3中无法发送邮件

15

我希望制作一个应用程序,当用户注册时能够发送电子邮件。

我在config/application.rb文件中添加了gmail的smtp设置,然后邮件功能如下:

mail(:to => "me@me.com", :subject => "Mail!", :from => "another@me.com", :content_type => "text/html")

现在当我查看日志时,我看到它说邮件已发送,但我根本没有收到任何邮件...

另外,当我调用邮件传递函数Emails.signed(@user).deliver时,表单页面不会重定向,但如果我注释掉发送电子邮件的代码,则可以正常工作。

Emails.signed(@user).deliver

或者

mail(:to => "me@me.com", :subject => "Mail!", :from => "another@me.com", :content_type => "text/html")

Thanks :)

Edit: development.rb

App::Application.configure do
  # Settings specified here will take precedence over those in config/environment.rb

  # In the development environment your application's code is reloaded on
  # every request.  This slows down response time but is perfect for development
  # since you don't have to restart the webserver when you make code changes.
  config.cache_classes = false

  # Log error messages when you accidentally call methods on nil.
  config.whiny_nils = true

  # Show full error reports and disable caching
  config.consider_all_requests_local       = true
  config.action_view.debug_rjs             = true
  config.action_controller.perform_caching = false

  # Don't care if the mailer can't send
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.perform_deliveries = true

  # Print deprecation notices to the Rails logger
  config.active_support.deprecation = :log

end

你是想要发送到开发环境还是生产环境?请粘贴 development.rbproduction.rb 文件。 - jpemberthy
我正在开发模式下...我添加了development.rb文件。 - Amit
嘿,请查看以下帖子,并让我们知道您是否继续遇到问题。http://asciicasts.com/episodes/206-action-mailer-in-rails-3 如果您已正确设置了Gmail SMTP设置,则应该可以正常工作。 - jpemberthy
嗨,这是我参考的同一个教程...这就是让我感到困惑的地方...也许开发模式不允许发送邮件,即使我将mail_delivery设置为true? - Amit
你怎么确定邮件没有被发送?默认情况下,所有邮件都是通过sendmail发送的。所以可能是因为你的sendmail队列系统出了问题。在你的输出中,并没有配置邮件的操作。 - shingara
4个回答

36

虽然有点晚了,但或许这可以帮助一些人节省几个小时的头痛。这可能仅适用于从Gmail发送电子邮件。 首先,在开发环境中为了帮助调试,将以下行设置为 true(假设你处于开发模式):

config.action_mailer.raise_delivery_errors = true
这将使ActionMailer不会默默地忽略错误。 当我这样做时,我意识到Gmail拒绝我的用户名和密码。 然后我去了我的配置文件,在那里我放置了所有的Action Mailer配置指令(对我来说是在development.rb中,可能有更好的放置位置),并注意到:user_name被设置为"admin"而不是"admin@thedomain.com"。更改它解决了问题。以下是我更正后的development.rb部分:
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
  :address              => "smtp.gmail.com",
  :port                 => 587,
  :domain               => 'thedomain.com',
  :user_name            => 'admin@thedomain.com',
  :password             => '<password>',
  :authentication       => 'plain',
  :enable_starttls_auto => true  }

参考资料:

http://forums.pragprog.com/forums/43/topics/541 http://edgeguides.rubyonrails.org/action_mailer_basics.html


7

另外一件不容忘记的事情是:在修改环境配置文件后,您必须重新启动应用程序。使用Passenger时,这很容易被忽略 :)

这就是解决ActionMailer未能发送电子邮件且没有显示任何错误的“问题”的方法。


我感到很傻,因为我意识到我没有重新启动我的服务器。在我重新启动服务器后,它完美地工作了。然而,我的日志文件创建的日志信息在我编辑代码之前和之后都是完全相同的,这让我认为它正在识别我的更改。我认为这是因为在实际开始引发交付错误之前,我需要重新启动服务器,这是我对配置文件的更改。因此,如果您已经告诉它开始引发交付错误,但尚未重新启动,则问题在重新启动服务器之前不会可见。 - Matt

6
这里的内容对我没有帮助。
我正在使用Rails 3.2.8,花了几个小时来摸索这个问题,结果在最后发现很简单。我忘记在返回的 Mail::Message 对象上调用 .deliver() 方法,该对象是由 mail(:to => @user.email, :subject => 'Welcome to the Site') 方法调用返回的。
只需像 官方RoR教程 中指定的那样保持一切不变即可。
也就是说,在你的开发/生产环境文件中,创建如下部分:
  # mailer
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.delivery_method = :smtp

  config.action_mailer.smtp_settings = {
      address:              'smtp.gmail.com',
      port:                 587,
      domain:               'gmail.com',
      user_name:            '<username>@gmail.com',
      password:             '<password>',
      authentication:       'plain',
      enable_starttls_auto: true
  }

那么你可以通过继承ActionMailer::Base来创建子类,例如像这样:

class InfoMailer < ActionMailer::Base
  default from: "<username>@gmail.com"

  def welcome_user_and_send_password(user, password)
    default_url_options = self.default_url_options()


    @user = user
    @password = password
    @url = default_url_options[:host]
    mail(:to => @user.email, :subject => 'Welcome to the Site').deliver()
  end

end

接下来,您可以像使用类方法一样从您的代码中使用这个 InfoMailer 方法:

InfoMailer.welcome_user_and_send_password(user, password)

0
如果您正在使用测试环境,请确保在environments/test.rb中注释掉此行代码:
config.action_mailer.delivery_method = :test

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接