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?
Cheers,
Andreas
Took me a while but the following seems working:
The code needs a little bit tiding up but as is Outlook displays it correctly (with Next/Previous options). There was no need to create
From, Send, To, Subject
headers by hand, appending the content worked.Hope this saves someone else's time