0

So I have tried at least 5-10 examples online to send an email, but I keep getting the same error.

Traceback (most recent call last):
  File "C:/Users/alxu/Project/LogFilter.py", line 88, in ?
    smtpserver = smtplib.SMTP("smtp.gmail.com",587)
  File "C:\Python24\lib\smtplib.py", line 244, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Python24\lib\smtplib.py", line 292, in connect
    for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
socket.gaierror: (11004, 'getaddrinfo failed')

The last example I used code was like this...

import smtplib

to = '[email protected]'
gmail_user = '[email protected]'
gmail_pwd = 'yourpassword'
smtpserver = smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_pwd)
header = 'To:' + to + '\n' + 'From: ' + gmail_user + '\n' + 'Subject:testing \n'
print header
msg = header + '\n this is test msg from mkyong.com \n\n'
smtpserver.sendmail(gmail_user, to, msg)
print 'done!'
smtpserver.close()

I am using Python 2.4.3.

EDIT: McAfee is has host intrusion prevention so all the times I attempted to do this it blocked it.

6
  • possible duplicate of How to fix socket.gaierror: (11004, 'getaddrinfo failed') error in GAE? Commented Jul 9, 2015 at 20:24
  • @alfasin I tried the suggestions in the post.. I opened the host file then I added the values. 0:0.. wasnt in my host file to begin with. Commented Jul 9, 2015 at 20:31
  • 127.0.0.1 smtp.gmail.com ::1 smtp.gmail.com right? Commented Jul 9, 2015 at 20:32
  • @alfasin Is that what it mean when it says myhostname Commented Jul 9, 2015 at 20:33
  • Are you behind a corporate proxy? Commented Jul 9, 2015 at 20:41

2 Answers 2

1

Since you are behind a corporate proxy, you can't directly connect to any outside server, you need to connect through the proxy. In this case, you're trying to connect to a SMTP server, which uses port 25 or 587. I assume you're behind a HTTP proxy that doesn't block SMTP ports. If your corporate proxy blocks these ports, then there's nothing you can do to connect to the server.

The smtplib module doesn't include the functionality to connect to a SMTP server through a proxy. As a workaround, I wrote the custom class ProxySMTP, which I have posted here along with instructions on how to use it.

Let me know if it works for you.

Sign up to request clarification or add additional context in comments.

Comments

1

Your last example code should work. One thing you need to do is to make sure you allow access to less secure apps by going to the following link.

google settings for secure apps

The reason for this is google flags such automated email scripts as less secure. You should also be aware of the risks. Make sure you change this setting back if you are not gonna need it.

2 Comments

I changed it and test it.. Still got the same error. I think @zenadix is correct.
When I changed the setting earlier, it did not work immediately. I do not know why it would take time but without any other changes, it started working again in a day.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.