Parent: [a31727] (diff)

Child: [04356a] (diff)

Download this file

open_relay.py    67 lines (57 with data), 2.3 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
import smtpd
import smtplib
import asyncore
from getpass import getpass
from optparse import OptionParser
def main():
parser = OptionParser()
parser.add_option('--host', dest='host')
parser.add_option('-p', '--port', dest='port', default=25)
parser.add_option('-s', '--ssl', dest='ssl', action='store_true', default=False)
parser.add_option('-t', '--tls', dest='tls', action='store_true', default=False)
parser.add_option('-l', '--login', dest='login', action='store_true', default=False)
(options, args) = parser.parse_args()
if options.login:
username = raw_input('Username:')
password = getpass('Password:')
else:
username = password = None
smtp_client = MailClient(options.host,
int(options.port),
options.ssl,
options.tls,
username, password)
server = MailServer(('0.0.0.0', 8826), None,
smtp_client=smtp_client)
asyncore.loop()
class MailClient(object):
def __init__(self, host, port, ssl, tls, username, password):
self.host, self.port, self.ssl, self.tls, self.username, self.password = \
host, port, ssl, tls, username, password
self._client = None
self._connect()
def sendmail(self, mailfrom, rcpttos, data):
print 'Sending mail to %s' % rcpttos
try:
self._client.sendmail(mailfrom, rcpttos, data)
except:
self._connect()
self._client.sendmail(mailfrom, rcpttos, data)
def _connect(self):
if self.ssl:
self._client = smtplib.SMTP_SSL(self.host, int(self.port))
else:
self._client = smtplib.SMTP(self.host, int(self.port))
if self.tls:
self._client.starttls()
if self.username:
self._client.login(self.username, self.password)
class MailServer(smtpd.SMTPServer):
def __init__(self, *args, **kwargs):
self._client = kwargs.pop('smtp_client')
smtpd.SMTPServer.__init__(self, *args, **kwargs)
def process_message(self, peer, mailfrom, rcpttos, data):
self._client.sendmail(mailfrom, rcpttos, data)
if __name__ == '__main__':
main()