Reputation: 69
I have a curious problem. When I send a message with the send_email1
function in my script, the message ends up in my Gmail spam folder, but when I use send_email2
it is successfully delivered to my inbox. The second message uses the legacy API, and I assume it will be deprecated at some point and that I should not use it any more.
Many thanks for the help.
import smtplib
from email.message import EmailMessage
from email.mime.text import MIMEText
def send_email1(subject: str, content: str):
msg = EmailMessage()
msg['From'] = sender
msg['To'] = recipient
msg['Subject'] = subject
msg.set_content(content)
with smtplib.SMTP('mysmtpserver') as s:
s.starttls()
s.send_message(msg)
def send_email2(subject: str, content: str):
msg = MIMEText(content)
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = recipient
with smtplib.SMTP('mysmtpserver') as s:
s.starttls()
s.send_message(msg)
sender = '[email protected]'
recipient = 'Recipient <[email protected]>'
send_email1("Test message from Pyton1", "Test message from Python script. 1")
send_email2("Test message from Pyton2", "Test message from Python script. 2")
Upvotes: 1
Views: 1933
Reputation: 191
looking at the resulting mail-data, which is both compliant with RFC 5322, one can identify two differences:
From: [email protected]
To: Recipient <[email protected]>
Subject: Test message from Pyton1
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit
MIME-Version: 1.0
Test message from Python script. 1
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: Test message from Pyton2
From: [email protected]
To: Recipient <[email protected]>
Test message from Python script. 2
The first difference is the order. The mail which does not end up in the spam-folder declares the header-fields Content-Type
and MIME-Version
first. Secondly, the specified charset is charset="us-ascii"
opposed to utf-8
.
While we can only speculate about Gmail's spam filtering rules, I propose
to change the order of setting content and the header-fields as well as
explicitly stating the Content-Type
(with its subtype and charset) by using
msg.set_content(content, subtype="plain", charset='us-ascii')
This results in a modificiation of send_email(...)
like so:
def send_email1(subject: str, content: str):
msg = EmailMessage()
msg.set_content(content, subtype="plain", charset='us-ascii')
msg['From'] = sender
msg['To'] = recipient
msg['Subject'] = subject
Upvotes: 2
Reputation: 2479
Gmail controls what emails get sent to inbox or spam. Google has all sorts of data collection and analysis on what emails are spam and what are not spam. This seems to be google treating the one content type as spam and the other not.
at the end of the day its not how you send the mail from python its the content that you put into the mail (and other things like sending location, content type, attachments etc) that google will decide to send the email to spam or not.
For example if I send a mail with "Catchup" and a body of how we haven't spoken in a while. Google will probably send it to my inbox. However if I send an email with "CLICK THIS LINK TO WIN 100 MILLION" its probably going to get put in spam. Its not how you send the mail. Its the content of the mail and its properties.
Upvotes: 0