【问题标题】:Rails 4 ActionMailer not sending emails in development modeRails 4 ActionMailer 不在开发模式下发送电子邮件
【发布时间】:2015-08-25 18:59:47
【问题描述】:

首先,让我指定我知道 ActionMailer 在开发模式下默认发送电子邮件

另外,我查看了类似的问题,但没有一个与我的情况有关。

那么,我们开始吧:

  1. 我从 2015 年 3 月 26 日起关注 Ilya Bodrov-Krukowski 的 this Site Point tutorial,以便在我的 Rails 4 应用程序上安装和设置 Devise。

  2. 因为我们激活了 Devise 的 confirmable 模块,所以我需要在开发中发送电子邮件,以允许注册的用户激活他们的帐户并登录。

  3. 每当我创建一个新用户(通过http://localhost:3000/users/sign_up)时,我都会收到以下警报:

带有确认链接的消息已发送到您的电子邮件 地址。请点击链接激活您的帐户。

  1. 但是,我实际上从未收到带有确认链接的电子邮件。

—————————

更新

在我的 development.log 文件中,我可以看到电子邮件已“发送”:

Devise::Mailer#confirmation_instructions: processed outbound mail in 172.1ms

Sent mail to hello@mydomain.com (54.4ms)
Date: Tue, 25 Aug 2015 12:15:15 -0700
From: contact@anotherdomain.com
Reply-To: contact@anotherdomain.com
To: hello@mydomain.com
Message-ID: <55dcbec3a1ef8_33b93fcffc4988f493685@XXXXXX.local.mail>
Subject: Confirmation instructions
Mime-Version: 1.0
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

<p>Welcome hello@mydomain.com!</p>

<p>You can confirm your account email through the link below:</p>

<p><a href="http://localhost:3000/users/confirmation?confirmation_token=ZsL_iQVxfDQd7mB1RkGf">Confirm my account</a></p>

  [1m[35m (0.8ms)[0m  commit transaction
Redirected to http://localhost:3000/
Completed 302 Found in 560ms (ActiveRecord: 1.7ms)

而且,当我将日志中的链接复制并粘贴到浏览器中时,我会收到以下警报:

登录成功。

因此,似乎唯一不起作用的是电子邮件的实际发送/接收。

—————————

按照教程中的建议,我遵循了有关 ActionMailer configuration 的 Rails 文档,这是我在 config/environments/development.rb file 中的内容:

config.action_mailer.delivery_method = :sendmail
  # Defaults to:
  # config.action_mailer.sendmail_settings = {
  #   location: '/usr/sbin/sendmail',
  #   arguments: '-i -t'
  # }
  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.default_options = {from: 'hello@mydomain.com'}
  config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

显然,我遗漏了一些东西,但我不知道是什么(我对 Rails 非常非常初级)。

有什么想法吗?

【问题讨论】:

  • 你用的是什么邮件服务器?
  • 非常好的问题。我如何/在哪里可以获得这些信息? (也许这是我的问题:我认为我没有设置邮件服务器)。
  • 试试mailcatcher。它使处理开发中的电子邮件不再是 PITA。
  • 您指定的config.action_mailer.delivery_method 是什么?
  • 感谢您的提示。有没有办法让电子邮件在不使用第三方解决方案的情况下在开发模式下工作(即仅使用 Rails 默认配置选项)?

标签: ruby-on-rails ruby-on-rails-4 devise actionmailer devise-confirmable


【解决方案1】:

默认情况下不会在开发中发送邮件。要启用此功能,您必须在 development.rb 文件中设置 config.action_mailer.perform_deliveries = true,我看到您已经拥有该文件。

您可能还必须启用config.action_mailer.raise_delivery_errors = true 以引发递送错误。这样,您将能够查看电子邮件传递过程中是否有任何错误。

此外,请仔细检查您的 default from 是否有有效的电子邮件。

也可以试试把投递方式改成sendmail

config.action_mailer.delivery_method = :sendmail

这应该可以解决您的问题。

【讨论】:

  • 谢谢。如问题中所述,我已经定义了config.action_mailer.delivery_method = :sendmailconfig.action_mailer.raise_delivery_errors = true。还有其他可能导致问题的原因吗?是否有任何日志可以让我了解出了什么问题?
  • 你也有config.action_mailer.raise_delivery_errors = true 吗?然后它应该在交付时显示一些错误
  • 是的,我愿意。您可以在问题中查看我对config/environments/development.rb file 的所有配置。我没有错误。
【解决方案2】:

您需要一个邮件服务器供 Action Mailer 发送电子邮件。 如 MailGun 它有一个免费帐户和一个可以轻松设置的 gem。 mailgun_rails

【讨论】:

    猜你喜欢
    • 2014-12-31
    • 2014-12-06
    • 2014-01-13
    • 1970-01-01
    • 2012-08-29
    • 2014-12-05
    • 1970-01-01
    • 2011-08-11
    • 1970-01-01
    相关资源
    最近更新 更多