0

I'm trying to use the following script to send an email behind a proxy server.

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
import sys
import urllib2

fromaddr = "abc@gmail.com"
toaddr = "xyz@gmail.com"
cc = "123@gmail.com"
toaddrs = toaddr + cc
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "subject"
body = "body"
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login("user", "pass")
text = msg.as_string()
server.sendmail(fromaddr, toaddrs, text)
server.close()

It gives the following error:

Traceback (most recent call last):
File "script.py", line 18, in <module>
   server = smtplib.SMTP('smtp.gmail.com:587')
File "/usr/lib/python2.7/smtplib.py", line 251, in __init__
   (code, msg) = self.connect(host, port)
File "/usr/lib/python2.7/smtplib.py", line 311, in connect
   self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python2.7/smtplib.py", line 286, in _get_socket
   return socket.create_connection((host, port), timeout)
File "/usr/lib/python2.7/socket.py", line 553, in create_connection
   for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known

I have already set the http_proxy environment variable and fetching webpages using urllib2 in python works but not smtplib. Any help will be appreciated.

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
m_amber
  • 747
  • 3
  • 13
  • 23
  • SMTP to port 587 has nothing to do with HTTP or `urllib2`. It is a separate protocol to a different port. –  Aug 06 '14 at 15:18
  • @Tichodroma But the fact that he uses `http_proxy` gives us a clue. Perhaps that means he lives in a walled garden and cannot connect to `smtp.google.com` directly. – Robᵩ Aug 06 '14 at 15:21
  • 1
    @m_amber - What is the result of the command `telnet smtp.google.com 587` ? – Robᵩ Aug 06 '14 at 15:23
  • @Robᵩ `telnet smtp.google.com 587` gives the following output `telnet: could not resolve smtp.google.com/587: Name or service not known` – m_amber Aug 06 '14 at 15:31
  • 1
    Your problem has nothing to do with Python. It has to do with how your system and/or network are configured. Contact the people who own your network (probably your school or company's IT department) or maybe the folks at http://superuser.com/. – Robᵩ Aug 06 '14 at 16:06
  • @Robᵩ, I have no issue using `'smtp.gmail.com:587'` but `telnet smtp.google.com 587` woukld give me the same error – Padraic Cunningham Aug 06 '14 at 16:56
  • **mea cupla**. I apologize, @m_amber. I meant to say: "What is the result of the command `telnet smtp.gmail.com 587`?" – Robᵩ Aug 06 '14 at 17:38

1 Answers1

0

That should work:

In [1]: import smtplib

In [2]: server = smtplib.SMTP('smtp.gmail.com:587')

In [3]: server
Out[3]: <smtplib.SMTP at 0x7f80fbaa0ba8>

In [4]: server.ehlo()
Out[4]: 
(250,
 b'mx.google.com at your service, [46.237.207.180]\nSIZE 35882577\n8BITMIME\nSTARTTLS\nAUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN\nENHANCEDSTATUSCODES\nCHUNKING\nSMTPUTF8')

In [5]: server.starttls()
Out[5]: (220, b'2.0.0 Ready to start TLS')

Make sure your network allows connections to port 587 at this host.