简体   繁体   English

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

[英]Rails 4 ActionMailer not sending emails in development mode

First, let me specify that I am aware that ActionMailer does NOT send emails by default in development mode . 首先,让我指出我知道ActionMailer在开发模式下默认发送电子邮件

Also, I took a look at similar questions, and none of them was relevant to my situation. 此外,我看了类似的问题,但没有一个与我的情况有关。

So, here we go: 那么,我们走了:

  1. I am following this Site Point tutorial by Ilya Bodrov-Krukowski, from March 26, 2015 in order to install and setup Devise on my Rails 4 app. 我正在遵循Ilya Bodrov-Krukowski的这个Site Point教程 ,从2015年3月26日开始,以便在我的Rails 4应用程序上安装和设置Devise。

  2. Because we activated Devise's confirmable module, I need to send emails in development to allow users who signup to activate their account and login. 因为我们激活了Devise的可confirmable模块,所以我需要在开发中发送电子邮件,以允许注册的用户激活他们的帐户并登录。

  3. Whenever I create a new user (through http://localhost:3000/users/sign_up ), I do get the following alert: 每当我创建一个新用户时(通过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. However, I never actually receive the email with the confirmation link. 但是,我实际上从未收到带有确认链接的电子邮件。

—————————— ----------

UPDATE UPDATE

In my development.log file, I can see that the email was "sent": 在我的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)

And, when I copy and paste the link from the log into my browser, then I get the following alert: 并且,当我将日志中的链接复制并粘贴到我的浏览器中时,我收到以下警告:

Signed in successfully. 成功签约。

So, it seems the only thing that is not working is the actual sending / receiving of the email. 因此,似乎唯一不起作用的是实际发送/接收电子邮件。

—————————— ----------

As recommended in the tutorial, I followed Rails documentation regarding ActionMailer configuration and here is what I have in my config/environments/development.rb file : 根据教程中的建议,我遵循了有关ActionMailer配置的 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 }

Obviously, I am missing something, but I cannot figure out what (I am very, very junior with Rails). 显然,我错过了一些东西,但我无法弄清楚是什么(我是非常非常初级的Rails)。

Any idea? 任何的想法?

Mail is not sent in development by default. 默认情况下,邮件不会在开发中发送。 To enable this you have to set config.action_mailer.perform_deliveries = true in development.rb file which I see you already have. 要启用此功能,您必须在我看到您已经拥有的development.rb文件中设置config.action_mailer.perform_deliveries = true

You may also have to enable config.action_mailer.raise_delivery_errors = true to raise delivery errors. 您可能还必须启用config.action_mailer.raise_delivery_errors = true以引发传递错误。 That way, you will be able to see if there are any errors in the email delivery process. 这样,您就可以看到电子邮件递送过程中是否存在任何错误。

Also, double check your default from if that has a valid email. 此外,如果有有效的电子邮件,请仔细检查您的default from

Also try switching the delivery method to sendmail : 还尝试将交付方式切换到sendmail

config.action_mailer.delivery_method = :sendmail

This should solve your issue. 这应该可以解决您的问题。

You need a mail server for Action Mailer to send emails. 您需要Action Mailer的邮件服务器才能发送电子邮件。 such as MailGun which has a free account and a gem which makes set up easy. 例如MailGun ,它有一个免费帐户和一个易于设置的宝石。 mailgun_rails mailgun_rails

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM