Python Forum
Smtplib: What does context argument means?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Smtplib: What does context argument means?
#1
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)
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Context-sensitive delimiter ZZTurn 9 1,392 May-16-2023, 07:31 AM
Last Post: Gribouillis
  How does open context manager work? deanhystad 7 1,263 Nov-08-2022, 02:45 PM
Last Post: deanhystad
  Decimal context stevendaprano 1 1,009 Apr-11-2022, 09:44 PM
Last Post: deanhystad
  Sending Attachments via smtplib [SOLVED] AlphaInc 3 2,107 Oct-28-2021, 12:39 PM
Last Post: AlphaInc
  Sending random images via smtplib [SOLVED] AlphaInc 0 1,661 Oct-19-2021, 10:10 AM
Last Post: AlphaInc
  [UnicodeEncodeError from smtplib] yoohooos 0 3,344 Sep-25-2021, 04:27 AM
Last Post: yoohooos
  Understand what it means that everything in Python is an object... bytecrunch 8 3,714 Mar-19-2021, 04:47 PM
Last Post: nilamo
  Understanding The Arguments for SMTPlib - sendmail JoeDainton123 3 2,679 Aug-03-2020, 08:34 AM
Last Post: buran
  TimeOutError when trying to initiate smtplib.SMTP thecosmos 0 3,292 Jun-19-2020, 05:30 AM
Last Post: thecosmos
  SMTPlib help tpolim008 4 3,732 Apr-27-2020, 11:11 PM
Last Post: tpolim008

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020