Skip to content

Instantly share code, notes, and snippets.

@jamescalam
Created May 26, 2020 16:53
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamescalam/93d915e4de12e7f09834ae73bdf37299 to your computer and use it in GitHub Desktop.
Save jamescalam/93d915e4de12e7f09834ae73bdf37299 to your computer and use it in GitHub Desktop.
Example code for sending an email via SMTP with TLS encryption in Python.
import smtplib
# initialize connection to our email server, we will use Outlook here
smtp = smtplib.SMTP('smtp-mail.outlook.com', port='587')
smtp.ehlo() # send the extended hello to our server
smtp.starttls() # tell server we want to communicate with TLS encryption
smtp.login('joe.bloggs@outlook.com', 'Password123') # login to our email server
# send our email message 'msg' to our boss
smtp.sendmail('joe.bloggs@outlook.com',
'joes.boss@outlook.com',
msg.as_string())
smtp.quit() # finally, don't forget to close the connection
@Svtter
Copy link

Svtter commented Jun 21, 2022

The msg is missing.

@wigggles
Copy link

The msg is missing.

Same boat for me, this was useful.

from email.message import EmailMessage
message = EmailMessage()

@b0tting
Copy link

b0tting commented May 24, 2023

Another workaround is to replace msg.as_string() with your own string, ie. "hello world!"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment