在 Heroku 上的 rails 应用程序上为 ruby 使用 MailGun

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 默认来自:"abcd@gmail.com" #布局'mailer' def notify_user(用户) 放 "Going to send email!! to" 把用户 邮件(给:用户,主题:"Welcome!") 放 "Done sending mail!" return 结尾 结束

使用 .deliver_later 调用邮件程序:

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

此外,请确保 ENV variables are set on Heroku 使用您的 Mailgun 帐户中的详细信息。

编辑:您可能在调用 ApplicationMailer 时遇到异常。理想情况下,您应该在开发中解决该问题,但如果需要,您可以添加:config.action_mailer.raise_delivery_errors = true 到 production.rb 以了解正在发生的事情。

切线地,不要使用 print 语句,而是查看像 byebug 和 pry-byebug 这样的调试器。它们可以为您节省大量的故障排除时间。