加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 部署到heroku的Rails 5应用程序不会在生产中发

发布时间:2020-12-17 03:10:47 所属栏目:百科 来源:网络整理
导读:花了昨天和今天早上试图弄清楚发生了什么.我有一个简单的Rails 5应用程序使用sendgrid从开发rails控制台成功发送电子邮件,但在heroku部署生产rails控制台尝试相同的事情不会发送电子邮件,但会生成以下错误: Errno::ECONNREFUSED: Connection refused - conn
花了昨天和今天早上试图弄清楚发生了什么.我有一个简单的Rails 5应用程序使用sendgrid从开发rails控制台成功发送电子邮件,但在heroku部署生产rails控制台尝试相同的事情不会发送电子邮件,但会生成以下错误:

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

更多细节:

>ruby2.3.3
>轨道5.1.3
> gem’sentgrid-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

配置/环境/ development.rb

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

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

config.action_mailer.perform_caching = false

配置/环境/ production.rb

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

config.action_mailer.perform_caching = false

在本地计算机上进行开发时会生成一封电子邮件.

在Development 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",[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,:tls=>nil}>

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

事先向任何有见识的人发送了很多积极的业力!

解决方法

该错误显示它已配置为使用localhost端口25上的SMTP服务器发送电子邮件,这表示您的配置未被使用!

当Rails.application.initialize!在生产中运行,ActionMailer是急切加载的,并配置了当时给出的设置.在您的情况下,它看起来意味着它使用默认值,因为您自己的配置直到稍后才设置.

您可以通过在初始化应用程序之前设置配置来解决此问题;要么重新命令你的environment.rb,要么首先发生配置,要么将ActionMailer配置移动到application.rb或环境特定的配置(例如,environment / production.rb).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读