【问题标题】:What does the outgoing SMTP request from ActionMailer look like?ActionMailer 发出的 SMTP 请求是什么样的?
【发布时间】:2019-06-19 20:53:42
【问题描述】:

一些背景知识:我正在开发一个应用程序,该应用程序通过 Ruby on Rails 中的 ActionMailer 发送电子邮件,但我需要我的 Heroku 托管应用程序位于静态 IP 范围后面,以便被它发送的 SMTP 中继服务器列入白名单.为此,我包含了一个名为 Fixie 的 Heroku 插件,它为我的应用程序提供了一个静态 IP 范围,并且可以通过 Faraday 等 gem 配置请求。

我的主要问题是:当我调用#mail 时,如何找出和/或记录 ActionMailer 发出的传出请求?我想知道这个请求是在哪里发出的以及更多关于它的信息,以便我可以使用 Faraday gem 发出这个请求,它支持使用 Fixie 的代理 IP。

编辑:经过更多研究,我似乎需要设置一个 SOCKS5 代理而不是一个简单的 HTTP(S) 代理,因为这个出站请求是使用 SMTP 完成的。不过,我仍然不确定我原来的问题。

谢谢!

【问题讨论】:

  • 嘿@jj57347,你找到解决方案了吗?我有完全同样的问题。如果您能提供帮助,我将不胜感激!

标签: ruby-on-rails ruby smtp actionmailer faraday


【解决方案1】:

要在 Heroku 上设置动作邮件程序,我的标准配置如下所示

# config/environments/production.rb
  config.action_mailer.default_url_options = { host: 'https://www.example.com' }
  config.action_mailer.perform_deliveries = true
  ActionMailer::Base.smtp_settings = {
    :address => 'smtp.sendgrid.net',
    :port => '587',
    :authentication => :plain,
    :user_name => ENV['SENDGRID_USERNAME'],
    :password => ENV['SENDGRID_PASSWORD'],
    :domain => 'heroku.com'
  }
  ActionMailer::Base.delivery_method = :smtp

现在我在 SENDGRID 上使用 Heroku 广告,它会将 SENDGRID_USERNAMESENDGRID_PASSWORD 添加到您的配置变量中

现在我没有在邮件上做任何特别的事情,但我会为示例添加一个

# app/mailers/example_mailer.rb
class ExampleMailer < ActionMailer::Base
  default from: 'donotreply@example.com'
  default cc:   'cc@example.com'
  default bcc:  'bcc@example.com'

  layout 'mailer'

  def some_email(example_id)
    @example = Example.find(example_id)
    mail(
        :to =>   'user@example.com',
        :subject =>" SUBJECT ",
        :body => "This can and should be moved to a view like app/view/example_mailr/some_email.html.erb"
    )
  end
end

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-02
    • 2017-10-13
    • 1970-01-01
    • 2011-02-02
    • 1970-01-01
    • 2017-04-12
    • 1970-01-01
    相关资源
    最近更新 更多