为什么python要求我在输入相同的东西之后一遍又一遍地输入它(smtpib gmail)

2024-03-29 06:45:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个脚本,允许用户发送电子邮件。但是,当用户对“Do you want to send another email”说“是”,然后对“Do you want to switch accounts”说“是”时,为什么登录不起作用?在我输入不同的用户名和密码后,它会一次又一次地请求它,即使凭据是正确的。这是我的密码:

end = ("false")
end2 = ("false")
while True:
    if (end == "true"):
        break
    print ("Contacting server...")
    user = smtplib.SMTP('smtp.gmail.com', 587)
    user.starttls()
    while True:
        euser = input("Enter Gmail Username: ")
        epass = input("Enter Gmail Password: ")
        try:
            user.login(euser, epass)
        except smtplib.SMTPAuthenticationError:
            print ("Invalid Credentials!")
            continue
        break
        while True:
            if (end == "true" or end2 == "true"):
                break
            user.login(euser, epass)
            print ("Retrieving Files")
            subjectinput = input("Enter Subject: ")
            msginput = input("Enter Message: ")
            msg = ("\n"+msginput)
            sourcemail = (euser)
            while True:
                targetmail = input("Enter Your Recipient: ")
                try:
                    user.sendmail(sourcemail, targetmail, 'Subject: {}\r\n\r\n{}'.format(subjectinput, msg))
                except (smtplib.SMTPRecipientsRefused):
                    print ("Invalid Recipient!")
                    continue
                break
            user.sendmail(sourcemail, targetmail, 'Subject: {}\r\n\r\n{}'.format(subjectinput, msg))
            print ("Email Sent")
            ask = input("Whould you like to send another one [Y/N]: ")
                if(ask == "n" or ask == "N" or ask == "no" or ask == "No"):
                    end = ("true")
                    break
                if(ask == "y" or ask == "Y" or ask == "yes" or ask == "Yes"):
                    newuser = input("Do you want to switch accounts [Y/N]: ")
                    if (newuser == "n" or newuser == "N" or newuser == "no" or newuser == "No"):
                        continue
                    if (newuser == "y" or newuser == "Y" or newuser == "yes" or newuser == "Yes"):
                        end2 = "true"
                        break

非常感谢您的回答。谢谢,我是python的新手:)


Tags: ortoyoutrueinputifaskend
1条回答
网友
1楼 · 发布于 2024-03-29 06:45:56

看看这部分:

while True:
    euser = input("Enter Gmail Username: ")
    epass = input("Enter Gmail Password: ")
    try:
        user.login(euser, epass)
    except smtplib.SMTPAuthenticationError:
        print ("Invalid Credentials!")
        continue
    break

当登录成功时,执行的下一个语句是break,这会将您返回到外部while循环的开头。您需要将break移动到try语句中(并且您可以删除continue,因为您现在处于循环的末尾),并删除以下while循环,使其与上一个循环处于同一级别:

while True:
    euser = input("Enter Gmail Username: ")
    epass = input("Enter Gmail Password: ")
    try:
        user.login(euser, epass)
        break
    except smtplib.SMTPAuthenticationError:
        print ("Invalid Credentials!")

while True:
    if end == "true" or end2 == "true": # better use booleans and `if end or end2:`
        break
    # unnecessary: user.login(euser, epass)
    print ("Retrieving Files")

相关问题 更多 >