Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: smtp with TLS delivers no message

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()
like image 968
user1148257 Avatar asked Jun 06 '14 16:06

user1148257


2 Answers

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 
"""
like image 69
Steffen Ullrich Avatar answered Sep 20 '22 00:09

Steffen Ullrich


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().

like image 24
HoppyKamper Avatar answered Sep 18 '22 00:09

HoppyKamper