简体   繁体   English

动作邮件程序不发送/接收电子邮件轨道5

[英]Action mailer not sending/receiving email rails 5

I'm trying to create a simple blog (using rails 5) and want to send email updates when to users when I post something new. 我正在尝试创建一个简单的博客(使用rails 5),并希望在发布新内容时向用户发送电子邮件更新。 I'm trying to use actionmailer and I'm using devise to register users. 我正在尝试使用actionmailer,我正在使用设计来注册用户。 I'm trying to set it up with just the first user initially. 我试图最初只用第一个用户进行设置。

When I look at my local server the email appears to be sending but it's not received. 当我查看我的本地服务器时,电子邮件似乎正在发送但未收到。 Any advice would be very welcome I've been stuck a while. 任何建议都会非常受欢迎我已经被困了一段时间。

    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

My posts controller looks like this: 我的帖子控制器看起来像这样:

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

My mailer looks like this: 我的邮件看起来像这样:

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

and in my development.rb I have these settings: 在我的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"
  }

In your configured Gmail account: 在您配置的Gmail帐户中:

  1. Go to Account Settings Than 转到帐户设置

  2. Go to Sign-in & security , followed by Connected apps & sites 登录和安全 ,然后是已连接的应用和网站

  3. At last in Allow less secure apps ... Check it. 最后在允许不太安全的应用程序 ...检查它。

Check Once And Try 检查一次并尝试

 config.action_mailer.raise_delivery_errors = true config.action_mailer.perform_deliveries = true ... config.action_mailer.delivery_method = :smtp 

I don't config any of the above and it still works. 我没有配置上面的任何一个,它仍然有效。

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

and have to .deliver_later or .deliver_now , .deliver not work 并且必须.deliver_later.deliver_now.deliver不起作用

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

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