DaftPunk
DaftPunk

Reputation: 103

sending email with google SMTP in python

Hi I'm attempting to send an email in python over google SMTP
i think the code is correct but i'm getting the following exception ..

"G:\Installed Applications\Python\Python35-32\python.exe" "C:/Users/brand/Desktop/Test Projects/Python Projects/SMTP.py"
Traceback (most recent call last):
  File "C:/Users/brand/Desktop/Test Projects/Python Projects/SMTP.py", line 22, in <module>
    server.login(sender, password)
  File "G:\Installed Applications\Python\Python35-32\lib\smtplib.py", line 730, in login
    raise last_exception
  File "G:\Installed Applications\Python\Python35-32\lib\smtplib.py", line 721, in login
    initial_response_ok=initial_response_ok)
  File "G:\Installed Applications\Python\Python35-32\lib\smtplib.py", line 627, in auth
    initial_response = (authobject() if initial_response_ok else None)
  File "G:\Installed Applications\Python\Python35-32\lib\smtplib.py", line 664, in auth_login
    raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (502, b'5.5.1 Unrecognized command. m16sm36193099wmb.13 - gsmtp')

Process finished with exit code 1

my code is shown here :

import smtplib

sender = '[email protected]' #  will be replaced with my real email address

password = 'mypassword' #  will be replaced with my real password

receivers = ['[email protected]']

message = """From: DaftPunk <[email protected]>
To: BlueStar <[email protected]>
MIME-Version: 1.0
Content-Type: text/html
Subject: Test E-Mail

Please visit this new website
<a href="http://www.google.com">http://www.google.com</a>
"""

server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(sender, password) #  Exception here
try:
    server.sendmail(sender, receivers, message)
    print("Message sent successfully")
except:
    print("Failed to send message")
server.quit()

can someone explain what's wrong or what i'm missing ?

Upvotes: 2

Views: 3442

Answers (1)

DaftPunk
DaftPunk

Reputation: 103

I've found the solution, there's nothing wrong with the code
it's a security issue in Google mail .. i followed this link and turned on access for less secure apps, now the script works as a charm! :)


Reference: Allowing less secure apps to access your account - Account Help

Upvotes: 5

Related Questions