Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the Message-ID mail header in Rails3 / ActionMailer

I would like to alter the Message-ID header that is in the header portion of an email sent from a Ruby on Rails v3 application using ActionMailer.

I am using Sendmail on localhost for mail delivery.

Do I configure this in Sendmail or ActionMailer?

Where do I configure this (if it is ActionMailer): a file in config/ folder or a file in app/mailers/ folder?

like image 849
Teddy Avatar asked Jul 06 '11 14:07

Teddy


2 Answers

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.)

like image 145
jasoncrawford Avatar answered Dec 28 '22 22:12

jasoncrawford


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 ">"

like image 37
Mohammad Navid Avatar answered Dec 28 '22 21:12

Mohammad Navid