使用Heroku + SendGrid时是否需要提供"发件人"电子邮件地址?

Hei*_*erg 2 email ruby-on-rails heroku sendgrid

我有标准设置 config/environments/production.rb

  config.action_mailer.default_url_options = { host: "myapp.heroku.com" }
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    :addresses            => 'smtp.sendgrid.net',
    :port                 => '587',
    :authentication       => :plain,
    :user_name            => ENV['SENDGRID_USERNAME'],
    :password             => ENV['SENDGRID_PASSWORD'],
    :domain               => 'heroku.com',
    :enable_starttls_auto => true  
  }
Run Code Online (Sandbox Code Playgroud)

但是,我在尝试发送邮件时遇到以下错误:

ArgumentError: An SMTP From address is required to send a message. Set the message smtp_envelope_from, return_path, sender, or from address.

然后我尝试使用我在heroku附加页面上找到的电子邮件,即app######@heroku.com,但现在我得到了

Errno::ECONNREFUSED: Connection refused - connect(2)

那么我是否需要指定来自电子邮件?如果是,我应该使用哪一个?

mmi*_*ike 9

在您的production.rb文件中尝试此代码:

config.action_mailer.default_options = { from: "my_address@example.com" }
Run Code Online (Sandbox Code Playgroud)


hea*_*iti 7

是的,你需要设置一个起始地址.所有电子邮件都需要指定的发件人地址.

:from => "address_you_are_sending_from@your_domain.com"
Run Code Online (Sandbox Code Playgroud)

这需要在_mailer.rb文件中设置.您也可以gloablly设置在initiallizer,但它必须始终your_mailer.rb文件来引用,并且每个邮件的方法本身.

您应该将:域更改为您控制的域.但是,如果您使用Heroku,您的应用程序地址不是yourapp.herokuapp.com而不是yourapp.heroku.com?