2.3. Protocol SMTP
2.3.1. Send plaintext email
import smtplib
from email.mime.text import MIMEText
SMTP_HOST = 'smtp.gmail.com'
SMTP_PORT = 465
SMTP_USER = 'myusername@gmail.com'
SMTP_PASS = 'mypassword'
EMAIL_FROM = 'myusername@gmail.com'
EMAIL_TO = ['user1@example.com', 'user2@example.com']
EMAIL_SUBJECT = 'My Subject'
EMAIL_BODY = 'My Email Body'
msg = MIMEText(EMAIL_BODY)
msg['Subject'] = EMAIL_SUBJECT
msg['From'] = EMAIL_FROM
msg['To'] = ', '.join(EMAIL_TO)
server = smtplib.SMTP_SSL(
host=SMTP_HOST,
port=SMTP_PORT)
server.login(
user=SMTP_USER,
password=SMTP_PASS)
server.sendmail(
from_addr=EMAIL_FROM,
to_addrs=EMAIL_TO,
msg=msg.as_string())
server.quit()
2.3.2. Send email with attachments
import os
import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
SMTP_HOST = 'smtp.gmail.com'
SMTP_PORT = 465
SMTP_USER = 'myusername@gmail.com'
SMTP_PASS = 'mypassword'
EMAIL_FROM = 'myusername@gmail.com'
EMAIL_TO = ['user1@example.com', 'user2@example.com']
EMAIL_SUBJECT = 'My Subject'
EMAIL_BODY = 'My Email Body'
msg = MIMEMultipart()
msg['Subject'] = EMAIL_SUBJECT
msg['From'] = EMAIL_FROM
msg['To'] = ', '.join(EMAIL_TO)
txt = MIMEText(EMAIL_BODY)
msg.attach(txt)
FILE = r'/tmp/myfile.png'
with open(FILE, mode='rb') as file:
img = MIMEImage(file.read())
img.add_header('Content-Disposition', 'attachment', filename=os.path.basename(FILE))
msg.attach(img)
server = smtplib.SMTP_SSL(
host=SMTP_HOST,
port=SMTP_PORT)
server.login(
user=SMTP_USER,
password=SMTP_PASS)
server.sendmail(
from_addr=EMAIL_FROM,
to_addrs=EMAIL_TO,
msg=msg.as_string())
server.quit()
2.3.3. Secured connection to the SMTP server
import ssl
import smtplib
SMTP_HOST = 'localhost'
SMTP_PORT = 465
SMTP_USER = 'myusername'
SMTP_PASS = 'mypassword'
smtp = smtplib.SMTP_SSL(
host=SMTP_HOST,
port=SMTP_PORT)
context = ssl.create_default_context()
smtp.starttls(context=context)
# (220, b'2.0.0 Ready to start TLS')
2.3.4. Assignments
2.3.4.1. Send email
- About:
Name: Send email
Difficulty: medium
Lines: 20
Minutes: 21
- License:
Copyright 2025, Matt Harasymczuk <matt@python3.info>
This code can be used only for learning by humans (self-education)
This code cannot be used for teaching others (trainings, bootcamps, etc.)
This code cannot be used for teaching LLMs and AI algorithms
This code cannot be used in commercial or proprietary products
This code cannot be distributed in any form
This code cannot be changed in any form outside of training course
This code cannot have its license changed
If you use this code in your product, you must open-source it under GPLv2
Exception can be granted only by the author (Matt Harasymczuk)
- English:
Connect to SMTP server provided by instructor
Send email to address provided by instructor
Attach
pep20.txt
file which is a result ofimport this
PEP 20 -- The Zen of PythonRun doctests - all must succeed
- Polish:
Połącz się z serwerem podanym przez prowadzącego
Wyślij wiadomość email na podany przez prowadzącego adres
Do wiadomości załącz plik
pep20.txt
, który będzie wynikiem poleceniaimport this
PEP 20 -- The Zen of PythonUruchom doctesty - wszystkie muszą się powieść