我的电子邮件程序通过电子邮件将每行邮件作为单独的电子邮件发送,我想知道如何在一封电子邮件中发送所有邮件,当电子邮件发送时,程序将返回到开头。
import smtplib
from email.mime.text import MIMEText
a = 1
while a == 1:
print " "
To = raw_input("To: ")
subject = raw_input("subject: ")
input_list = []
print "message: "
while True:
input_str = raw_input(">")
if input_str == "." and input_list[-1] == "":
break
else:
input_list.append(input_str)
for line in input_list:
# Create a MIME text message and populate its values
msg = MIMEText(line)
msg['Subject'] = subject
msg['From'] = '123@example.com'
msg['To'] = To
server = smtplib.SMTP_SSL('server', 465)
server.ehlo()
server.set_debuglevel(1)
server.ehlo()
server.login('username', 'password')
# Send a properly formatted MIME message, rather than a raw string
server.sendmail('user@example.net', To, msg.as_string())
server.close()
(需要多行的部分是在Paul Griffiths的帮助下完成的,Multiline user input python)
答案 0 :(得分:3)
for line in input_list:
# Create a MIME text message and populate its values
msg = MIMEText(line)
您正在此循环中调用server.sendmail
。
首先构建整个msg(循环播放),然后添加所有标题并发送消息。