8000 [3.12] gh-120662: Improve `smtplib` example (GH-120668) (#120682) · python/cpython@70d71fb · GitHub
[go: up one dir, main page]

Skip to content

Commit 70d71fb

Browse files
miss-islingtonpicnixzAlexWaygood
authored
[3.12] gh-120662: Improve smtplib example (GH-120668) (#120682)
gh-120662: Improve `smtplib` example (GH-120668) (cherry picked from commit 4bc27ab) Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
1 parent 5d997b5 commit 70d71fb

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

Doc/library/smtplib.rst

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -556,34 +556,33 @@ This example prompts the user for addresses needed in the message envelope ('To'
556556
and 'From' addresses), and the message to be delivered. Note that the headers
557557
to be included with the message must be included in the message as entered; this
558558
example doesn't do any processing of the :rfc:`822` headers. In particular, the
559-
'To' and 'From' addresses must be included in the message headers explicitly. ::
559+
'To' and 'From' addresses must be included in the message headers explicitly::
560560

561561
import smtplib
562562

563-
def prompt(prompt):
564-
return input(prompt).strip()
563+
def prompt(title):
564+
return input(title).strip()
565565

566-
fromaddr = prompt("From: ")
567-
toaddrs = prompt("To: ").split()
566+
from_addr = prompt("From: ")
567+
to_addrs = prompt("To: ").split()
568568
print("Enter message, end with ^D (Unix) or ^Z (Windows):")
569569

570570
# Add the From: and To: headers at the start!
571-
msg = ("From: %s\r\nTo: %s\r\n\r\n"
572-
% (fromaddr, ", ".join(toaddrs)))
571+
lines = [f"From: {from_addr}", f"To: {', '.join(to_addrs)}", ""]
573572
while True:
574573
try:
575574
line = input()
576575
except EOFError:
577576
break
578-
if not line:
579-
break
580-
msg = msg + line
577+
else:
578+
lines.append(line)
581579

580+
msg = "\r\n".join(lines)
582581
print("Message length is", len(msg))
583582

584-
server = smtplib.SMTP('localhost')
583+
server = smtplib.SMTP("localhost")
585584
server.set_debuglevel(1)
586-
server.sendmail(fromaddr, toaddrs, msg)
585+
server.sendmail(from_addr, to_addrs, msg)
587586
server.quit()
588587

589588
.. note::

0 commit comments

Comments
 (0)
0