Send email using smtplib and tor

891 views Asked by At

I'm trying to send an email while having tor proxy open below is a snippet of code. If I run each part separately they work (tor and send email).

import socks
import socket
import smtplib

socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS4, '127.0.0.1', 9050, True)
socket.socket = socks.socksocket

FROM = "test@test"
TO = ["test1@test1"]
SUBJECT = "SB"
TEXT = "test"
message = """\
From: %s
To: %s
Subject: %s
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
server = smtplib.SMTP('localhost')
server.sendmail(FROM, TO, message)
server.quit()

When I join them together I get the following error:

Traceback (most recent call last):
  File "em.py", line 20, in <module>
    server = smtplib.SMTP('localhost')
  File "/usr/lib/python2.7/smtplib.py", line 249, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/lib/python2.7/smtplib.py", line 309, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/usr/lib/python2.7/smtplib.py", line 284, in _get_socket
    return socket.create_connection((port, host), timeout)
  File "/usr/lib/python2.7/socket.py", line 562, in create_connection
    sock.connect(sa)
  File "/usr/local/lib/python2.7/dist-packages/socks.py", line 459, in connect
    dest_addr, dest_port = dest_pair
ValueError: too many values to unpack

Any help would be apprecaited

1

There are 1 answers

0
Sjon On

You're connecting to localhost; but if you're going through the TOR-proxy, you're basically asking the tor-exit to connect to it's smtp-server running at his 'localhost'. That's not going to work:

  • most tor-exits don't run smtp servers
  • tor won't allow exiting to the exit itself

how would you expect this to work?