Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-120662: improve smtplib example #120668

Merged
merged 6 commits into from
Jun 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions Doc/library/smtplib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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()
picnixz marked this conversation as resolved.
Show resolved Hide resolved
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))
AlexWaygood marked this conversation as resolved.
Show resolved Hide resolved

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::
Expand Down
Loading