Python Forum

Full Version: Smtplib: What does context argument means?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
In lines 13 and 14 what exactly does context mean? Why do we need to use it?
import smtplib, ssl

port = 465  # For SSL
smtp_server = "smtp.gmail.com"
sender_email = "[email protected]"  # Enter your address
receiver_email = "[email protected]"  # Enter receiver address
password = input("Type your password and press enter: ")
message = """\
Subject: Hi there

This message is sent from Python."""

context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message)
ssl is built upon using certificates to encrypt and decrypt data between two machines. Those certificates are held in a temporary context, so the encryption and decryption can take place. No context = no ssl.