Set the Message-ID mail header in Rails3 / ActionMailer

流过昼夜 提交于 2019-11-30 06:47:40

Teddy's answer is good, except that if you actually want each message to have a different ID, you need to make the default a lambda. In the first block of code in his answer, it calculates the message-ID once, at init, and uses the same one for every message.

Here's how I'm doing this in my app:

default "Message-ID" => lambda {"<#{SecureRandom.uuid}@#{Rails.application.config.mailgun_domain}>"}

... with the domain taken from a custom app config variable (and using SecureRandom.uuid, which is a little more straightforward than a SHA-2 based on the timestamp IMO.)

I usually prefer generating the message-id with a UUID. Assuming you have the uuid gem:

headers['Message-ID'] = "<#{ UUID.generate }@example.com>"

Also you should note that according to RFC 2822 the message-id should be placed inside "<" and ">"

In Rails 4+ (or just Ruby 2.0+) the folowing syntax is working correctly:

default "Message-ID" => ->(v){"<#{Digest::SHA2.hexdigest(Time.now.to_i.to_s)}@yourdomain.com>"}

Tested this with MailCatcher.

I figured this out. The easiest way to do is to use the default method at the top of the mailer class file.

Example:

require 'digest/sha2'
class UserMailer < ActionMailer::Base
  default "Message-ID"=>"#{Digest::SHA2.hexdigest(Time.now.to_i.to_s)}@yourdomain.com"

  # ... the rest of your mailer class
end

However, I found this difficult to test, so I wrote a private method and used the sent_at time instead of Time.now:

def message_id_in_header(sent_at=Time.now)
  headers["Message-ID"] = "#{Digest::SHA2.hexdigest(sent_at.to_i.to_s)}@yourdomain.com"
end

And I simply called that method before calling the mail method. This made it easy to pass a sent_at parameter from my test and verify a match in email.encoded.

@jasoncrawford is almost right. The problem is that the mailgun_domain attribute may not be able at development environment, so it is better to access the ActionMailer configs.

default "Message-ID" => lambda {"<#{SecureRandom.uuid}@{ActionMailer::Base.smtp_settings[:domain]}>"}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!