简体   繁体   中英

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?

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.

Not all backends actually support asserting a message id (for eg 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(), eg:

e=EmailMessage(
            subject,
            content,
            from_email,
            recipient_list,
            headers = headers,
        )
 e.send()
 message_id = e.extra_headers.get('Message-Id',None)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM