syscharger
Posts: 19
Joined: Wed Aug 26, 2015 8:46 am

Problem sending SMTP Email

Wed Jan 13, 2016 10:04 am

i have been trying to send email using these codes.
but will receive the following error code occasionally:

[Errno -5] No address associated with hostname

Any idea why? My success rate of sending this email is probably 0-20%.
Is there a socket or timeout error? or??
Please advise. Many Thanks.

Code: Select all

#!/usr/bin/env python
import smtplib
from time import sleep

GMAIL_USER = 'XXX@gmail.com'
GMAIL_PASS = 'YYY'
SMTP_SERVER = 'smtp.gmail.com'
EMAIL_ADDRESS1 = 'ZZZ@gmail.com'
SMTP_PORT = 587

def send_email(recipient, subject, text):
        try:   
                smtpserver = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
                smtpserver.ehlo()
                smtpserver.starttls()
                smtpserver.ehlo()
                smtpserver.login(GMAIL_USER, GMAIL_PASS)
                header = 'To:' + recipient + '\n' + 'From: ' + GMAIL_USER
                header = header + '\n' + 'Subject:' + subject + '\n'
                msg = header + '\n' + text + ' \n\n'
                smtpserver.sendmail(GMAIL_USER, recipient, msg)
                smtpserver.quit()
                print ("Successfully SENT")
        except Exception, msg:
                print ("Failure:  " + str(msg))
       
cnt = 0
while (cnt < 10):
        send_email(EMAIL_ADDRESS1, 'Test Email', 'Thank You')
        cnt = cnt + 1
        sleep(5)
Last edited by syscharger on Thu Jan 14, 2016 2:04 am, edited 1 time in total.

Heater
Posts: 15949
Joined: Tue Jul 17, 2012 3:02 pm

Re: Problem sending SMTP Email

Wed Jan 13, 2016 10:56 am

Are you really intending to send 10 mails in rapid succession and then sleep for 5 seconds?
That what your while loop seems to be doing.

I wouldn't be surprised if gmail did reject that as being some kind of SPAM or abuse. Not that I have any idea how they deal really deal with such things.

What happens when you send the mail just once?
Memory in C++ is a leaky abstraction .

User avatar
rpdom
Posts: 17173
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Problem sending SMTP Email

Wed Jan 13, 2016 11:01 am

Heater wrote:Are you really intending to send 10 mails in rapid succession and then sleep for 5 seconds?
That what your while loop seems to be doing.
It looks to me that it is going to send 10 mails 5 seconds apart, then stop. The sleep(5) is inside the loop.

Heater
Posts: 15949
Joined: Tue Jul 17, 2012 3:02 pm

Re: Problem sending SMTP Email

Wed Jan 13, 2016 11:16 am

Ah, yes, sorry. Read that to quickly before my morning coffee. Python's lack of any sensible syntax confused me :)

I'd be testing that with a delay of one minute or so. Or just send one mail every time you run the test.


When I run that code, with proper gmail addresses, I get:

Code: Select all

Failure:  global name 'GMAIL_USER' is not defined
If I change MAIL_USER to GMAIL_USER I get:

Code: Select all

Failure:  (534, '5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbsLC\n5.7.14 n379hXdXpNNcxj_DmizqoalxlPR2lxK8tEyRFwLpfpPQ0YCrm8olkdWP51IMfykBsDGP63\n5.7.14 qRuiGwm4PTk41cJs6PQ_FCbMNCqOM3hPCZuRpbul3oKqb0bQL8nw9k-aWYs3jmZYaTCn-p\n5.7.14 Mh-3d-BK2jswu_W2cISikdGpeQX4zRDsSr1P7i-SQ6I747QdPmyHvXDzuBoWbw2pr4IcTR\n5.7.14 OlI_zg3yGXnHB3wfNOvi2fouGnTo> 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 pq10sm110522lbc.34 - gsmtp')
Memory in C++ is a leaky abstraction .

Heater
Posts: 15949
Joined: Tue Jul 17, 2012 3:02 pm

Re: Problem sending SMTP Email

Wed Jan 13, 2016 11:32 am

Of course one should only login to the gmail account once when the program starts up.

So even if the login did work I'd move all the login stuff out of the loop.

Given that the error says: "Please log in via your web browser and then try again." I imagine there are some settings in your gmail account you can tweak to make it allow such non-browser logins.

The error message also says: "Learn more at https://support.google.com/mail/answer/78754". Sure enough there are some good clues at that URL.

Here is a better example anyway:

http://jmduke.com/posts/how-to-send-ema ... and-gmail/
Memory in C++ is a leaky abstraction .

Heater
Posts: 15949
Joined: Tue Jul 17, 2012 3:02 pm

Re: Problem sending SMTP Email

Wed Jan 13, 2016 12:22 pm

When I run syscharger's original code, with the GMAIL_USER corrected it fails.

Then gmail sends me a mail that warns that someone has been trying to log in as me:

"Sign-in attempt prevented ... Someone just tried to sign in to your Google Account fred.blogs@gmail.com from an app that doesn't meet modern security standards."

As I said. There may be some settings on your gmail account you can tweak to tell it to allow your connection.


See the discussion of "less secure apps" here : http://stackoverflow.com/questions/1014 ... ing-python
Memory in C++ is a leaky abstraction .

syscharger
Posts: 19
Joined: Wed Aug 26, 2015 8:46 am

Re: Problem sending SMTP Email

Thu Jan 14, 2016 2:07 am

Thanks for the replies....

GMAIL_USER was a typo on my side when i did a copy and paste.

The intention was to send 1 email, but unfortunately the success rate is very low, hence i decided to send a succession of 10 emails to see the outcome. In most cases, i only manage to send 2-3 emails out of 10.
Yes, i have enabled the GMAIL - Less secure apps during my test.

I wanted to know, why i cant succeed in the 1st attempt of sending email.

Return to “General discussion”