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?

Aaron McMillin
  • 2,532
  • 27
  • 42
  • 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. – Craig Trader Aug 14 '10 at 18:49

2 Answers2

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.

Aaron McMillin
  • 2,532
  • 27
  • 42
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)
Darb
  • 1,463
  • 11
  • 10
  • How does it work when recipient_list has many email ids. We should get different message_id for different recipient. – Kumar Deepak Apr 17 '13 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 Apr 28 '13 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. – Kumar Deepak May 02 '13 at 12:58
  • 2
    Does not work on my Django setup, returned id is `None`. – Amir Ali Akbari Feb 28 '14 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 – Jocelyn delalande Aug 27 '14 at 10:21