【问题标题】:Net::OpenTimeout exception when sending mail using Sendgrid from Rails 4 app on Google Compute Engine使用 Sendgrid 从 Google Compute Engine 上的 Rails 4 应用程序发送邮件时出现 Net::OpenTimeout 异常
【发布时间】:2015-06-15 13:59:47
【问题描述】:

我已经使用 Capistrano 将 Rails 4 应用程序部署到运行 Debian 7 的 Google Compute Engine 实例。我使用 Nginx 作为网络服务器,Passenger 作为应用程序服务器。对于发送电子邮件,我使用的是 Sendgrid,我的 staging.rb 文件中有以下内容

config.action_mailer.default_url_options = { host: 'smtp.sendgrid.net', port: 2525 }
config.action_mailer.delivery_method = :smtp

ActionMailer::Base.smtp_settings = {
    :user_name => '<my_sendgrid_username>',
    :password => '<my_sengrid_password>',
    :domain => '<ip address of my instance>',
    :address => 'smtp.sendgrid.net',
    :port => '2525',
    :authentication => 'plain',
    :enable_starttls_auto => true
  }

我还使用谷歌云控制台在我的实例的防火墙上打开了端口 2525。但是,当我的 rails 应用程序尝试发送电子邮件时,我得到 Net::OpenTimeout 异常。下面是异常跟踪的一部分

Net::OpenTimeout (execution expired):
  /home/jimcgh/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/smtp.rb:541:in `initialize'
  /home/jimcgh/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/smtp.rb:541:in `open'
  /home/jimcgh/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/smtp.rb:541:in `tcp_socket'
  /home/jimcgh/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/smtp.rb:551:in `block in do_start'
  /home/jimcgh/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/timeout.rb:89:in `block in timeout'
  /home/jimcgh/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/timeout.rb:99:in `call'
  /home/jimcgh/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/timeout.rb:99:in `timeout'
  /home/jimcgh/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/smtp.rb:550:in `do_start'
  /home/jimcgh/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/smtp.rb:520:in `start'
  mail (2.6.3) lib/mail/network/delivery_methods/smtp.rb:112:in `deliver!'
  mail (2.6.3) lib/mail/message.rb:2141:in `do_delivery'
  mail (2.6.3) lib/mail/message.rb:236:in `block in deliver'
  actionmailer (4.2.1) lib/action_mailer/base.rb:543:in `block in deliver_mail'
  activesupport (4.2.1) lib/active_support/notifications.rb:164:in `block in instrument'
  activesupport (4.2.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.2.1) lib/active_support/notifications.rb:164:in `instrument'
  actionmailer (4.2.1) lib/action_mailer/base.rb:541:in `deliver_mail'
  mail (2.6.3) lib/mail/message.rb:236:in `deliver'
  actionmailer (4.2.1) lib/action_mailer/message_delivery.rb:85:in `deliver_now'
  devise (3.5.1) lib/devise/models/authenticatable.rb:170:in `send_devise_notification'
  devise (3.5.1) lib/devise/models/confirmable.rb:114:in `send_confirmation_instructions'
  devise (3.5.1) lib/devise/models/confirmable.rb:170:in `send_on_create_confirmation_instructions'
  activesupport (4.2.1) lib/active_support/callbacks.rb:432:in `block in make_lambda'
  activesupport (4.2.1) lib/active_support/callbacks.rb:228:in `call'
  activesupport (4.2.1) lib/active_support/callbacks.rb:228:in `block in halting_and_conditional'
  activesupport (4.2.1) lib/active_support/callbacks.rb:506:in `call'
  activesupport (4.2.1) lib/active_support/callbacks.rb:506:in `block in call'
  activesupport (4.2.1) lib/active_support/callbacks.rb:506:in `each'
  activesupport (4.2.1) lib/active_support/callbacks.rb:506:in `call'
  activesupport (4.2.1) lib/active_support/callbacks.rb:92:in `_run_callbacks'
  activesupport (4.2.1) lib/active_support/callbacks.rb:776:in `_run_create_callbacks'
  activerecord (4.2.1) lib/active_record/callbacks.rb:306:in `_create_record'
  activerecord (4.2.1) lib/active_record/timestamp.rb:57:in `_create_record'
  activerecord (4.2.1) lib/active_record/persistence.rb:502:in `create_or_update'

我已阅读 Google Compute Engine 网站上关于 Sending Emails 的文档,发现端口 25 上的传出被阻止,因此我使用 2525。我也尝试使用 :2525 和 :plain 而不是 '2525' 和 '在我的 staging.rb 中的 ActionMailer 设置中使用plain'。这可能是什么问题?

请帮忙 谢谢你

【问题讨论】:

  • 您有没有机会查看实例防火墙 (iptables..) 以检查端口是否被阻止?

标签: ruby-on-rails-4 nginx passenger google-compute-engine sendgrid


【解决方案1】:

你的配置不好。

config.action_mailer.default_url_options = { host: (it must be your host ie: 'localhost or www.domain.com', port: 3000(in production usually port is not needed')}

ActionMailer::Base.smtp_settings = {
    :address        => 'smtp.sendgrid.net',
    :port           => 587, #port used by smtp not yours
    :user_name      => ENV['SENDGRID_USERNAME'],
    :password       => ENV['SENDGRID_PASSWORD'],
    :enable_starttls_auto => true,
    :authentication => :plain
    :domain => 'domain.com' #your domain
  }

【讨论】:

  • 感谢 ppascualv 的回复。我进行了您指出的更改,但仍然收到 Net::OpenTimeout (execution expired) 错误。
【解决方案2】:

存在一个gem,它通过提供使用 SendGrid Web API 而不是 SMTP API 的 ActionMailer DeliveryMethod 来解决问题。

【讨论】:

    【解决方案3】:

    将此添加到您的配置中,基于https://github.com/mikel/mail/issues/639 tls: true

    【讨论】:

      猜你喜欢
      • 2014-12-10
      • 2014-05-19
      • 2012-09-17
      • 2020-05-14
      • 1970-01-01
      • 1970-01-01
      • 2017-05-03
      • 2019-09-01
      • 2014-12-07
      相关资源
      最近更新 更多