python 中的多个输入
Multiple input in 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)
所以我编写了这段代码,它发送我在输入字段中键入的消息。问题是我希望能够在输入字段中键入多行。该代码应该输入多行,但只是将最后一行发送到我的电子邮件。
这是我的代码:
# 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)