I am sending e-mails with the following code:
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)
But it gets treated as spam with the following scores:
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
I would like to keep the image in the e-mail. How can I make the score lower? Is it possible to remove the MIME_QP_LONG_LINE scores by attaching the picture instead of embedding it, or by encoding it with base64 ?
There is a whole load of (old) online examples that are using email.mime modules but not the interface from email.message. The documentation shows the two interfaces but it is very unclear if you should use one or the other or mix the two. Then, I didn't find how to implement those fixes, could anyone help?
Thanks for your help!