Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the easiest/cleanest way to get the MessageID of a sent email?

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?

like image 411
Aaron McMillin Avatar asked Aug 12 '10 18:08

Aaron McMillin


2 Answers

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.

like image 86
Aaron McMillin Avatar answered Sep 18 '22 02:09

Aaron McMillin


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)
like image 38
Darb Avatar answered Sep 21 '22 02:09

Darb