Why does python ask me to input the same thing over and over again after it has been already entered - (smtpib - gmail)

StackOverflow https://stackoverflow.com/questions/22125085

質問

I have this script that allows the user to send emails. However, when the user says yes to "Do you want to send another email" and then yes to "Do you want to switch accounts", why does the login then not work? After I enter a different username and password, it just asks for it again and again, even though the credentials are correct. Here is my code:

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

Any answers are greatly appreciated. Thanks, I am fairly new at python :)

役に立ちましたか?

解決

Look at this part:

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

When login succeeds, the next statement that is executed is break, and that throws you back to the beginning of the outer while loop. You need to move the break into the try statement (and you can remove the continue because you're now at the end of the loop anyway), and outdent the following while loop so it's on the same level as the previous one:

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")
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top