需要帮助创建一个Python循环。

huangapple go评论61阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2023年5月22日 08:53:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76302497.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定