Python - Cannot Send Email

780 views Asked by At

I am running Python 3.4.2, on Windows 7 Enterprise, 64 bit.

I have the below script (write_email.py) that receives an error when run:

# smtplib module send mail

import smtplib

TO = '[email protected]'
SUBJECT = 'TEST MAIL'
TEXT = 'Here is a message from python.'

# Gmail Sign In
gmail_sender = '[email protected]'
gmail_passwd = ''   #this is left blank on purpose

server = smtplib.SMTP('smtp-relay.gmail.com', 25)
server.ehlo()
server.starttls()
server.login(gmail_sender, gmail_passwd)

BODY = '\r\n'.join(['To: %s' % TO,
                'From: %s' % gmail_sender,
                'Subject: %s' % SUBJECT,
                '', TEXT])

try:
    server.sendmail(gmail_sender, [TO], BODY)
print ('email sent')
except:
    print ('error sending mail')

server.quit()

The error is:

Traceback (most recent call last): 
File "email_test.py", line 17, in <module>
server.login(gmail_sender, gmail_passwd)
File "C:\Python34\lib\smtplib.py", line 652, in login
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (501, b'5.5.2 Cannot Decode response       13sm147030
88ith.4 - gsmtp')

I've done a lot of searching to resolve this, but so far no luck.

0

There are 0 answers