簡體   English   中英

將 MailGun 用於 Heroku 上的 ruby​​ on rails 應用程序

[英]Using MailGun for ruby on rails application on Heroku

我正在嘗試使用 MailGun 在我托管在 heroku 上的 RubyonRails 應用程序中發送電子郵件。

我將 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,
  }

我在 app/mailers 目錄中創建了一個文件:

class ApplicationMailer < ActionMailer::Base
  default from: "myemail@gmail.com"

  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("xyz@gmail.com")
    puts "over"
end

我從我的其他控制器之一調用 notify_people。 但是在我的 heroku 日志中,通知人們! 正在打印,之后什么都沒有。 我不明白為什么它不能調用notify_user。

我的 notify_user 函數是:

class ApplicationMailer < ActionMailer::Base default from: "abcd@gmail.com" #layout 'mailer' def notify_user(user) puts "Going to send email!! to" puts user mail(to: user, subject: "Welcome! ") 輸入 "完成發送郵件!" 返回結束結束

使用.deliver_later調用郵件程序:

ApplicationMailer.notify_user("xyz@gmail.com").deliver_later

此外,請確保使用您的 Mailgun 帳戶中的詳細信息在 Heroku 上設置 ENV 變量

編輯:您可能在調用ApplicationMailer遇到異常。 理想情況下,您應該在開發中排除故障,但如果需要,您可以添加: config.action_mailer.raise_delivery_errors = true到 production.rb 以了解正在發生的事情。

切線地,而不是打印語句來看看調試器,如 byebug 和pry-byebug 它們可以為您節省大量的故障排除時間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM