ruby-on-rails - Action 邮件程序不发送/接收电子邮件 rails 5

标签 ruby-on-rails ruby email devise actionmailer

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

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

    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

我的帖子 Controller 看起来像这样:

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"
  }

最佳答案

在您配置的 Gmail 帐户中:

  1. 转到帐户设置

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

  3. 最后在 Allow less secure apps ...检查一下。

检查一次并尝试

关于ruby-on-rails - Action 邮件程序不发送/接收电子邮件 rails 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38611642/

相关文章:

javascript - Twitter Typeahead 与预取 JSON

ruby - 在 Ruby 数组 Enumerable Enumerator 中查找上一个元素

html - 所有的邮件客户端都支持拉伸(stretch) 1px 高度的图像吗?

linux - 您会选择哪个邮件服务器来开发您的产品

php电子邮件编辑界面及保存方法

ruby-on-rails - 最佳实践 - RoR 4 Heroku - Cron 每小时从外部 API 填充数据库

ruby-on-rails - 在 Thinking Sphinx 中对 has_many 关系使用 crc32 调整

ruby-on-rails - 在 rspec 请求规范中使用请求方法

mysql - 无法在 RubyMine 中将 mysql2 gem 与 Rails 4.1.5 一起使用

ruby-on-rails - rails : Return only objects where all associations meet a condition