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 = 'mkyong2002@yahoo.com'
gmail_user = 'mkyong2002@gmail.com'
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.

Alan
  • 131
  • 1
  • 11

2 Answers2

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.

Sriharsha Madala
  • 275
  • 3
  • 10
  • I changed it and test it.. Still got the same error. I think @zenadix is correct. – Alan Jul 09 '15 at 21:05
  • 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. – Sriharsha Madala Jul 09 '15 at 21:12
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.

Community
  • 1
  • 1
Zenadix
  • 15,291
  • 4
  • 26
  • 41