Python
Sending Email
31 flashcards · answers and spaced-repetition review in the KnowCard app
You want your Python script to SEND an outgoing email. Which standard-library module connects to the mail server, and which protocol does it speak?
You connect with smtplib.SMTP(host, 587), then immediately call .login(...) over the plain connection. Why is this a problem, and what step is missing?
For an encrypted SMTP connection, what's the difference between smtplib.SMTP with .starttls() and smtplib.SMTP_SSL — including the typical port for each?
What does smtp.sendmail(...) return when ALL recipients accepted the message, and how would a competent dev wrongly interpret it?
In smtp.sendmail(from_addr, to_addrs, msg), you pass a single recipient string "a@b.com" as to_addrs and it works. Then you switch to two recipients — what's the gotcha?
What happens when you run this?
```
from email.message import EmailMessage
m = EmailMessage()
m["Subject"] = "Hi"
m["Subject"] = "Bye"
print(m["Subject"])
```
Why prefer email.message.EmailMessage + .set_content(...) over hand-building MIMEText/MIMEMultipart strings for a modern email?
You build a message and send it. Why is smtp.send_message(msg) usually better than smtp.sendmail(frm, to, msg.as_string())?
When you call smtp.login("me", "pwd") in smtplib, whose username and password are you authenticating with?
Name the standard-library module for each role: send an email, retrieve mail leaving copies on the server, and download mail off the server.
pop.retr(1) in poplib returns a 3-tuple — what are its three elements?
You call pop.dele(1) then reconnect and the mail is still there. Why, and how do you undo pending deletions?
imaplib's IMAP4([host, port, timeout]) — which port does it default to, and which parameter is version-gated?
Every imaplib IMAP4 command method returns the same shape of result. What is it, and how do you tell success from failure?
How do you cleanly open and close an imaplib session, and what does using IMAP4 in a with-block do at exit?
im.list(".", "*am") in imaplib — what does it return, and what are the three space-separated pieces inside each result entry?
An SMTP session speaks a text-based protocol. Put these commands in session order and say how HELO differs from EHLO: DATA, MAIL FROM, EHLO/HELO, RCPT TO, QUIT.
You write smtplib.SMTP("mail.example.com") with no port and it connects. Which port did it use, and what is the local_hostname argument for?
In smtp.sendmail(from_addr, to_addrs, msg), what two string forms may each email address take?
Why can't you drop raw UTF-8 text or a JPEG straight into an SMTP message body, and what's the fix?
sendmail(from_addr, to_addrs, msg, mail_options, rcpt_options) — what do the last two optional parameters do?
Retrieving mail with poplib: which port does POP3(host) default to, why is the password method named pass_ (trailing underscore), and when does the inbox unblock?
What does poplib's pop.stat() return, and what do the two values mean?
pop.list() vs pop.list(1) in poplib — what does each return, and where does mail numbering start?
im.select("INBOX") returns what, and what happens if you select a mailbox that doesn't exist?
Besides select/close, which three imaplib IMAP4 methods manage mailboxes on the server, and what does each take?
im.search(None, '(FROM "Johannes")') in imaplib — why is the first argument None, what can the criteria look like, and what comes back?
imaplib's fetch(message_set, message_parts): what forms can message_set take, and what do (RFC822), (BODY[TEXT]) and (BODY[HEADER]) each download?
Using the base email.message.Message class directly, how do you set the body, add Subject/From/To, and turn it into sendable text?
To email an image attachment with the legacy email.mime API, which container class do you build, how are the text and image parts added, and what do all MIME part classes share?
You have a raw email saved as a string (or in a file). How do you parse it back into a Message object with the email package?
Start learning today
Free to start — download the app or use it in your browser.
