@@ -556,34 +556,33 @@ This example prompts the user for addresses needed in the message envelope ('To'
556556and 'From' addresses), and the message to be delivered. Note that the headers
557557to be included with the message must be included in the message as entered; this
558558example 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