2016-12-29 275 views
0

我正在使用設計進行身份驗證。它提供了一個忘記的密碼鏈接。當我發送電子郵件時,電子郵件不會被髮送。以下是我使用的設置。你能告訴我爲什麼gmail不發送郵件嗎?我還打開了「允許安全性較低的應用程序發送電子郵件」,並且我還啓用了gmail設置中的imap。爲什麼設計不通過gmail smtp發送電子郵件?

application.rb具有以下設置。

ActionMailer::Base.smtp_settings = { 


    :address => 'smtp.gmail.com', 
    :domain => 'mail.google.com', 
    :port => 587, 
    :user_name => '[email protected]', 
    :password => 'validpassword', 
    :authentication => 'login', 
    :enable_starttls_auto => true 


} 

development.rb有

config.action_mailer.default_url_options = { host: '127.0.0.1'} 


    config.action_mailer.delivery_method = :smtp 

發送電子郵件我得到在控制檯下面的文本之後。

Devise::Mailer#reset_password_instructions: processed outbound mail in 215.2ms 
Sent mail to [email protected] (1097.6ms) 
Date: Thu, 29 Dec 2016 09:50:41 +0000 
From: [email protected] 
Reply-To: [email protected] 
To: [email protected] 
Message-ID: <[email protected]l> 
Subject: Reset password instructions 
Mime-Version: 1.0 
Content-Type: text/html; 
charset=UTF-8 
Content-Transfer-Encoding: 7bit 

<p>Hello [email protected]!</p> 

<p>Someone has requested a link to change your password. You can do this through the link below.</p> 

<p><a href="http://127.0.0.1/users/password/edit?reset_password_token=WQxYad91mPghMxaurYA5">Change my password</a></p> 

<p>If you didn't request this, please ignore this email.</p> 
<p>Your password won't change until you access the link above and create a new one.</p> 

Redirected to https://rubyonrails-kofhearts.c9users.io/users/sign_in 
Completed 302 Found in 1965ms (ActiveRecord: 14.7ms) 

UPDATE:

我只是按照本教程。

https://www.youtube.com/watch?v=ZEk0Jp2dThc

發送電子郵件沒有與在此視頻中指定的設置工作。

+0

可能是重複http://stackoverflow.com/questions/25735206/rails-4-smsmpauthenticationerror-535-5-7-0-authentication-failed/25884665#25884665 –

+0

我知道這不是你的答案問題,但我放棄了通過Gmail使用smtp。我現在使用'sendinblue',他們有一個smpt服務,每天約300封電子郵件免費。也是一個很好的紅寶石,所以它很容易使用。 – Iceman

回答

0

這應該是有效的。 也許你也可以打開config.action_mailer.raise_delivery_errors = trueconfig/environments/development.rb。 調試起來更容易。

也許你忘了設置config.action_mailer.perform_deliveries = trueconfig/environments/development.rb

+0

謝謝,但即使添加perform_deliveries設置和raise_delivery_errors標誌郵件沒有發送。我已經使用控制檯消息更新了我的帖子,該消息是在點擊「發送重置密碼指令」後打印的,該消息應該將電子郵件發送到提供的電子郵件。 – kofhearts

0

你試過嗎?

config.action_mailer.default_url_options = { :host => 'localhost:3000' } 
ActionMailer::Base.smtp_settings = { 
    :address    => "smtp.gmail.com", 
    :port     => 587, 
    :domain    => "gmail.com", 
    :user_name   => "[email protected]", 
    :password    => "secret", 
    :authentication  => "plain" 
    # :enable_starttls_auto => true # I don't have this, but it should work anyway 
} 

它它發送也許你不接受,因爲垃圾郵件過濾器的話,首先要檢查:

class UserMailer < ActionMailer::Base 
    default :from => "[email protected]" 
    # ... 
end 

您檢查您在您的Gmail Forwarding and POP/IMAP ..

包括您的這些代碼行development.rb

config.action_mailer.default_url_options = {host: 'your server' } # ex. localhost:3000 
config.action_mailer.raise_delivery_errors = true # to raise error if smtp has error on setup 
config.action_mailer.default :charset => "utf-8" 
+0

謝謝我補充說,也沒有工作。我也檢查了垃圾郵件,並且郵件不存在。 – kofhearts

+0

@ user734861好的..現在你必須啓用你「轉發和POP/IMAP」.. –

+0

我已經啓用了imap。我應該也啓用流行? – kofhearts

0
  1. 如果您正在使用Gmail,您應激活 的權限。外部應用程序通過SMTP發送電子郵件。
  2. 在發展模式Rails不發送電子郵件,你應該運行在生產rails s -e production導軌或打開config.action_mailer.raise_delivery_errors = true
  3. 不要忘記使用mail().deliver或爲Rails 5+ mail().delivery_now

你看這是我的SMTP配置

config.action_mailer.delivery_method = :smtp 
    config.action_mailer.smtp_settings = { 
    :address    => ENV['mailer_address'], 
    :port     => ENV['mailer_port'], 
    :user_name   => ENV['mailer_username'], 
    :password    => ENV['mailer_password'], 
    :authentication  => "plain", 
    :enable_starttls_auto => true 
    } 

這是我的班級郵編

def send_email 
mail(to: "[email protected]", subject: 'not reply this message').deliver 
end 

我從我的控制器調用該函數。如果你有任何錯誤,你應該打印它來檢查是什麼問題。

begin 
    myMailer.send_email 
rescue Exception => e 
    flash[:error] = "Imposible sent email, #{e.inspect}" 
end 
+0

謝謝傑夫,但我已啓用pop,imap,並啓用不太安全的應用程序來訪問Gmail的權限。仍然使用上述設置,電子郵件未收到。 – kofhearts

+0

你可以創建一個郵件程序測試它是否正在工作嗎?,你正在使用哪個Rails版本? – Jeff

0

啓用不夠安全的應用在我的帳戶設置爲我工作。

谷歌帳戶設置>登錄到谷歌>向下滾動到下

打開允許安全性較低的應用程序。嘗試再次發送電子郵件。