Rails - 无法使用mailgun发送生产电子邮件

时间:2017-03-06 12:19:13

标签: ruby-on-rails heroku mailgun

我在heroku中部署了一个应用程序,邮件仅在开发中成功发送,但未在生产中发送。我使用的是mailgun。以下是错误结果:

Exception
Net::OpenTimeout
Error
execution expired
/app/vendor/ruby-2.2.6/lib/ruby/2.2.0/net/smtp.rb:541:in `initialize'
/app/vendor/ruby-2.2.6/lib/ruby/2.2.0/net/smtp.rb:541:in `open'
/app/vendor/ruby-2.2.6/lib/ruby/2.2.0/net/smtp.rb:541:in `tcp_socket'
/app/vendor/ruby-2.2.6/lib/ruby/2.2.0/net/smtp.rb:551:in `block in do_start'
/app/vendor/ruby-2.2.6/lib/ruby/2.2.0/timeout.rb:88:in `block in timeout'
/app/vendor/ruby-2.2.6/lib/ruby/2.2.0/timeout.rb:98:in `call'
/app/vendor/ruby-2.2.6/lib/ruby/2.2.0/timeout.rb:98:in `timeout'
/app/vendor/ruby-2.2.6/lib/ruby/2.2.0/net/smtp.rb:550:in `do_start'
/app/vendor/ruby-2.2.6/lib/ruby/2.2.0/net/smtp.rb:520:in `start'
/app/vendor/bundle/ruby/2.2.0/gems/mail-2.6.4/lib/mail/network/delivery_methods/smtp.rb:113:in `deliver!'
/app/vendor/bundle/ruby/2.2.0/gems/mail-2.6.4/lib/mail/message.rb:253:in `deliver!'
/app/vendor/bundle/ruby/2.2.0/gems/actionmailer-4.2.5/lib/action_mailer/message_delivery.rb:77:in `deliver_now!'
/app/vendor/bundle/ruby/2.2.0/gems/actionmailer-4.2.5/lib/action_mailer/delivery_job.rb:10:in `perform'
/app/vendor/bundle/ruby/2.2.0/gems/activejob-4.2.5/lib/active_job/execution.rb:32:in `block in perform_now'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:117:in `call'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:117:in `call'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:505:in `call'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:505:in `call'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:498:in `block (2 levels) in around'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:343:in `call'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:343:in `block (2 levels) in simple'
/app/vendor/bundle/ruby/2.2.0/gems/i18n-0.8.1/lib/i18n.rb:257:in `with_locale'

和我的config / environments / production.rb代码

config.action_mailer.delivery_method = :smtp
    ActionMailer::Base.smtp_settings = 
   {

     :address              => 'mailgun.org',
     :port                 => ENV['MAILGUN_SMTP_PORT'],
     :domain               => ENV['MY_DOMAIN'],
     :user_name            => ENV['MAILGUN_SMTP_LOGIN'],
     :password             => ENV['MAILGUN_SMTP_PASSWORD'],
     :authentication       => 'plain'
   }

  config.action_mailer.default_url_options = { :host => 'https://myapp.herokuapp.com/' }
  config.action_mailer.asset_host = 'https://myapp.herokuapp.com/'
  config.action_controller.asset_host = 'https://myapp.herokuapp.com/'

1 个答案:

答案 0 :(得分:0)

好吧,我没注意到我只使用'mailgun.org'代替'smtp.mailgun.org'作为地址。我将地址值更改为ENV ['MAILGUN_SMTP_SERVER'],然后它在生产中正常工作。