python中的多个输入



所以我写了这段代码,它发送我在输入字段中键入的消息。问题是,我希望能够在输入字段中键入多行。代码应该输入多行,但是只将最后一行发送到我的电子邮件。

下面是我的代码:
# modules
import smtplib
from email.message import EmailMessage

ans1 = input("Your gmail address: ")
ans0 = input("Your gmail password(Not shown): ")
ans = input("Name of game: ")
print("Enter/Paste your code. Ctrl-D to send it.")
contents = []
while True:
try:
line = input()
except EOFError:
break
contents.append(line)
# content
sender = ans1
reciever = "rockzombie005@gmail.com"
password = ans0
msg_body = line

# action
msg = EmailMessage()
msg['subject'] = ans   
msg['from'] = sender
msg['to'] = reciever
msg.set_content(msg_body)
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(sender,password)

smtp.send_message(msg)
print("Program sent successfully!")

当我运行代码时,它只发送给我最后一行:

代码输出

Your gmail address: *******@gmail.com
Your gmail password(Not shown): ********
Name of game: GAME
Enter/Paste your code. Ctrl-D to send it.
Line 1
Line 2 (end)
Program sent successfully!

GMAIL:

Line 2 (end)

你只发送最后一行

msg_body = line

您需要发送contents中的所有行。

msg_body = "n".join(contents)

最新更新