![](/img/trans.png)
[英]Python function for changing the subject line of an Outlook mail item
[英]Python mail puts unaccounted space in Outlook Subject line
我編寫了一個簡單的Python腳本,它使用MIMEMultipart和SMTPLib將郵件發送給收件人數組。 代碼看起來像這樣:
import smtplib
import sys
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
sender='foo@bar.com'
recipients='someguy@bar.com'
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()
這會成功發送郵件,但Outlook郵件客戶端中的主題看起來像這樣:
A pretty long subject line which looks like this
您的主題行超過78個字符,並且被.as_string()
分解。 前幾個字符在主題行上,其余字符在一個或多個連續行上。
當Outlook重建原始主題行時,它會錯誤地執行。
你可以通過避免延續線來避免這種情況,如下所示:
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))
參考文獻:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.