简体   繁体   English

Python:将消息编码为base64以解决“!”和行长问题

[英]Python: Encoding message as base64 to solve “!” and line length issue

BACKGROUND 背景

Regarding the following articles: 关于以下文章:

All the problems and solutions refer to PHP issue, but I have run into this problem in Python. 所有问题和解决方案都涉及PHP问题,但是我在Python中遇到了这个问题。

If I send the emails directly to recipients, all is well, no exclamation marks appear, and the message displays properly. 如果我直接将电子邮件发送给收件人,则一切正常,没有出现感叹号,并且消息正确显示。

However, utilizing our "Sympa" ( https://www.sympa.org/ ) system that the University uses for it "mailing list" solution, emails from this system have the exclamation marks and line breaks inserted in the message and HTML breaks causing display issues. 但是,利用大学用于其“邮件列表”解决方案的“ Sympa”( https://www.sympa.org/ )系统,来自该系统的电子邮件在消息中插入了感叹号和换行符,并在HTML中插入了换行符导致显示问题。

The problem stems from line length. 问题出在线长上。 Any line longer than a magical 998 character length line gets this exclamation marks and line breaks inserted. 任何长于998个字符的神奇行的行都将获得此感叹号并插入换行符。

NOW THE QUESTION 现在的问题

One of the solutions they mention is encoding the body of a message in base64, which apparently is immune to the line length issue. 他们提到的解决方案之一是在base64中编码消息的主体,这显然不受行长问题的影响。 However, I can not figure out how to properly form a message in Python and have the proper headers and encoding happen so the message will display properly in an email client. 但是,我无法弄清楚如何在Python中正确地形成消息并具有正确的标头和编码,因此该消息将在电子邮件客户端中正确显示。

Right now, I have only succeed in sending emails with base64 encode bodies as attached files. 目前,我只成功发送了带有base64编码主体的电子邮件作为附件。 Bleck! 布莱克!

I need to send HTML encoded emails (tables and some formatting). 我需要发送HTML编码的电子邮件(表格和某些格式)。 I create one very long concatenated string of all the html squished together. 我创建一个非常长的串联字符串,将所有html压缩在一起。 It is ugly but will display properly. 它很难看,但可以正确显示。

HELP?! 救命?!

NOTE: If anyone else has had this problem and has a solution that will allow me to send emails that are not plagued by line length issue, I am all ears! 注意: 如果其他任何人遇到了此问题,并且有解决方案可以使我发送不受行长问题困扰的电子邮件,我将不胜感激!

Source Code as Requested 源代码按要求

# Add support for emailing reports
import smtplib
# from email.mime.text import MIMEText
from email.mime.message import MIMEMessage
from email.encoders import encode_base64
from email.message import Message

... ...

headerData = {"rDate": datetime.datetime.now().strftime('%Y-%m-%d')}
msg_body = msg_header.format(**headerData) + contact_table + spacer + svc_table

theMsg = Message()
theMsg.set_payload(msg_body)
encode_base64(theMsg)
theMsg.add_header('Content-Transfer-Encoding', 'base64')
envelope = MIMEMessage(theMsg, 'html')

theSubject = "Audit for: "+aService['description']
envelope['Subject'] = theSubject

from_addr = "xxx@xxx"
envelope['From'] = from_addr

to_addrs = "xxx@xxxx"
# to_addrs = aService['contact_email']
envelope['To'] = to_addrs

# Send the message via our own SMTP server.
s = smtplib.SMTP('x.x.x.x')
s.sendmail(from_addr, to_addrs, envelope.as_string())
s.quit()

SOLUTION, thank you @Serge Ballesta Going back to MIMEText, and specifying a character set seemed to do the trick: 解决方案,谢谢@Serge Ballesta回到MIMEText,并指定字符集似乎可以解决问题:

envelope = MIMEText(msg_body, 'html', _charset='utf-8') 
assert envelope['Content-Transfer-Encoding'] == 'base64'

envelope['Subject'] = "Audit for: "+aService['description']

from_addr = "f5-admins@utlists.utexas.edu"
envelope['From'] = from_addr

to_addrs = "xx-test@xx.xx.edu"
envelope['To'] = to_addrs

# Send the message via our own SMTP server.
s = smtplib.SMTP('xx.xx.xx.edu')
s.sendmail(from_addr, to_addrs, envelope.as_string())
s.quit()

Apparently I was just stabbing around and did not account for character set. 显然我只是在刺伤,没有考虑字符集。 Using MIMEText and not MIMEMessage. 使用MIMEText而不是MIMEMessage。

Normally, email.mime.MIMEText automatically sets the Content-Transfert-Encoding to base64 if the body is not declared to be plain ASCII. 通常,如果未将主体声明为纯ASCII,则email.mime.MIMEText自动将Content-Transfert-Encoding为base64。 So, assuming that body contains the HTML text of the body of the message (no mail headers there), declaring it as utf-8 should be enough: 因此,假设body包含邮件body的HTML文本(那里没有邮件头),则将其声明为utf-8应该就足够了:

msg = email.mime.text.MIMEText(body, 'html', _charset='utf-8')

# assert the cte:
assert msg['Content-Transfer-Encoding'] == 'base64'

theSubject = "Audit for: "+aService['description']
msg['Subject'] = theSubject

from_addr = "xxx@xxx"
msg['From'] = from_addr

to_addrs = "xxx@xxxx"
# to_addrs = aService['contact_email']
msg['To'] = to_addrs

# optionally set other headers
# msg['Date']=datetime.datetime.now().isoformat()

# Send the message
s = smtplib.SMTP('x.x.x.x')
s.sendmail(from_addr, to_addrs, msg.as_bytes())
s.quit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM