Rails 4 ActionMailer 在开发模式下不发送电子邮件

Rails 4 ActionMailer not sending emails in development mode

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

另外,我也看了类似的问题,其中 none 与我的情况相关。

那么,我们开始吧:

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

  2. 因为我们激活了Devise的confirmable模块,我需要在开发中发送邮件让注册的用户激活他们的账户并登录。

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

A message with a confirmation link has been sent to your email address. Please follow the link to activate your account.

  1. 但是,我从未真正收到确认邮件 link。

————————————

更新

在我的 development.log 文件中,我可以看到电子邮件是 "sent":

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)

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

Signed in successfully.

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

————————————

按照教程中的建议,我遵循了 Rails 有关 ActionMailer configuration 的文档,这是我的 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)。

有什么想法吗?

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

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

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

也尝试将发送方式切换为 sendmail:

config.action_mailer.delivery_method = :sendmail

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

您需要一个邮件服务器,以便 Action Mailer 发送电子邮件。 如 MailGun 它有一个免费帐户和一个 gem,这使得设置变得容易。 mailgun_rails