I've written a simple Python script which used MIMEMultipart and SMTPLib to send a mail to an array of recipients. The code looks something like this:
import smtplib
import sys
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
sender='[email protected]'
recipients='[email protected]'
subject='A pretty long subject line which looks like this'
mail_server='microsfot_exchange_server_ip'
msg = MIMEMultipart('alternative')
body='Body of the Email'
msg['Subject'] = subject
msg['from'] = sender
msg['to'] = ", ".join(recipients)
s = smtplib.SMTP(mail_server)
s.sendmail(sender, recipients, msg.as_string())
s.quit()
This sends a mail successfully, but the Subject like in Outlook Mail client looks something like this:
A pretty long subject line which looks like this
Getting Started. If you have your Outlook already open, you are now ready to interact programatically with it using Python and start automating away the really boring tasks!
Use Python's built-in smtplib library to send basic emails. Send emails with HTML content and attachments using the email package. Send multiple personalized emails using a CSV file with contact data. Use the Yagmail package to send email through your Gmail account using only a few lines of code.
startfile('outlook') The easiest way to open Outlook on Windows using only a short and concise Python One-Liner is to use the os. startfile('outlook') function call from the os module.
It seems you've hit Issue #1974:
Long e-mail headers should be wrapped. This process is called "header folding" and is described in RFC822
. However, RFC822 seems to be a bit ambigous about how exactly header folding should take place.
Python in versions earlier than 2.7
/ 3.1
happened to do it in a way that caused the issued you described with certain mail clients (using tab \t
as the continuation character).
In the bug report there has been a workaround suggested: Make your subject line a header object like this:
from email.header import Header
# ...
msg['Subject'] = Header(subject)
I just verified this, and it does indeed use spaces instead of tabs as continuation characters, which should solve your problem.
Your subject line is longer than 78 chars, and is being broken up by .as_string()
. The first few chars are on the subject line, and the remaining chars are on one or several continuation lines.
When Outlook reconstructs the original subject line, it does so incorrectly.
You can try to avoid this by avoiding continuation lines, like so:
from StringIO import StringIO
from email.generator import Generator
def my_as_string(msg):
fp = StringIO()
g = Generator(fp, mangle_from_=False, maxheaderlen=0)
g.flatten(msg)
return fp.getvalue()
...
s.sendmail(sender, recipients, my_as_string(msg))
References:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With