2015-10-03 82 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

相关问题