【问题标题】:Rails 5 app deployed to heroku not sending sendgrid emails in production部署到heroku的Rails 5应用程序不在生产中发送sendgrid电子邮件
【发布时间】:2017-09-03 15:40:10
【问题描述】:

昨天和今天早上都在试图弄清楚发生了什么。我有一个简单的 Rails 5 应用程序使用 sendgrid 从开发 rails 控制台成功发送电子邮件,但在 heroku 部署的生产 rails 控制台中尝试相同的事情不会发送电子邮件但会生成以下错误:

Errno::ECONNREFUSED: Connection refused - connect(2) for "localhost" port 25
from (irb):1

更多细节:

  • 红宝石 2.3.3
  • 导轨 5.1.3
  • gem 'sendgrid-ruby', '~> 5.0'

config/environment.rb

# Load the Rails application.
require_relative 'application'                                

# Initialize the Rails application.                           
Rails.application.initialize!                                 

Invoicer::Application.configure do 
  # Setup the mailer config                                   
  config.action_mailer.delivery_method = :smtp                
  config.action_mailer.perform_deliveries = true              
  config.action_mailer.smtp_settings = {
    :user_name => ENV['SENDGRID_USERNAME'],                   
    :password => ENV['SENDGRID_PASSWORD'],                    
    :domain => '[myapp].herokuapp.com',
    :address => 'smtp.sendgrid.net',                          
    :port => 587,
    :authentication => :plain,                                
    :enable_starttls_auto => true    
  }
end

config/environments/development.rb

唯一与电子邮件相关的未注释代码是:

# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false

config.action_mailer.perform_caching = false

config/environments/production.rb

唯一没有评论的电子邮件相关行是:

config.action_mailer.perform_caching = false

在我的本地机器上进行开发时,会生成一封电子邮件。

在开发 Rails 控制台中:

>> ActionMailer::Base.delivery_method
=> :smtp
>> InvoiceMailer.invoice_email(Customer.first).delivery_method
  Customer Load (0.6ms)  SELECT  "customers".* FROM "customers" ORDER BY "customers"."id" ASC LIMIT $1  [["LIMIT", 1]]
  Rendering invoice_mailer/invoice_email.html.erb within layouts/mailer
  Rendered invoice_mailer/invoice_email.html.erb within layouts/mailer (0.0ms)
  Rendering invoice_mailer/invoice_email.text.erb within layouts/mailer
  Rendered invoice_mailer/invoice_email.text.erb within layouts/mailer (0.0ms)
InvoiceMailer#invoice_email: processed outbound mail in 17.6ms
=> #<Mail::SMTP:0x007f939b607b48 @settings=    {:address=>"smtp.sendgrid.net", :port=>587, :domain=>"fm-    invoicer.herokuapp.com", :user_name=>"app75430225@heroku.com", :password=>"ms9nps8r7677", :authentication=>:plain, :enable_starttls_auto=>true, :openssl_verify_mode=>nil, :ssl=>nil, :tls=>nil}>

Heroku 生产没有发送电子邮件,我收到一个错误。

在 heroku rails 控制台中

