7

I want to save the MessageID of a sent email, so I can later use it in a References: header to facilitate threading.

I see in root/django/trunk/django/core/mail.py (line ~55) where the MessageID is created.

I'm trying to think of the best way to collect this value, other than just copy/pasting into a new backend module and returning it. Maybe that is the best way?

1
  • 1
    Welcome to Stack Overflow. Since you've solved your problem, and documented the answer, please accept that answer as the answer to your question. That way the other denizens know that you don't need any more assistance with this question. Commented Aug 14, 2010 at 18:49

2 Answers 2

12

Ok, I see I was browsing tragically old code. I should be able to call django.core.mail.message.make_msgid() and populate the header myself before calling send.

1

Not all backends actually support asserting a message id (for e.g. SES sets it's own message ID and returns it in it's send response). You can actually pull out the returned/generated/set message id if you use the newer (circa 1.1?) EmailMessage class you can extract the returned message ID from the instance once you call .send(), e.g.:

e=EmailMessage(
            subject,
            content,
            from_email,
            recipient_list,
            headers = headers,
        )
 e.send()
 message_id = e.extra_headers.get('Message-Id',None)
5
  • How does it work when recipient_list has many email ids. We should get different message_id for different recipient. Commented Apr 17, 2013 at 14:25
  • Multiple recipients will all have the same message id, since you don't (even at an SMTP level) create multiple messages, you create one message, and the SMTP protocol handles replicating that message to multiple recipients. I've tested with SES and confirmed.
    – Darb
    Commented Apr 28, 2013 at 11:07
  • 3
    I could not get EmailMessage to work, there was no message_id in the variable. I moved to boto, which returns message_id properly and (message_id, email) is my unique key. But I lose the efficiency of using SMTP over using API. Commented May 2, 2013 at 12:58
  • 2
    Does not work on my Django setup, returned id is None. Commented Feb 28, 2014 at 19:28
  • 1
    Nothing in the current code of mail backends included in django handles the message-id. Except for 3rd-party backends, this is not supposed to work Commented Aug 27, 2014 at 10:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.