2016-07-27 122 views
4

我试图创建一个简单的博客(使用rails 5),并希望发送电子邮件更新时,当我发布新的东西的用户。我试图使用actionmailer,我正在使用设计来注册用户。我试图在第一个用户开始时进行设置。动作邮件不发送/接收电子邮件5

当我看着我的本地服务器的电子邮件似乎是发送,但它没有收到。任何建议将非常欢迎我一直卡住了一段时间。

UserNotifierMailer#sample_email: processed outbound mail in 410.4ms 
Sent mail to [email protected] (853.2ms) 
Date: Wed, 27 Jul 2016 12:05:33 +0100 
From: [email protected] 
To:[email protected] 
Message-ID: <[email protected]me.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: "[email protected]" 
    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" 
    } 
+0

试着改变对你的谷歌帐户此功能(不知道这是否会帮助):https://www.google。 com /设置/安全/ lesssecureapps – Deep

+0

我终于得到它的工作,这是谷歌2步验证。确保它已关闭! –

回答

2

在您配置的Gmail中ccount:

  1. 转到帐户设置

  2. 转到登录在&安全,其次是关联的应用&网站

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

检查一次,并尝试

0
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不行

+0

我已经用blockquotes包装了上面的代码,因为您似乎指的是问题中的代码。如果这不正确,您可以从[修订历史记录](http://stackoverflow.com/posts/40966354/revisions)或[编辑]文章中展开更改以进行进一步的改进。不过,我不知道最后一句话的意思。 –