Forums

Not the usual "can't sent email through Google SMTP" issue

I am aware of the following up front:

  1. Google requires the less secure apps setting
  2. 2.7 is not the most efficient version to do this in Python
  3. UTF-8 is unnecessary with this character list, but I have plans to expand so I have that in place for a new phase

I'm not interested in learning 3.X Python right now plus this is for a spy game so date sensitivity was important. With that out of the way, the issue is a PythonAnywhere issue. The following code is the culprit:

# -*- coding: utf-8 -*-
# Base character list
cipher = [" ","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9",".",",","!","?","@","(",")","[","]","$","%","-","+","=","#","*","&","~","/","_","'",'"'];

# Random number generator to initiate blind encryption
from random import randint
lock = randint(1,len(cipher)) #Inclusive

# Defined variables, lists, etc as needed
retain = []
n = len(cipher)
#key = n - lock
exhibit = []
secret = ''

unlock = raw_input('Enter your key (For decryption only): ') # Flag for decryption
conceal = raw_input('Enter your message now: ') # Original message from user

# Loop to find data
for i in range(len(conceal)):
    # Returns index (in cipher) of each character from input
    retain.append(cipher.index(conceal[i]))
    # Determine if we need to Encrypt or Decrypt
    if unlock == '':
        # Enforce key is only used for decryption
        key = n - lock
        # Offsets index above of each character from input by lock
        # The mod allows lock to wrap around to index 0 and keep going
        exhibit.append(cipher.index(conceal[i]) + lock % n)
        # And print string without quotes and commas
        j = (exhibit[i] % n)
        secret += ''.join(cipher[j])

    else:
        # Enforce key is taken from user
        key = int(unlock)
        exhibit.append(cipher.index(conceal[i]) + key % n)
        # And print string without quotes and commas
        j = (exhibit[i] % n)
        secret += ''.join(cipher[j])

if key == n - lock:
    # Import smtplib for the actual sending function
    import smtplib

    # Import the email modules we'll need
    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEText import MIMEText

    fromaddr = 'XXX@gmail.com' # Standard Sending Address
    username = 'XXX@gmail.com'
    toaddr = raw_input('Enter your drop address now: ') # Recipient Email Address
    password = '*******'

    msg = MIMEMultipart()
    msg['From'] = fromaddr
    msg['To'] = toaddr
    msg['Subject'] = 'Message from Agent %s' % key

    body = secret + '\n' + '\n' + 'This message has been delivered through the Dead Drop Service. You may also use this service to decrypt your message. Please visit lalala to began the decryption process.'

    msg.attach(MIMEText(body, 'plain'))

    # Send the message via our own SMTP server, with normal headers, subject, and body.
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    server.login(username, password)
    text = msg.as_string()
    server.sendmail(fromaddr, toaddr, text)
    server.quit()
else:
    # Prints indices
    #print(retain)
    #print(lock)
    #print(key)
    #print(exhibit)#.encode)
    print(secret)#.encode)

This wonderful bit of coding gives me the following error:

Traceback (most recent call last): File "DeadDropEng.py", line 68, in <module> server.login(username, password) File "/usr/lib/python2.7/smtplib.py", line 622, in login raise SMTPAuthenticationError(code, resp) smtplib.SMTPAuthenticationError: (534, '5.7.14 https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbtuH\n5.7.14 VKsANiVYRXimrcvXWOIZAF3sShMn0NNVlRkje9m9Prk2YdAhrLEq_6hfAv2dM-G9b5MZNr\n5.7.14 MOtoKuL9I8f1fXp7AUBT3THasSG_p6q0pYHQxJOL45anuHlHvtxH2P3YWKoVv_TsgN3pw5\n5.7.14 nR7zZu3zaLCHVdxfO0J_zB37eiAbXKznsJI9BzW5iKt-5SyH-XS04UzO3bP_Hk79XEnOQR\n5.7.14 7ELs3LA2Kb6HIA-at0tLB0mz6iks Please log in via your web browser and\n5.7.14 then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/answer/78754 b201sm7661962qhb.48 - gsmtp')

BUT if I run this same code on my desktop, it works perfectly. Sends the email without any issues or errors. Any clues?

Thanks in advance.

It sounds like they're being more careful with logins from our servers (which are based on Amazon EC2) than they are with connections from your home machine (which they've presumably seen logins directly to Gmail coming from). You could try visiting that link, perhaps using the text-only Lynx browser from a bash console?

Thank you giles! That worked. It's just a lengthy process to login to Gmail through Lynx. So that's excellent to know for others.

Great! Thanks for confirming it worked :-)

Hi, yesterday I've succeeded via above lynx trick, and email was sent. But for scheduled tasks (for example today), still same problem. If someone have idea (for example instead gmail to use another smtp), will be glad to hear.

Less secure app gmail setting is on

Trying now with
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.ehlo()
instead previosly only
smtpObj.ehlo()
smtpObj.starttls()
Let see would this help

I also think that getting an app specific password may work.

https://security.google.com/settings/security/apppasswords Says: "The setting you are looking for is not available for your account."

Probably I'll need 2 step verification to enable it, which I tend to avoid.

However, I've received 2 notify emails for now good, had to go here https://accounts.google.com/DisplayUnlockCaptcha and then send mail from PA.

Question remains how this will be reliable :)

Since topic using gmail smtp can be really painful at present, I've ended using https://www.smtp2go.com/

1000 free emails per month.

Thanks for the link!

For those who don't want to pay extra for an smtp service instead of free Gmail:

1) configuring 2-factor on a dedicated Gmail account is absolutely no pain and takes about 15 minutes;

2) when you get the app password (or whatever they name it, I just can't remember the exact term, since I went through this a year ago), just save it an environment variable (for 12-factor adepts) or in the setting file (for those who don't care much), and that's it.

So, just one line in settings.py enables your app to send emails with a Gmail account for free (mind daily limits though). After all the work done, just one step :-) P.S. And yes, when I first faced that 2-factor requirement, I was not happy with extra stuff to do. But later it made sense.