-2

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!

Guillaume Ansanay-Alex
  • 1,212
  • 2
  • 9
  • 20
  • Could the user who downvoted my question please explain why? I am trying to design a corporate mail sender for business reports, not a spam machine, and I have been trying many solutions found in a mix of python documentation (which lacks any consideration of spam evaluation) and SO answers (often written for Python 2.x or also not considering spam evaluation). If my question is badly written please tell me. – Guillaume Ansanay-Alex Aug 03 '18 at 12:32
  • 1
    I didn't downvote, but you have the reasons why it was filtered right there. You know exactly why it was treated as such, so fix it. You have a few hypotheses for how you could make it better, so implement them and see if that helps – dfundako Aug 03 '18 at 12:54
  • The thing is I didn't find how to implement those fixes when I searched. That's why I am posting here. – Guillaume Ansanay-Alex Aug 03 '18 at 13:04

1 Answers1

0

A SpamAssassin score slightly above 2.5 is not a very high score. The dominating weight is from the image (1.78) and you say you don't want to remove it. But yes, obviously, you can shave off that 0.78 by not violating the MIME standard. QP should be wrapped, and I trust Python is already doing that for the image - the overlong line is probably in your text or HTML contents.

Without seeing the full message it's hard to pinpoint where exactly to look, but examining the source of the message should easily reveal where exactly you have ling lines in quoted-printable parts.

As an aside, SpamAssassin isn't complaining about this, but doing a multipart/alternative which doesn't contain any alternatives also looks like you don't know what you are doing, which of course doesn't make you a spammer, but certainly makes you look even more like one. Put a mutipart/alternative wrth a plain-text part and a HTML part.

Ditto in spades for putting the subject in the MIME preamble - that's just crazy.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Not in a place where I can debug your code, but once you know what to look for, it should not be hard to find examples of how to do these things correctly. – tripleee Aug 03 '18 at 13:29
  • Ultimately, the preferred solution is to delegate your email to somebody who understands email deliverability, so you can concentrate on the content and business side of things. It's not free, but it can tremendously alleviate frustrations. (And I don't work in that industry, so don't assume I'm using this as an opportunity to promote my own services.) – tripleee Aug 03 '18 at 13:33