2015-10-03 148 views
2

我想用MailGun在Heroku上託管的RubyonRails應用程序中發送電子郵件。在Heroku上使用MailGun for Rails應用程序上的應用程序

我將MailGun插件添加到我的heroku應用程序中。我下面更新我的config/environments/production.rb

config.action_mailer.delivery_method = :smtp 
    config.action_mailer.smtp_settings = { 
    :port   => ENV['MAILGUN_SMTP_PORT'], 
    :address  => ENV['MAILGUN_SMTP_SERVER'], 
    :user_name  => ENV['MAILGUN_SMTP_LOGIN'], 
    :password  => ENV['MAILGUN_SMTP_PASSWORD'], 
    :domain   => 'mydomain.herokuapp.com', #mydomain actually contains the realvalue 
    :authentication => :plain, 
    } 

我創建了應用程序/郵寄目錄中的文件:

class ApplicationMailer < ActionMailer::Base 
    default from: "[email protected]" 

    def notify_user(user) 
    puts "Going to send email!! to" 
    puts user 
    mail(to: user, subject: "Welcome!") 
    end 
end 

在我application_controller.rb

我有一個函數:

def notify_people() 
    puts "Notify people!" 
    ApplicationMailer.notify_user("[email protected]") 
    puts "over" 
end 

從我的其他控制器之一,我打電話給notify_people。但是,在我的heroku日誌中,通知人們!正在印刷中,之後沒有任何內容。我無法理解爲什麼它無法調用notify_user。

我notify_user功能是:

類ApplicationMailer <的ActionMailer ::從基地 默認: 「[email protected]」 #layout '郵件' 高清notify_user(用戶) 看跌期權「將發送電子郵件!!到「 把用戶 郵件(到:用戶,主題:」歡迎!「) 放入」完成發送郵件!「 回報 結束 結束

回答

2

呼叫與.deliver_later的郵件:

ApplicationMailer.notify_user("[email protected]").deliver_later 

還,確保ENV variables are set on Heroku使用細節從Mailgun帳戶。

編輯:你可能會從電話ApplicationMailer得到一個例外。理想情況下,您應該在開發過程中排除故障,但如果需要,您可以將:config.action_mailer.raise_delivery_errors = true添加到production.rb中以瞭解發生的情況。

切線,而不是打印語句看看調試器,如byebug和pry-byebug。他們可以爲您節省大量的時間。

+0

編輯了一些其他的建議:) – thebenedict

+0

我添加了你在我的production.rb文件中提到的命令。我的heroku日誌中沒有出現任何錯誤。但是,在打印完郵件之後,它看起來會失敗,因爲沒有任何郵件功能。 – user1692342

+0

您是否能夠從應用程序發送電子郵件?你是否用'deliver_later'來呼叫郵件?你在Heroku上面設置了4個環境變量嗎?你能確認'notify_user'被執行嗎? – thebenedict

相關問題