$ heroku run rails c
Loading production environment (Rails 5.1.3)
irb(main):001:0> InvoiceMailer.invoice_email(Customer.first).deliver
D, [2017-09-03T14:59:53.803069 #4] DEBUG -- :Customer Load (7.2ms)      SELECT  "customers".* FROM "customers" ORDER BY "customers"."id" ASC LIMIT     $1  [["LIMIT", 1]]
I, [2017-09-03T14:59:53.863967 #4]  INFO -- :Rendering     invoice_mailer/invoice_email.html.erb within layouts/mailer
I, [2017-09-03T14:59:53.864680 #4]  INFO -- :Rendered invoice_mailer/invoice_email.html.erb within layouts/mailer (0.6ms)
I, [2017-09-03T14:59:53.865742 #4]  INFO -- :Rendering invoice_mailer/invoice_email.text.erb within layouts/mailer
I, [2017-09-03T14:59:53.866329 #4]  INFO -- :Rendered invoice_mailer/invoice_email.text.erb within layouts/mailer (0.4ms)
D, [2017-09-03T14:59:54.087932 #4] DEBUG -- : InvoiceMailer#invoice_email: processed outbound mail in 232.7ms
I, [2017-09-03T14:59:54.101584 #4]  INFO -- : Sent mail to XXX@gmail.com (13.4ms)
D, [2017-09-03T14:59:54.102047 #4] DEBUG -- : Date: Sun, 03 Sep 2017 14:59:54 +0000
From: XXX@gmail.com
To: XXX@gmail.com
Message-ID: <59ac18ea16bd6_451d0d888799@f74404ed-7ea9-41ff-9b05-715d2a185bf4.mail>
Subject: XXXXX Website Monthly Invoice - August-2017
Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="--==_mimepart_59ac18ea151a7_451d0d8886dd";
 charset=UTF-8
Content-Transfer-Encoding: 7bit


----==_mimepart_59ac18ea151a7_451d0d8886dd
Content-Type: text/plain; 
 charset=UTF-8
Content-Transfer-Encoding: 7bit

Dear XXX

Attached please find your monthly invoice.

Best,
XXX
CrazyJ Media ;-)


----==_mimepart_59ac18ea151a7_451d0d8886dd
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style>
      /* Email styles need to be inline */
    </style>
  </head>

  <body>
    <!DOCTYPE html>
<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  </head>
  <body>
    <p>Dear XXX,</p>
    <p>Attached please find the latest monthly invoice.</p>
    <p>Best,<br>XXX<br>Crazy J Media</p>
  </body>
</html>

  </body>
</html>

----==_mimepart_59ac18ea151a7_451d0d8886dd--

Errno::ECONNREFUSED: Connection refused - connect(2) for "localhost" port 25
    from (irb):1

也在 Heroku Rails 控制台中:

irb(main):001:0> ActionMailer::Base.delivery_method
=> :smtp
irb(main):002:0>     InvoiceMailer.invoice_email(Customer.first).delivery_method
D, [2017-09-13T22:42:58.951166 #4] DEBUG -- :   Customer Load (2.8ms)      SELECT  "customers".* FROM "customers" ORDER BY "customers"."id" ASC LIMIT     $1  [["LIMIT", 1]]
I, [2017-09-13T22:42:59.037206 #4]  INFO -- :   Rendering     invoice_mailer/invoice_email.html.erb within layouts/mailer
I, [2017-09-13T22:42:59.038316 #4]  INFO -- :   Rendered     invoice_mailer/invoice_email.html.erb within layouts/mailer (0.9ms)
I, [2017-09-13T22:42:59.039720 #4]  INFO -- :   Rendering  invoice_mailer/invoice_email.text.erb within layouts/mailer
I, [2017-09-13T22:42:59.040359 #4]  INFO -- :   Rendered invoice_mailer/invoice_email.text.erb within layouts/mailer (0.5ms)
D, [2017-09-13T22:42:59.413839 #4] DEBUG -- :     InvoiceMailer#invoice_email: processed outbound mail in 405.7ms
=> #<Mail::SMTP:0x0000000575c678 @settings={:address=>"localhost",   :port=>25, :domain=>"localhost.localdomain", :user_name=>nil, :password=>nil, :authentication=>nil, :enable_starttls_auto=>true, :openssl_verify_mode=>nil, :ssl=>nil, :tls=>nil}>

有人有什么想法吗?我研究了Heroku's sendgrid ruby docs 以及几个 SO 问题和答案,例如this one,但无济于事。我已经重新启动了我的 heroku 服务器,并且链接到此应用程序的 sendgrid 帐户正在工作并记录从开发发送的电子邮件。

许多正面的业力会提前发送给任何有见识的人!

【问题讨论】:

  • 确定,您在 heroku 仪表板中设置了 sendgrid 变量?
  • 非常肯定,@RockwellRice。 heroku sendgrid 插件与该应用程序相关联,已存储用户名和密码凭据。我可以通过应用程序的 heroku 仪表板访问我的应用程序的 sendgrid 仪表板。
  • 您是否在开发中添加了config.action_mailer.default_url_options = { :host =&gt; 'localhost:3000' },在生产中添加了config.action_mailer.default_url_options = { :host =&gt; 'myapp.herokuapp.com' }?不过,这只是一个疯狂的猜测。
  • 您可以运行ActionMailer::Base.delivery_methodActionMailer::Base.smtp_settings 来验证您的设置。如果没有问题,请检查电子邮件实例 InvoiceMailer.invoice_email(Customer.first).delivery_method 上的设置。您可以使用结果更新您的问题。
  • 谢谢,@MichałMłoźniak,我已经用该信息更新了我的问题。现在也会尝试其他建议。

标签: ruby-on-rails email heroku sendgrid


【解决方案1】:

错误表明它被配置为使用 SMTP 服务器在 localhost 端口 25 发送电子邮件,这表明您的配置没有被使用!

Rails.application.initialize! 在生产环境中运行时,ActionMailer 会被预先加载并使用当时提供的设置进行配置。在您的情况下,这似乎意味着它正在使用默认值,因为您自己的配置要到稍后才会设置。

您可以通过在初始化应用程序之前简单地设置配置来解决此问题;要么重新排序你的 environment.rb 以便首先进行配置,要么将 ActionMailer 配置移动到 application.rb 或特定于环境的配置(例如,environments/production.rb)。

【讨论】:

  • 谢谢你,@jpn。您诊断出问题并提供了治疗方法。现在,在将配置代码移至 environment/production.rb 和 environment/development.rb 后,可以在开发和生产中发送电子邮件。将看看我是否可以通过在 application.rb 中证明一次或在 environment.rb 中证明代码的顺序来干燥我的代码
【解决方案2】:

您可能应该将 Action Mailer 配置移动到 config/environments/production.rb 和/或检查此文件中是否没有覆盖您的设置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-27
    • 2017-06-15
    • 1970-01-01
    • 2015-02-08
    • 1970-01-01
    • 2019-12-12
    • 1970-01-01
    • 2020-09-22
    相关资源
    最近更新 更多