首页 文章

在Heroku上使用MailGun for ruby on rails应用程序

提问于
浏览
2

我正在尝试使用MailGun在我的RubyonRails应用程序中发送电子邮件,该应用程序托管在heroku上 .

我将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”#layout'mailer'def notify_user(user)puts“Going to send email !! to”put user mail(to:user,subject:“Welcome! “)发出”完成发送邮件!“返回结束

1 回答

  • 2

    使用 .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以了解正在发生的事情 .

    切向,而不是打印语句看看像byebug和pry-byebug这样的调试器 . 它们可以为您节省大量的故障排除时间 .

相关问题