Search code examples
pythonsmtpsmtplib

SMTP email for office365


I want to send an email through python, I followed the below code from this link:

import smtplib

mailserver = smtplib.SMTP('smtp.office365.com',587)
mailserver.ehlo()
mailserver.starttls()
mailserver.login('myemail@company.com', 'mypassword')
msg = ('this is a message')
mailserver.sendmail('myemail@company.com','receiver@company.com',msg)

The issue: The email is in my sentbox, and the receiver's inbox, however there is no text. It is empty.

There is no error, or output, the script just runs, so I am unsure where to start troubleshooting, as I am not an expert in this area; could anyone explain why there is no message/text?


Solution

  • You need a \n between subject and email body in the 3rd argument to sendmail

    msg = 'Subject: Email Subject.\n{}'.format('this is a message')
    mailserver.sendmail('myemail@company.com','receiver@company.com', msg)