2016-07-27 125 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)或[編輯]文章中展開更改以進行進一步的改進。不過,我不知道最後一句話的意思。 –