send SMTP email for office365 with python using tls/ssl
Vishesh Arya!
SMTP AUTH extension not supported by Office 365 account. Search for Microsoft Graph to do this.
SMTP email for office365
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)
Errors when using SMTPLIB SSL email with a 365 email address
From the documentation of SMTP_SSL:
SMTP_SSL should be used for situations where SSL is required from the beginning of the connection and using starttls() is not appropriate.
Thus, SMTP_SSL is for implicit SMTP and the common port for this is 465. Port 587 is instead used for explicit SMTP where a plain connect is done and later an upgrade to SSL with the STARTTLS command.
What happens here is that the client tries to speak SSL/TLS to a server which does not expect SSL/TLS at this stage and thus replies with non-TLS data. These get interpreted as TlS nonetheless which results in this strange [SSL: WRONG_VERSION_NUMBER]
.
To fix this either use port 465 (and not 587) with SMTP_SSL (not supported by Office365) or use port 587 but with starttls:
with smtplib.SMTP("smtp.office365.com", 587) as server:
server.starttls(context=context)
SSL error while sending email using smtp with Python
Microsoft Outlook uses STARTTLS when sending an email. So you need to replace smtplib.SMTP_SSL
with smtplib.SMTP
and you need to call starttls
reference: smtplib.SMTP.starttls
import smtplib
from email.message import EmailMessage
sender = 'somename@outlook.com'
recipient = 'somename@gmail.com'
msg = EmailMessage()
msg.set_content('this is a test')
msg['From'] = 'somename@outlook.com'
msg['To'] = 'somename@gmail.com'
msg['Subject'] = 'test email'
with smtplib.SMTP('smtp.office365.com', 587) as server:
server.ehlo()
server.starttls()
server.ehlo()
server.ehlo()
server.login('your_login', "your_password", initial_response_ok=True)
server.ehlo()
server.sendmail(sender, recipient, msg.as_string())
print('Email sent!')
server.close()
Here is the Outlook message in my Gmail account.
I noted that I had to change my Outlook password, because it had a \n
, which Python read as a new line.
----------------------------------------
My system information
----------------------------------------
Platform: macOS
OS Version: 10.15.7
Python Version: 3.9
----------------------------------------
Your question didn't identity the type of Outlook account you have.
- Free account
- Corporate account
You stated in the comments below that your error message included ask your email administrator.
I haven't seem this message with the Free account so I'm assuming that you might have a Corporate account. If you do have the latter please review this Enable or disable authenticated client SMTP submission, because an email administrator would need to enable Authenticated SMTP
on your corporate account.
python: smtp with TLS delivers no message
Your message is not a valid mail message, which consists of a header and a body. Try something like this:
msg = """From: <me@example.com>
To: <you@example.com>
Subject: foo
This is a test email
"""
Django: send email via Office365
To send email using the Office365 smtp server, the "from" user and the "host" user must be the same. So I added this setting in Django in order to send email to the admins:
SERVER_EMAIL = EMAIL_HOST_USER
Related Topics
Subtract One Dataframe from Another Excluding the First Column Pandas
Change a Colour of a Pixel in Python
No Unique Mode; Found 2 Equally Common Values
Finding Index of an Item Closest to the Value in a List That'S Not Entirely Sorted
Pip3: Command Not Found But Python3-Pip Is Already Installed
How to Make Print() Accept the User Input in Same Line
Truth Value of a Series Is Ambiguous. Use A.Empty, A.Bool(), A.Item(), A.Any() or A.All()
Calculating Manhattan Distance in Python in an 8-Puzzle Game
Python: Import Cx_Oracle Importerror: No Module Named Cx_Oracle Error Is Thown
Typeerror: Image Data Can Not Convert to Float
How to Share Data Between a Parent and Forked Child Process in Python
How to Rotate Xticks on One Axis of Figure in Matplotlib Without "Getting" the Labels as a List
How to Add Thousand Separator to Numbers in Python Pandas Dataframe
Python/Pandas: How to Match List of Strings With a Dataframe Column
Convenient Way to Handle Deeply Nested Dictionary in Python
Running an Excel Macro Via Python