DEV Community

Cover image for Python Scripts-E-mail sender
Envoy-VC
Envoy-VC

Posted on • Updated on

Python Scripts-E-mail sender

import smtplib 
# creates SMTP session 
s = smtplib.SMTP('smtp.gmail.com', 587) 
# start TLS for security 
s.starttls() 
# Authentication 
s.login("email-id", "password") 
# message to be sent 
message = "message sent using python"
# sending the mail 
s.sendmail("senders_email_id", "recievers_email_id", message) 
# terminating the session 
s.quit()
Enter fullscreen mode Exit fullscreen mode

If you get a SMTPAuthenticationError follow these two steps

Step 1: Ensure your password is correct

Copy and paste your password to ensure there are no typos. Don't think too highly of yourself to believe you are incapable of making this mistake! Additionally, if you are using 2-factor authentication, you will be required to use application-specific passwords instead of your normal password.

Step 2: Disabling CAPTCHA for clients

If you are not using 2-factor authentication and you have verified the credentials in your Python source are correct, follow these steps:

  1. Login to gmail in your browser
  2. Navigate to the DisplayUnclockCaptcha page.
  3. Click the continue button, and you will see the message

    Account access enabled Please try signing in to your Google account again from your new device or application.

  4. Run your Python script - your login attempt should be successful.

Discussion (0)