Search code examples
pythonsmtpsmtplib

Having trouble with credentials (google smtp)


I'm a very new Python coder so please don't go too harsh on me, thanks.

I'm trying to make an emailer using smtplib and I'm having trouble with handing the users credentials to Google.

Full code:

mailcheck = input("This mailer is only for gmail, want to continue? (yes or no)")

# GMAIL SMTP INFORMATION
if mailcheck == "yes" or mailcheck == "Yes" or mailcheck == "y":
    smtp_server = 'smtp.gmail.com'
    port = 587
    set_server = "gmail"
else:
    print("Hey, sorry this program is only for Gmail")

#USER CREDENTIALS + RECEIVER

username = input("Google Email?")
password = input("Password?")
receiver = input("Who to send it to?")
subject = input("Subject of the email?")
econtents = input("What to put in the email?")
amount = input("Amount of emails to send?")

credentials = username + password
global d1
global d2

try:
    server = smtplib.SMTP(smtp_server, port)
    server.ehlo()

    server.starttls()
    server.login(username, password)

    print("Sending" + amount, "to", receiver)

    for i in range(1, int(amount) + 1):
        message = "From" + credentials[0] + '\nSubject' + subject + '\n' + econtents
        time.sleep(random.uniform(d1, d2))
        server.sendmail(credentials[0], receiver, message)
        print("\nEmail Sent!")

    else:
        print("Finished sending emails")

except smtplib.SMTPRecipientsRefused:
        print("Recipient refused. Invalid email address?")

except smtplib.SMTPAuthenticationError:
        print("Unable to authenticate with server. Ensure the details are correct.")

except smtplib.SMTPServerDisconnected:
        print("Server disconnected for an unknown reason.")

except smtplib.SMTPConnectError:
        print("Unable to connect to server.")

The error :

Unable to authenticate with server. Ensure the details are correct.

This means it went wrong with the login process. It should be going wrong somewhere in this part:

 #USER CREDENTIALS + RECEIVER

username = input("Google Email?")
password = input("Password?")
receiver = input("Who to send it to?")
subject = input("Subject of the email?")
econtents = input("What to put in the email?")
amount = input("Amount of emails to send?")

credentials = username + password
global d1
global d2

try:
    server = smtplib.SMTP(smtp_server, port)
    server.ehlo()

    server.starttls()
    server.login(username, password)

    print("Sending" + amount, "to", receiver)

    for i in range(1, int(amount) + 1):
        message = "From" + credentials[0] + '\nSubject' + subject + '\n' + econtents
        time.sleep(random.uniform(d1, d2))
        server.sendmail(credentials[0], receiver, message)
        print("\nEmail Sent!")

I think it's because of the credentials = username + password which doesn't work, but I have no idea how I'd fix it.

If anyone knows what I'd have to change to fix this that'd be great!


Solution

  • Instead of adding those two strings, you're meaning to put them in an array. In Python, that's either

    credentials = [username, password]
    

    or

    credentials = list(username, password)
    

    But that doesn't seem to be your issue. Your issue is related to the login() function as you get the SMTPAuthenticationError exception. The smtplib documentation says that after running .starttls(), you should run .ehlo() again. Try to run that before logging in. Additionally, you could try to generate an SSL instance on port 465.

    (Ctrl+F for .starttls())

    https://docs.python.org/2/library/smtplib.html