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
  smtplib : name of attachment wrongly set to default arbiel 5 120 Apr-08-2026, 12:05 PM
Last Post: arbiel
  smtplib send email has no timestamp Pedroski55 2 3,011 Jun-11-2024, 04:57 AM
Last Post: Pedroski55
  [PROBLEM] Removing IDLE from context menu WebSpider 1 1,744 Sep-28-2023, 03:35 PM
Last Post: deanhystad
  with open context inside of a recursive function billykid999 1 1,962 May-23-2023, 02:37 AM
Last Post: deanhystad
  Context-sensitive delimiter ZZTurn 9 4,557 May-16-2023, 07:31 AM
Last Post: Gribouillis
  How does open context manager work? deanhystad 7 3,932 Nov-08-2022, 02:45 PM
Last Post: deanhystad
  Decimal context stevendaprano 1 1,987 Apr-11-2022, 09:44 PM
Last Post: deanhystad
  Sending Attachments via smtplib [SOLVED] AlphaInc 3 13,087 Oct-28-2021, 12:39 PM
Last Post: AlphaInc
  Sending random images via smtplib [SOLVED] AlphaInc 0 2,877 Oct-19-2021, 10:10 AM
Last Post: AlphaInc
  [UnicodeEncodeError from smtplib] yoohooos 0 5,369 Sep-25-2021, 04:27 AM
Last Post: yoohooos

Forum Jump:

User Panel Messages

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