【问题标题】:How to set up mailer in Rails app for production environment on Heroku如何在 Rails 应用程序中为 Heroku 上的生产环境设置邮件程序
【发布时间】:2012-08-13 09:28:07
【问题描述】:

我需要使用邮件程序向用户发送电子邮件,以将他们的密码设置为设计和活动管理员的“可恢复”功能。在开发环境中,我通过将以下内容添加到这些文件中来做到这一点:

配置/环境/开发

#Added per active admin install instructions
config.action_mailer.default_url_options = { :host => 'localhost:3000' }


#These settings are for the sending out email for active admin and consequently the   devise mailer
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.smtp_settings = 
{

  :address            => 'smtp.gmail.com',
  :port               => 587,
  :domain             => 'gmail.com', #you can also use google.com
  :authentication     => :plain,
  :user_name          => 'XXXXX@gmail.com',
  :password           => 'XXXXXXX'
}

如何在生产环境中获得相同的功能?我想将我的应用程序部署到 Heroku。我需要添加哪些文件和代码?

【问题讨论】:

  • 您需要在 config/environments/production.rb 中使用相同的代码
  • 如果所有环境都相同,则将其放在 config/application.rb 中
  • 是的,但是我应该在生产中为此添加什么:config.action_mailer.default_url_options = { :host => '????' }
  • 你的应用程序的 URL config.action_mailer.default_url_options = { :host => 'YOURAPPNAME.herokuapp.com' } 或 'yourcustomdomain.com' 如果使用一个

标签: ruby-on-rails heroku devise actionmailer production


【解决方案1】:

您在开发模式下设置的所有配置都将起作用,除非您需要重新配置默认邮件程序 url。

所以。

  1. 从 development.rb 复制粘贴您的设置。

  2. 将您的默认邮件程序指向您的 heroku 应用程序:

    config.action_mailer.default_url_options = { :host => 'YOURAPPNAME.herokuapp.com' }
    

此外,请注意您的 smtp 在迁移到生产环境时可能存在的任何电子邮件限制。例如,在开发过程中很难触发 gmail 的 smtp 限制,但在生产环境中可能更容易触发。

【讨论】:

    【解决方案2】:

    如果它在开发模式下工作,那么它将在生产模式下工作。

    假设一切设置正确,在开发中重置密码将已经使用您的 gmail 帐户发送实际电子邮件。

    Devise 仅依赖于正确的邮件配置设置(您已经完成),以及配置 devise 以允许密码重置,以及电子邮件的 From 字段可能的另一个设置。

    【讨论】:

    • 是的,但是我应该在生产中为此添加什么:config.action_mailer.default_url_options = { :host => '????' }
    • 你需要把你的应用程序的主机url,例如production.rbconfig.action_mailer.default_url_options = { host: 'my-app.herokuapp.com' }
    【解决方案3】:

    这应该可以正常工作!

    只要 config/environments/production.rb 有相同的东西,但有例外。 default_url_options 的 :host 值应仅在开发中为“localhost”,在 heroku 生产中应为“YOURAPPNAME.herokuapp.com”。

    config.action_mailer.default_url_options = { :host => 'YOURAPPNAME.herokuapp.com' }
    

    记得在 gmail 上解锁验证码,否则它不会从 heroku 发送电子邮件(来源不明)。你可以通过这个链接做到这一点:http://www.google.com/accounts/DisplayUnlockCaptcha

    作为一个建议,我会说从environments.rb中移出这个

    ActionMailer::Base.perform_deliveries = true
    ActionMailer::Base.raise_delivery_errors = true
    

    地点在environments/development.rb中

    config.action_mailer.perform_deliveries = true
    config.action_mailer.raise_delivery_errors = true
    

    在生产中不需要。

    有关 gmail 将 heroku 视为未知主机的更多信息,请参阅 Net::SMTPAuthenticationError when sending email from Rails app (on staging environment)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-18
      • 2016-06-21
      • 1970-01-01
      • 2021-12-23
      • 2012-10-09
      • 1970-01-01
      • 2017-08-31
      • 2013-03-06
      相关资源
      最近更新 更多