I'm trying to send an email through the office365 server. The email is properly delivered, however the message is not attached
Assistance is most appreciated
import smtplib
to = "[email protected]"
office365_user = '[email protected]'
office365_pwd = 'password'
smtpserver = smtplib.SMTP("smtp.office365.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login(office365_user,office365_pwd)
msg = "This is a test email \n"
smtpserver.sendmail(office365_user, to, msg)
smtpserver.close()
Your message is not a valid mail message, which consists of a header and a body. Try something like this:
msg = """From: <[email protected]>
To: <[email protected]>
Subject: foo
This is a test email
"""
Consider constructing the message in the same manner as the Python documentation.
from email.mime.text import MIMEText
msg = MIMEText("This is a test email")
msg['Subject'] = 'Email Subject'
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'
Also, I'm not sure about using smtpserver.close()
. It seems the proper way is smtpserver.quit()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With