Thursday, April 21, 2011

ActionMailer get messageid for sent messages

I am writing a rails application that sends emails when certain actions occur, users can then reply to these emails and the system needs to match the reply email to the original email sent to the user.

It seems like the best way to do this is to save the message id header field when sending messages, is this possible in ActionMailer? I can then retrieve the references header from replies and match the messages.

If this is not possible are there any alternate solutions?

From stackoverflow
  • I'm not sure if it is possible within actionmailer to access the message id, however it is possible to add your own custom headers and then reference these when the user replies.

    You do this within the mailer model as follows :-

    @headers["VetId"] = order.vet_id
  • another alternative is to add a tracking code to the email's 'reply to' field something like this: reply to: notification+${notifcation_id}

    for example, if the notification id is 123, the user will reply to, when the system receives the email, 123 can be used to identify the original message.

  • Have a look on!

    Greg : That is not at all helpful.
    anka : Sorry Greg, what I wanted to mention is, that you can access the message id of your TMail::Mail object within your ActionMailer subclass. You cann see the reference at For instance you can create a new mail with "mail = UserMailer.create_notification_mail" (if UserMailer is your subclass of ActionMailer) and then access the message id with mail.message_id. But on the other side, I prefer the way "ez" descriebed above to add a token to your reply adress. Hope that helped a little bit more.
  • With Rails 3, this is possible:

    message = Mailer.welcome(@user).deliver
    message.message_id #=> 4ccc56d19...


Post a Comment