【发布时间】:2019-01-11 06:52:58
【问题描述】:
我正在发送带有以下代码的电子邮件:
import smtplib
from email.message import EmailMessage
from email.utils import make_msgid, formataddr
from pkg_resources import resource_filename
def send_email(addressee, subject, text, cc=None):
msg = EmailMessage()
msg['Subject'] = subject
msg['From'] = formataddr(('Bot', 'bot@domain.fr'))
msg['To'] = addressee
if cc is not None:
msg['CC'] = cc
msg.preamble = subject
msg.set_content(text)
image_cid = make_msgid(domain='domain.fr')
mail_text = """<p>Bonjour,</p>
<p>{text}</p>
<p><img src="cid:{image_cid}"></p>
<p style="color:rgb(160, 160, 160);font-size:85%;">
___________________________________________________________________________<br>
Ce mail est généré automatiquement, veuillez ne pas y répondre.<br>
Contact : <a href="mailto:bot@domain.fr" target="_top">bot@domain.fr</a>
</p>
""".format(text=text, image_cid=image_cid[1:-1])
msg.add_alternative(mail_text, subtype='html')
with open(resource_filename('my_package', 'mail/img/bot.png'), 'rb') as fp:
img_data = fp.read()
msg.add_attachment(img_data,
cid=image_cid,
maintype='image',
subtype='png')
with smtplib.SMTP('smtp.domain.fr') as s:
s.send_message(msg)
但它会被视为具有以下分数的垃圾邮件:
pts rule name description
---- ---------------------- --------------------------------------------------
0.00 HTML_IMAGE_ONLY_08 BODY: HTML: images with 400-800 bytes of words
0.00 HTML_MESSAGE BODY: HTML included in message
0.00 MIME_QP_LONG_LINE RAW: Quoted-printable line longer than 76 chars
0.82 MIME_QP_LONG_LINE_2 RAW: Quoted-printable line longer than 76 chars
1.78 HTML_IMAGE_ONLY_08_2 HTML: images with 400-800 bytes of words
我想将图像保留在电子邮件中。怎样才能降低分数?是否可以通过附加图片而不是嵌入图片来删除 MIME_QP_LONG_LINE 分数,或者使用 base64 对其进行编码?
有一大堆(旧的)在线示例使用 email.mime 模块,但没有使用 email.message 的界面。文档显示了这两个接口,但不清楚您应该使用其中一个还是混合使用两者。然后,我没有找到如何实施这些修复,有人可以帮忙吗?
感谢您的帮助!
【问题讨论】:
-
对我的问题投反对票的用户能否解释一下原因?我正在尝试为业务报告设计一个公司邮件发件人,而不是垃圾邮件机器,并且我一直在尝试在 python 文档(没有考虑垃圾邮件评估)和 SO 答案(通常为 Python 2 编写)中找到的许多解决方案.x 或也不考虑垃圾邮件评估)。如果我的问题写得不好,请告诉我。
-
我没有投反对票,但您有理由将其过滤掉。你确切地知道为什么它被这样对待,所以修复它。你有一些假设可以让它变得更好,所以实施它们,看看是否有帮助
-
问题是我在搜索时没有找到如何实现这些修复。这就是我在这里发帖的原因。
标签: python email email-spam