I am trying to reply to an email using Python 3.4. The recipient of the email will be using Outlook (unfortunately) and it is important that Outlook recognizes the reply and displays the thread properly.
The code I currently have is:
def send_mail_multi(headers, text, msgHtml="", orig=None):
"""
"""
msg = MIMEMultipart('mixed')
# Create message container - the correct MIME type is multipart/alternative.
body = MIMEMultipart('alternative')
for k,v in headers.items():
if isinstance(v, list):
v = ', '.join(v)
msg.add_header(k, v)
# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
body.attach(MIMEText(text, 'plain'))
if msgHtml != "": body.attach(MIMEText(msgHtml, 'html'))
msg.attach(body)
if orig is not None:
msg.attach(MIMEMessage(get_reply_message(orig)))
# Fix subject
msg["Subject"] = "RE: "+orig["Subject"].replace("Re: ", "").replace("RE: ", "")
msg['In-Reply-To'] = orig["Message-ID"]
msg['References'] = orig["Message-ID"]+orig["References"].strip()
msg['Thread-Topic'] = orig["Thread-Topic"]
msg['Thread-Index'] = orig["Thread-Index"]
send_it(msg['From'], msg['To'], msg)
- The function
get_reply_message
is removing any attachments as in this answer. send_it
function sets the Message-ID header and uses the proper SMTP configuration. Then it callssmtplib.sendmail(fr, to, msg.as_string())
- Outlook receives the email but does not recognize/display the thread. However, the thread seems like being an attachment to the message (probably caused by
msg.attach(MIMEMessage(...))
Any ideas on how to do this? Have I missed any headers?
email
module in the standard library was overhauled in Python 3.6 to be more logical, versatile, and succinct; new code should target the (no longer very) newEmailMessage
API. Probably throw away anyMimeMultipart
code and start over with modern code from the Pythonemail
examples documentation. – PomeIn-Reply-To:
andReferences:
for RFC compliance, and probably the undocumented MicrosoftThread-xxx:
junk headers for Outlook, and wave a dead chicken at the crossroads at new moon. – Pome