英文:
Need help creating a python loop
问题
以下是您的Python代码的中文翻译部分:
我对Python相当新,需要帮助将我的电子邮件发送器制作成一个循环。我希望代码能够循环,以便我可以输入多个电子邮件地址,同时绕过电子邮件密码(在第一次执行后)。
这是我的代码:
import smtplib, ssl
from email.message import EmailMessage
port = 465
password = input('输入密码并按回车键:')
sender = 'email@gmail.com'
receiver = input('输入要接收消息的电子邮件地址并按回车键:')
subject = "谢谢!"
body = """
"""
em = EmailMessage()
em['From'] = sender
em['To'] = receiver
em['Subject'] = subject
em.set_content(body)
context = ssl.create_default_context()
with smtplib.SMTP_SSL('smtp.gmail.com', port, context=context) as server:
server.login(sender, password)
server.sendmail(sender, receiver, em.as_string())
print('邮件已发送!')
希望这有助于您的项目。如果您需要进一步的帮助,请随时提出。
英文:
I am fairly new to python and need help making my email sender a loop. I want the code to loop so that I can enter multiple emails while bypassing the email password (after the first execution).
Here is my code:
import smtplib, ssl
from email.message import EmailMessage
port = 465
password = input('Type your password and press enter: ')
sender = 'email@gmail.com'
receiver = input('Enter the email you wish to receive the message and press enter: ')
subject = "Thank you!"
body = """
asdfgh
"""
em = EmailMessage()
em['From'] = sender
em['To'] = receiver
em['Subject'] = subject
em.set_content(body)
context = ssl.create_default_context()
with smtplib.SMTP_SSL('smtp.gmail.com', port, context=context) as server:
server.login(sender, password)
server.sendmail(sender, receiver, em.as_string())
print('Email sent!')
I do not know where to start. I only know the very basics of the for loop.
答案1
得分: 1
这是一个如何在循环中输入字符串的非常简单的示例:
receivers = []
while not receivers or receivers[-1]:
receivers.append(input(
'输入您希望接收消息的电子邮件并按回车键:'
))
receivers.pop()
这个while
循环将在列表为空(not receivers
)或列表的最后一个元素(receivers[-1]
)不为空时继续运行。这意味着它将一直提示您进行输入,直到输入一个空字符串。
循环结束后,receivers.pop()
将弹出列表的最后一个元素(您输入的空字符串,用于终止循环)。
一旦您有了列表,您可能希望使用 str.join
将其转换为单个字符串,例如:
em['To'] = ','.join(receivers)
英文:
Here is a very simple example of how you could input strings in a loop:
receivers = []
while not receivers or receivers[-1]:
receivers.append(input(
'Enter the email you wish to receive the message and press enter: '
))
receivers.pop()
This while
loop will continue while the list is empty (not receivers
) or the last element of the list (receivers[-1]
) is not empty. That means that it will keep prompting you for input until you enter an empty string.
After the loop is done, receivers.pop()
will pop off the last element of the list (the empty string you entered to terminate the loop).
Once you have the list, you might want to turn it into a single string using str.join
, e.g.:
em['To'] = ','.join(receivers)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论