From 26652fbb94fa3e7942b3c83a0eaebbea12a892ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Tue, 18 Jun 2024 11:01:01 +0200 Subject: [PATCH 1/6] improve `smtplib` example --- Doc/library/smtplib.rst | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/Doc/library/smtplib.rst b/Doc/library/smtplib.rst index 2511ef7f2ada41..ca712ec1b6198b 100644 --- a/Doc/library/smtplib.rst +++ b/Doc/library/smtplib.rst @@ -556,35 +556,32 @@ 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 + import smtplib - def prompt(prompt): + def prompt(prompt): return input(prompt).strip() - fromaddr = prompt("From: ") - toaddrs = prompt("To: ").split() - print("Enter message, end with ^D (Unix) or ^Z (Windows):") + 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))) - while True: + # Add the From: and To: headers at the start! + lines = [f"From: {from_addr}", f"To: {', '.join(to_addrs)}", ""] + while True: try: - line = input() + lines.append(input()) except EOFError: break - if not line: - break - msg = msg + line - print("Message length is", len(msg)) + msg = '\r\n'.join(lines) + print("Message length is", len(msg)) - server = smtplib.SMTP('localhost') - server.set_debuglevel(1) - server.sendmail(fromaddr, toaddrs, msg) - server.quit() + server = smtplib.SMTP('localhost') + server.set_debuglevel(1) + server.sendmail(from_addr, to_addrs, msg) + server.quit() .. note:: From 064e9baead84d05f3b9b56d94660d7666dd5c7cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Tue, 18 Jun 2024 11:56:24 +0200 Subject: [PATCH 2/6] address Alex's review --- Doc/library/smtplib.rst | 50 +++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/Doc/library/smtplib.rst b/Doc/library/smtplib.rst index ca712ec1b6198b..9e4457f34b602d 100644 --- a/Doc/library/smtplib.rst +++ b/Doc/library/smtplib.rst @@ -558,30 +558,32 @@ 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:: - import smtplib - - def prompt(prompt): - return input(prompt).strip() - - 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! - lines = [f"From: {from_addr}", f"To: {', '.join(to_addrs)}", ""] - while True: - try: - lines.append(input()) - except EOFError: - break - - msg = '\r\n'.join(lines) - print("Message length is", len(msg)) - - server = smtplib.SMTP('localhost') - server.set_debuglevel(1) - server.sendmail(from_addr, to_addrs, msg) - server.quit() + import smtplib + + def prompt(prompt): + return input(prompt).strip() + + 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! + lines = [f"From: {from_addr}", f"To: {', '.join(to_addrs)}", ""] + while True: + try: + line = input() + except EOFError: + break + else: + lines.append(line) + + msg = '\r\n'.join(lines) + print("Message length is", len(msg)) + + server = smtplib.SMTP('localhost') + server.set_debuglevel(1) + server.sendmail(from_addr, to_addrs, msg) + server.quit() .. note:: From 848c2fc8fc3d81d9a2a61f3d081c5211139b88dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Tue, 18 Jun 2024 11:57:09 +0200 Subject: [PATCH 3/6] fix indent --- Doc/library/smtplib.rst | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Doc/library/smtplib.rst b/Doc/library/smtplib.rst index 9e4457f34b602d..7d50e606bf57aa 100644 --- a/Doc/library/smtplib.rst +++ b/Doc/library/smtplib.rst @@ -570,12 +570,12 @@ example doesn't do any processing of the :rfc:`822` headers. In particular, the # Add the From: and To: headers at the start! lines = [f"From: {from_addr}", f"To: {', '.join(to_addrs)}", ""] while True: - try: - line = input() - except EOFError: - break - else: - lines.append(line) + try: + line = input() + except EOFError: + break + else: + lines.append(line) msg = '\r\n'.join(lines) print("Message length is", len(msg)) From 87281a7fbfecbf560d10b444fd7872af0a2985b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Tue, 18 Jun 2024 11:57:28 +0200 Subject: [PATCH 4/6] fix indent --- Doc/library/smtplib.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/smtplib.rst b/Doc/library/smtplib.rst index 7d50e606bf57aa..344a9238ae3b32 100644 --- a/Doc/library/smtplib.rst +++ b/Doc/library/smtplib.rst @@ -561,7 +561,7 @@ example doesn't do any processing of the :rfc:`822` headers. In particular, the import smtplib def prompt(prompt): - return input(prompt).strip() + return input(prompt).strip() from_addr = prompt("From: ") to_addrs = prompt("To: ").split() From c2432301b5a4805f2461e8ab3571e7bcdaa053a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Tue, 18 Jun 2024 12:33:49 +0200 Subject: [PATCH 5/6] cleanup --- Doc/library/smtplib.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Doc/library/smtplib.rst b/Doc/library/smtplib.rst index 344a9238ae3b32..0e6a49095d2496 100644 --- a/Doc/library/smtplib.rst +++ b/Doc/library/smtplib.rst @@ -560,8 +560,8 @@ example doesn't do any processing of the :rfc:`822` headers. In particular, the import smtplib - def prompt(prompt): - return input(prompt).strip() + def prompt(title): + return input(title).strip() from_addr = prompt("From: ") to_addrs = prompt("To: ").split() @@ -577,10 +577,10 @@ example doesn't do any processing of the :rfc:`822` headers. In particular, the else: lines.append(line) - msg = '\r\n'.join(lines) + 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(from_addr, to_addrs, msg) server.quit() From 1547a6106ce9ca59a145f440630055efa1d16c94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Tue, 18 Jun 2024 12:51:14 +0200 Subject: [PATCH 6/6] Update Doc/library/smtplib.rst Co-authored-by: Alex Waygood --- Doc/library/smtplib.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/smtplib.rst b/Doc/library/smtplib.rst index 0e6a49095d2496..7cd530a5fd6438 100644 --- a/Doc/library/smtplib.rst +++ b/Doc/library/smtplib.rst @@ -577,7 +577,7 @@ example doesn't do any processing of the :rfc:`822` headers. In particular, the else: lines.append(line) - msg = "\r\n'.join(lines) + msg = "\r\n".join(lines) print("Message length is", len(msg)) server = smtplib.SMTP("localhost")