无法在生产中使用Action Mailer发送电子邮件

时间:2015-06-18 00:09:54

标签: ruby-on-rails heroku gmail actionmailer mandrill

我在使用带有Gmail的ActionMailer发送电子邮件时遇到了一些问题。我曾试图使用Mandrill,但它还没有发送。使用gmail测试ActionMailer时,它仅在开发中工作,但在生产中似乎存在问题。

我尝试了多种解决方案,但似乎没有任何效果。我已经尝试在environment.rb中设置动作邮件设置,从gmail交换到mandrill。我似乎无法弄清楚我错过了什么。

让我知道你的想法。我已在下面提供了代码段

production.rb:

config.action_mailer.default_url_options = { :host => ENV["HEROKU_DOMAIN"] }
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default :charset => "utf-8"
config.action_mailer.smtp_settings = {
  address: "smtp.mandrillapp.com",
  port: 587,
  domain: ENV["HEROKU_DOMAIN"],
  authentication: "plain",
  enable_starttls_auto: true,
  user_name: ENV["HEROKU_USERNAME"],
  password: ENV["MANDRILL_KEY"]
}

email_signup.rb:

class EmailSignup < ApplicationMailer
  default from: "example@gmail.com"

  def sample_email(user)
    @user = user

    mail subject: "test", to: user.email
  end
end

users_controller.rb:

def create
  @user = User.new(user_params)

  if @user.save
    @user.save
    EmailSignup.sample_email(@user).deliver
    redirect_to thanks_path
  else
    redirect_to root_path
  end
end

2 个答案:

答案 0 :(得分:0)

请尝试使用此配置,我认为default_url_optionssmtp domain name存在问题。你也可以省略其他配置。

config.action_mailer.default_url_options = { host: ENV['DEFAULT_URL_OPTIONS'] }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  address: 'smtp.mandrillapp.com',
  port: 587,
  domain: 'heroku.com',
  user_name: ENV['MANDRILL_USERNAME'],
  password: ENV['MANDRILL_APIKEY'],
  authentication: 'plain'
}
  

您的默认网址选项应该是服务器的完整域名,例如。 mytestapp.herokuapp.com

答案 1 :(得分:0)

Neo的解决方案解决了我的问题。

我所做的就是在Heroku上设置环境变量。

$ heroku config:set HEROKU_USERNAME = ...

和所有其他变量并修复它。

相关问题