25

我正在尝试创建一个自定义电子邮件标头以使用 SendGrid api。

这就是我正在做的事情 - 但它不起作用:

class Mailman < ActionMailer::Base
  default :from => "info@sample.com"

  def send_message(name, email, message)
    @name = name
    @email = email
    @message = message

    mail(:to => 'info@sample.com',
     :from => email,
     :subject => "Message from the site",
     :headers['X-SMTPAPI'] => "category: Drip Email"
    )
  end

end

任何帮助表示赞赏。

谢谢,亚当

4

4 回答 4

60

您可以使用 ActionMailer 的 #headers 方法,我编辑了您的示例以显示如何:

class Mailman < ActionMailer::Base
  default :from => "info@sample.com"

  def send_message(name, email, message)
    @name = name
    @email = email
    @message = message

    headers['X-SMTPAPI'] = '{"category": "Drip Email"}'

    mail(
     :to => 'info@sample.com',
     :from => email,
     :subject => "Message from the site"
    )
  end

end

或者,您也可以将哈希作为参数传递(给方法#headers):

headers {"SPECIFIC-HEADER-1" => "value", "ANOTHER-HEADER" => "and so..."}

我希望这可以帮助您,如果没有,您可以随时查看 rails 指南:http ://edgeguides.rubyonrails.org/action_mailer_basics.html 。

于 2011-08-10T15:00:12.313 回答
5

我正在使用下面的代码并且工作正常,只需将哈希转换为 jsonto_json

headers['X-SMTPAPI'] = { 
  category: "Weekly Newsletter",
  unique_args: { user_id: user.id } 
}.to_json
于 2016-07-08T15:07:05.443 回答
3

要在 sendgrid 的抑制组功能中使用取消订阅组,我使用了以下有效的语法。

headers['X-SMTPAPI'] = '{"asm_group_id": 1111}'
于 2017-01-11T08:40:34.743 回答
2

headers 方法需要有效的 JSON。所以里卡多的解决方案需要这条线:

headers['X-SMTPAPI'] = '{"category": "Drip Email"}'

于 2012-01-26T17:16:11.043 回答