Python Forum
Smtplib: What does context argument means? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Smtplib: What does context argument means? (/thread-17076.html)



Smtplib: What does context argument means? - Pythenx - Mar-27-2019

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)



RE: Smtplib: What does context argument means? - nilamo - Mar-27-2019

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.