4

I am trying to use Office365 smtp server for automatically sending out emails. My code works previously with gmail server, but not the Office365 server in Python using smtplib.

My code:

import smtplib

server_365 = smtplib.SMTP('smtp.office365.com', '587')

server_365.ehlo()

server_365.starttls()

The response for the ehlo() is: (501, '5.5.4 Invalid domain name [DM5PR13CA0034.namprd13.prod.outlook.com]')

In addition, .starttls() raises a SMTPException: STARTTLS extension not supported by server

Any idea why this happens?

Pratibha
  • 1,652
  • 6
  • 24
  • 45

1 Answers1

0

The smtplib ehlo function automatically adds the senders host name to the EHLO command, but Office365 requires that the domain be all lowercase, so when youe default host name is uppercase it errors.

You can fix by explicitly setting sender host name in the ehlo command to anything lowercase.

import smtplib

server_365 = smtplib.SMTP('smtp.office365.com', '587')

server_365.ehlo('mylowercasehost')

server_365.starttls()
Cain
  • 244
  • 1
  • 7