【问题标题】:Action mailer not sending/receiving email rails 5Action mailer 不发送/接收电子邮件 rails 5
【发布时间】:2016-07-27 11:25:57
【问题描述】:

我正在尝试创建一个简单的博客(使用 rails 5),并希望在我发布新内容时向用户发送电子邮件更新。我正在尝试使用 actionmailer,并且正在使用设计来注册用户。我最初尝试仅使用第一个用户进行设置。

当我查看本地服务器时,电子邮件似乎正在发送,但未收到。任何建议都会非常受欢迎,我已经卡了一段时间了。

    UserNotifierMailer#sample_email: processed outbound mail in 410.4ms
Sent mail to myemail@gmail.com (853.2ms)
Date: Wed, 27 Jul 2016 12:05:33 +0100
From: from@example.com
To:myemail@gmail.com
Message-ID: <5798957d9de31_ae813ff962abfe1466438@Unknown-7c-d1-c3-78-04-d2.home.mail>
Subject: Sample Email
Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="--==_mimepart_5798957d951e2_ae813ff962abfe1466312";
 charset=UTF-8
Content-Transfer-Encoding: 7bit

我的帖子控制器如下所示:

def create
    @user = User.first
    @post = current_user.posts.build(post_params)
    if @post.save
      flash[:success] = "Your post has been created!"
      UserNotifierMailer.sample_email(@user).deliver
      redirect_to posts_path
    else
      flash[:alert] = "Your new post couldn't be created!  Please check the form."
      render :new
    end
  end

我的邮件是这样的:

class UserNotifierMailer < ApplicationMailer
  default from: "from@example.com"
  def sample_email(user)
    @user = user
    @url = 'http://www.google.com'
    mail(to: @user.email, subject: 'Sample Email')
  end
end

在我的 development.rb 中我有这些设置:

config.action_mailer.raise_delivery_errors = true
  config.action_mailer.perform_deliveries = true
  config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

  config.action_mailer.delivery_method = :smtp

  config.action_mailer.smtp_settings = {
    address: "smtp.gmail.com",
    port: 587,
    domain: "gmail.com",
    authentication: "plain",
    enable_starttls_auto: true,
    user_name: "my_gmail_username",
    password: "my_gmail_password"
  }

【问题讨论】:

标签: ruby-on-rails ruby email devise actionmailer


【解决方案1】:

在您配置的 Gmail 帐户中:

  1. 转到帐户设置

  2. 转到登录和安全,然后是连接的应用和网站

  3. 最后在 允许不太安全的应用程序 ...检查它。

检查一次并尝试

【讨论】:

    【解决方案2】:
    config.action_mailer.raise_delivery_errors = true
    config.action_mailer.perform_deliveries = true
    ...
    config.action_mailer.delivery_method = :smtp
    

    我没有配置以上任何一个,它仍然有效。

    config.action_mailer.default_url_options = { host: 'localhost:3000' }
    

    并且必须.deliver_later.deliver_now.deliver 不起作用

    【讨论】:

    • 我已将上面的代码用块引号括起来,因为您似乎指的是问题中的代码。如果这不正确,您可以展开来自revision historyedit 帖子的更改以进行进一步改进。不过,我不知道最后一句话是什么意思。
    猜你喜欢
    • 2013-05-21
    • 2015-08-23
    • 1970-01-01
    • 2015-09-08
    • 2011-05-15
    • 2019-10-29
    • 2011-07-14
    • 2014-02-08
    • 2012-07-25
    相关资源
    最近更新 更多