2015-01-10 30 views
0

幾周前,我創建了一個郵件程序對象,在創建新「Thing」時發送電子郵件。它工作順利,但我後來評論了相關的路線,並沒有處理它一段時間。今天我又試了一次,什麼都沒有發生。我發現我使用的電子郵件帳戶由於某種原因已被斷開連接,所以我用另一個電子郵件帳戶嘗試過。依然沒有。有沒有人猜測爲什麼它可能沒有做任何事情,或者我可以如何調試它?我確定用戶名/密碼/域名是準確的。Mailer在切換電子郵件後停止工作

配置/環境/ development.rb

config.action_mailer.delivery_method = :smtp 
config.action_mailer.smtp_settings = { 
    address: 'smtp.gmail.com', 
    port: 587, 
    domain: 'gmail.com', 
    user_name: 'username', 
    password: 'password', 
    authentication: 'plain', 
    enable_starttls_auto: true 
} 

應用程序/郵寄者/ user_mailer.rb

class UserMailer < ActionMailer::Base 
    default from: '[email protected]' 

    def notify(user) 
    @user = user 
    mail(to: @user.email,subject: "Notification") 
    end 
end 

應用/視圖/ user_mailer文件/ notify.html.erb

<html> 
    <head> 
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' /> 
    </head> 
    <body> 
    <h1>Heading</h1> 
    <p>First paragraph</p> 
    <p>Second paragraph</p> 
    </body> 
</html> 

應用程序/控制器/ users_controller.rb

def create 
    #... 
    UserMailer.notify(@user).deliver 
end 
+0

待辦事項你有一個測試/規範來驗證電子郵件是否被髮送,並且內容/屬性如預期的那樣?這是驗證郵件程序功能正在工作的更好方法;比嘗試手動觸發電子郵件更好。這可能是電子郵件仍在發送,但本地的一些smtp問題正在壓制它。另外,郵件程序的功能是否適用於生產? –

+0

也許你必須在電子郵件配置中啓用對你的應用程序的訪問,爲什麼你不試試? http://stackoverflow.com/a/26374624/3851249 –

+0

爲交付添加爆炸符號,並讓我們知道,錯誤是什麼?和郵件不會去垃圾箱右.. ..? – Nithin

回答

1

試試這個:

config.action_mailer.delivery_method = :smtp 
config.action_mailer.default_url_options = { host:'localhost', port: '3000' } 
config.action_mailer.perform_deliveries = true 
config.action_mailer.raise_delivery_errors = true 
config.action_mailer.default :charset => "utf-8" 
config.action_mailer.smtp_settings = { 
    address: 'smtp.gmail.com', 
    port: 587, 
    domain: 'localhost:3000', 
    user_name: '[email protected]', 
    password: 'password', 
    authentication: 'plain', 
    enable_starttls_auto: true 
} 
+0

它的工作原理,謝謝!但是我不明白我的代碼在那之前是如何工作的,如果我錯誤地填充了域和user_name字段...... –

0

如果你是在開發測試ENV,下面添加行,並檢查
config.action_mailer.perform_deliveries = true config.action_mailer.raise_delivery_errors = true

+0

我可以在我的environments/development.rb中的任何地方添加這些行嗎? –

+0

是的,在有權訪問(或訪問配置範圍)變量的地方添加這些行。 –

+0

我應該檢查什麼? –

相關問題