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 = 'sender@abc.com'
recipient = 'Recipient <recipient@abc.com>'
send_email1("Test message from Pyton1", "Test message from Python script. 1")
send_email2("Test message from Pyton2", "Test message from Python script. 2")