By Adrian | October 14, 2019
Recently I’ve been working with python and smtplib
to automate sending emails based off a template. The sending part of the emails worked flawlessly but I had issues where the emails were being delivered into the junk folder instead of the Inbox folder in Outlook.
Now, if you’re going to a scripting emails to send out notifications, newsletters and general communications, losing your audience because the email gets trashed isnt ideal. I thought that perhaps it was because I was doing something funky by embedding images as base64 in html instead of adding them as attachments. After I changed the way I added attachments, I still had the same issue.
Naturally I turned to google to help and came across a website Mailtrap.io where you can configure your script to use the mailtrap SMTP server and it would score your email and determine if it was spam. I decided to do just that.
I created a free account and headed over to the SMTP settings to get the required settings (server, port, user, pass) and it even gives you a pretty descent list on how to use it with various programming languages.
But first. Some code. This is what my python code looked like for constucting the message looked like. Pretty basic. All contacts BCC’d.
# Build message
msg_related = MIMEMultipart('related')
msg_related['Subject'] = subject
msg_related['From'] = mail_from
msg_related['bcc'] = ', '.join(recipients_list)
msg_related.preamble = 'This is a multi-part message in MIME format.'
Now after I configured the SMTP server with that of Mailtrap’s, I got this analysis and I knew exactly where my problem lied…. It was generating spam!
The really useful thing about this analysis is it told me what the contributing factors caused this and from that I was able to address them. They were pretty self explainatory, so I started working my way through some of the recomendations. The first ones were:
- [+1.2] MISSING_TO_HEADER
- [+1.9] REPLYTO_WITHOUT_TO_CC
These 2 items meant adding in the following lines into my code:
msg_related['To'] = mail_from
msg_related['Reply-To'] = mail_from
I resent a test message to Mailtrap to see the result, and were all good to go. It’s not spam.
At this time I was thinking that were making some progress and my emails will probably pass a junk test, but theres a few more quick wins there to bring that spam score even lower so I continued by working on these recommendations:
- [+1.4] MISSING DATE
- [+0.1] MISSING_MID
For this I had to add in 2 imports at the top of my script and a further 3 lines of code so that I could include a date and Message_ID.
from email.utils import formatdate
from email.utils import make_msgid
msgid = make_msgid()
msg_related['Date'] = formatdate(localtime=True)
msg_related['Message-ID'] = msgid
Now when I ran the email again, my score was brought down to 1.5
Resetting my SMTP settings away from Mailtrap to perform some testing was now successful. So yeah, this is webapp is worthwhile and saved me a lot of time in the process.