Moe
Moe

Reputation: 23

Got an error by sending emails using python 2 and smtplib

EDIT: The main error is when this script runs from different IP / Wifi or whatever. It will just cancel it like skip over it or whatever.

I'm trying to send an email with text that says something. (or I can insert a variable for example: score = 32 and I would put 'score' in body without the ' '.)

The following code is what I'm using:

import smtplib

gmail_user = '[email protected]'  
gmail_password = 'password'

sent_from = gmail_user  
to = '[email protected]'  
subject = 'OMG Super Important Message'  
body = 'blah blah blah this is a message' 

email_text = """\  
From: %s  
To: %s  
Subject: %s

%s
""" % (sent_from, ", ".join(to), subject, body)

try:  
    server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
    server.ehlo()
    server.login(gmail_user, gmail_password)
    server.sendmail(sent_from, to, email_text)
    server.close()

    print 'Email sent!'
except:  
    print 'Something went wrong...'

So where it says 'body' I can put like a variable not just a text and it would send. But now,

when I send a program to someone and they go through the steps until this and the script will just skip it because the print 'loaded successful' one won't print meaning it didn't work. Any help on how to fix?

Upvotes: 1

Views: 539

Answers (1)

Pedro Lobito
Pedro Lobito

Reputation: 99011

You'll have to enable less secure apps in order to access your gmail account via smtplib.


Let less secure apps access your account

If an app or device doesn’t meet our security standards, Google will block anyone who tries to sign in from that app or device. Because these apps and devices are easier to break into, blocking them helps keep your account safe.

Some examples of apps that do not support the latest security standards include:

  • The Mail app on your iPhone or iPad with version 6 or below
  • The Mail app on your Windows phone preceding the 8.1 release
  • Some Desktop mail clients like Microsoft Outlook and Mozilla Thunderbird

Change account access for less secure apps

To help keep Google Accounts through work, school, or other groups more secure, we block some less secure apps from using them. If you have this kind of account, you’ll see a "Password incorrect" error when trying to sign in. If so, you have two options:

  • Option 1: Install a more secure app that uses stronger security measures. All Google products, like Gmail, use the latest security measures.
  • Option 2: Change your settings to allow less secure apps into your account. We don't recommend this option because it can make it easier for someone to break into your account. If you want to allow access anyway, follow these steps:
    1. Go to the "Less secure apps" section of my Account.
    2. Turn on Allow less secure apps. (Note: If your administrator has locked less secure app account access, this setting is hidden.)

If you still can't sign in to your account, learn more about the "password incorrect" error.

Upvotes: 1

Related Questions