summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2024-06-18 12:12:58 (GMT)
committerGitHub <noreply@github.com>2024-06-18 12:12:58 (GMT)
commit36b0052cb22d4761709e6bb84a3ce0f4f570e235 (patch)
treeed65b43aa24a73eb88ad19578f64a3ad0edbf070
parent692874cdcc4bde3507c1fec614669dea28b9bb2e (diff)
downloadcpython-36b0052cb22d4761709e6bb84a3ce0f4f570e235.zip
cpython-36b0052cb22d4761709e6bb84a3ce0f4f570e235.tar.gz
cpython-36b0052cb22d4761709e6bb84a3ce0f4f570e235.tar.bz2
[3.13] gh-120662: Improve `smtplib` example (GH-120668) (#120681)
gh-120662: Improve `smtplib` example (GH-120668) (cherry picked from commit 4bc27abdbee88efcf9ada83de6e9e9a0e439edaf) Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
-rw-r--r--Doc/library/smtplib.rst23
1 files changed, 11 insertions, 12 deletions
diff --git a/Doc/library/smtplib.rst b/Doc/library/smtplib.rst
index 2511ef7..7cd530a 100644
--- a/Doc/library/smtplib.rst
+++ b/Doc/library/smtplib.rst
@@ -556,34 +556,33 @@ This example prompts the user for addresses needed in the message envelope ('To'
and 'From' addresses), and the message to be delivered. Note that the headers
to be included with the message must be included in the message as entered; this
example doesn't do any processing of the :rfc:`822` headers. In particular, the
-'To' and 'From' addresses must be included in the message headers explicitly. ::
+'To' and 'From' addresses must be included in the message headers explicitly::
import smtplib
- def prompt(prompt):
- return input(prompt).strip()
+ def prompt(title):
+ return input(title).strip()
- fromaddr = prompt("From: ")
- toaddrs = prompt("To: ").split()
+ from_addr = prompt("From: ")
+ to_addrs = prompt("To: ").split()
print("Enter message, end with ^D (Unix) or ^Z (Windows):")
# Add the From: and To: headers at the start!
- msg = ("From: %s\r\nTo: %s\r\n\r\n"
- % (fromaddr, ", ".join(toaddrs)))
+ lines = [f"From: {from_addr}", f"To: {', '.join(to_addrs)}", ""]
while True:
try:
line = input()
except EOFError:
break
- if not line:
- break
- msg = msg + line
+ else:
+ lines.append(line)
+ msg = "\r\n".join(lines)
print("Message length is", len(msg))
- server = smtplib.SMTP('localhost')
+ server = smtplib.SMTP("localhost")
server.set_debuglevel(1)
- server.sendmail(fromaddr, toaddrs, msg)
+ server.sendmail(from_addr, to_addrs, msg)
server.quit()
.. note::