mirror of https://github.com/python/cpython
gh-120662: Improve `smtplib` example (#120668)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
parent
3044d3866e
commit
4bc27abdbe
|
@ -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
|
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
|
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
|
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(title):
|
||||||
return input(prompt).strip()
|
return input(title).strip()
|
||||||
|
|
||||||
fromaddr = prompt("From: ")
|
from_addr = prompt("From: ")
|
||||||
toaddrs = prompt("To: ").split()
|
to_addrs = prompt("To: ").split()
|
||||||
print("Enter message, end with ^D (Unix) or ^Z (Windows):")
|
print("Enter message, end with ^D (Unix) or ^Z (Windows):")
|
||||||
|
|
||||||
# Add the From: and To: headers at the start!
|
# Add the From: and To: headers at the start!
|
||||||
msg = ("From: %s\r\nTo: %s\r\n\r\n"
|
lines = [f"From: {from_addr}", f"To: {', '.join(to_addrs)}", ""]
|
||||||
% (fromaddr, ", ".join(toaddrs)))
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
line = input()
|
line = input()
|
||||||
except EOFError:
|
except EOFError:
|
||||||
break
|
break
|
||||||
if not line:
|
else:
|
||||||
break
|
lines.append(line)
|
||||||
msg = msg + line
|
|
||||||
|
|
||||||
|
msg = "\r\n".join(lines)
|
||||||
print("Message length is", len(msg))
|
print("Message length is", len(msg))
|
||||||
|
|
||||||
server = smtplib.SMTP('localhost')
|
server = smtplib.SMTP("localhost")
|
||||||
server.set_debuglevel(1)
|
server.set_debuglevel(1)
|
||||||
server.sendmail(fromaddr, toaddrs, msg)
|
server.sendmail(from_addr, to_addrs, msg)
|
||||||
server.quit()
|
server.quit()
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
Loading…
Reference in New Issue