Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python mail puts unaccounted space in Outlook Subject line

Tags:

python

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
like image 702
screwgoth Avatar asked Sep 04 '14 17:09

screwgoth


People also ask

Can Python interact with Outlook?

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!

How do you write the body of an email in Python?

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.

How do I open an Outlook email from Python?

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.


2 Answers

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.

like image 155
Lukas Graf Avatar answered Oct 01 '22 14:10

Lukas Graf


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:

  • https://docs.python.org/2/library/email.generator.html#email.generator.Generator
  • https://docs.python.org/2/library/email.message.html#email.message.Message.as_string
  • https://docs.python.org/2/library/stringio.html
like image 43
Robᵩ Avatar answered Oct 01 '22 13:10

Robᵩ