Reputation: 78
I'm trying to send an email in Python
It's working without problem with gmail with this code :
import smtplib
sender = 'xxx@xxx'
receivers = ['[email protected]']
message = "hello"
try:
smtpObj = smtplib.SMTP('smtp.gmail.com:587')
smtpObj.starttls()
smtpObj.login('[email protected]', 'my_password')
smtpObj.sendmail(sender, receivers, message)
smtpObj.quit()
print("okay")
except:
print("notokay")
But when i use it with office 365, the email is send but the message is empty.
It's the same code but with 'smtp.office365.com:587' with my correct login and password.
Upvotes: 1
Views: 2010
Reputation: 23490
import smtplib
from email.mime.text import MIMEText
msg = MIMEText('hello')
msg['Subject'] = 'Urgent message'
msg['From'] = 'xxx@xxx'
msg['To'] = '[email protected]'
s = smtplib.SMTP('smtp.gmail.com:587')
s.starttls()
s.login('[email protected]', 'my_password')
s.sendmail('xxx@xxx', '[email protected]', msg.as_string())
s.quit()
Try the following, it might be because you need to create a MIMEText context for the formatting to be accepted by office365.
Upvotes: 1