@@ -556,34 +556,33 @@ This example prompts the user for addresses needed in the message envelope ('To'
556
556
and 'From' addresses), and the message to be delivered. Note that the headers
557
557
to be included with the message must be included in the message as entered; this
558
558
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::
560
560
561
561
import smtplib
562
562
563
- def prompt(prompt ):
564
- return input(prompt ).strip()
563
+ def prompt(title ):
564
+ return input(title ).strip()
565
565
566
- fromaddr = prompt("From: ")
567
- toaddrs = prompt("To: ").split()
566
+ from_addr = prompt("From: ")
567
+ to_addrs = prompt("To: ").split()
568
568
print("Enter message, end with ^D (Unix) or ^Z (Windows):")
569
569
570
570
# 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)}", ""]
573
572
while True:
574
573
try:
575
574
line = input()
576
575
except EOFError:
577
576
break
578
- if not line:
579
- break
580
- msg = msg + line
577
+ else:
578
+ lines.append(line)
581
579
580
+ msg = "\r\n".join(lines)
582
581
print("Message length is", len(msg))
583
582
584
- server = smtplib.SMTP(' localhost' )
583
+ server = smtplib.SMTP(" localhost" )
585
584
server.set_debuglevel(1)
586
- server.sendmail(fromaddr, toaddrs , msg)
585
+ server.sendmail(from_addr, to_addrs , msg)
587
586
server.quit()
588
587
589
588
.. note ::
0 commit comments