【问题标题】:Do I need to provide a "from" email address while using Heroku + SendGrid?使用 Heroku + SendGrid 时是否需要提供“发件人”电子邮件地址?
【发布时间】:2014-09-09 09:52:18
【问题描述】:

我在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  
  }

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

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)

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

【问题讨论】:

    标签: ruby-on-rails email heroku sendgrid


    【解决方案1】:

    是的,您需要设置一个发件人地址。所有电子邮件都需要指定发件人地址。

    :from => "address_you_are_sending_from@your_domain.com"
    

    这需要在您的 _mailer.rb 文件中设置。您也可以在初始化程序中全局设置它,但必须始终在 your_mailer.rb 文件和每个邮件方法本身中引用它。

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

    【讨论】:

    • 您的意思是我必须注册一个 gmail 帐户并将其用作电子邮件吗?但如果是这样,那么我如何提供 gmail 用户名和密码?
    【解决方案2】:

    您可以在邮件文件中设置发件人地址。

    例如,如果您有一个 admin_mailer.rb 用于通知您网站上的新活动您的个人电子邮件,它可能如下所示:

      def new_customer_trial(customer_object)
        @new_customer = customer_object
        mail :to => "me@personal_email.com", :subject => "New Customer Trial", :from => "'My Site Notifications' <notifications@my_site.com>"
      end
    

    你会从控制器发送邮件:

    AdminMailer.new_customer_trial(customer_object)
    

    或作为延迟工作:

    AdminMailer.delay.new_customer_trial(customer_object)
    

    【讨论】:

      【解决方案3】:

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

      config.action_mailer.default_options = { from: "my_address@example.com" }
      

      【讨论】:

      • 有效。谢谢!
      猜你喜欢
      • 2019-10-29
      • 2011-02-14
      • 2016-01-21
      • 1970-01-01
      • 1970-01-01
      • 2013-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多