-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathpostfix-filter-loop.py
executable file
·83 lines (69 loc) · 2.17 KB
/
postfix-filter-loop.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Author: Miroslav Houdek miroslav.houdek at gmail dot com
# License is, do whatever you wanna do with it (at least I think that that is what LGPL v3 says)
#
import smtpd
import asyncore
import smtplib
import traceback
class CustomSMTPServer(smtpd.SMTPServer):
def process_message(self, peer, mailfrom, rcpttos, data):
mailfrom.replace('\'', '')
mailfrom.replace('\"', '')
for recipient in rcpttos:
recipient.replace('\'', '')
recipient.replace('\"', '')
# print 'Receiving message from:', peer
# print 'Message addressed from:', mailfrom
# print 'Message addressed to :', rcpttos
# print 'MSG >>'
# print data
# print '>> EOT'
try:
# DO WHAT YOU WANNA DO WITH THE EMAIL HERE
# In future I'd like to include some more functions for users convenience,
# such as functions to change fields within the body (From, Reply-to etc),
# and/or to send error codes/mails back to Postfix.
# Error handling is not really fantastic either.
pass
except:
pass
print 'Something went south'
print traceback.format_exc()
try:
server = smtplib.SMTP('localhost', 10026)
server.sendmail(mailfrom, rcpttos, data)
server.quit()
# print 'send successful'
except smtplib.SMTPException:
print 'Exception SMTPException'
pass
except smtplib.SMTPServerDisconnected:
print 'Exception SMTPServerDisconnected'
pass
except smtplib.SMTPResponseException:
print 'Exception SMTPResponseException'
pass
except smtplib.SMTPSenderRefused:
print 'Exception SMTPSenderRefused'
pass
except smtplib.SMTPRecipientsRefused:
print 'Exception SMTPRecipientsRefused'
pass
except smtplib.SMTPDataError:
print 'Exception SMTPDataError'
pass
except smtplib.SMTPConnectError:
print 'Exception SMTPConnectError'
pass
except smtplib.SMTPHeloError:
print 'Exception SMTPHeloError'
pass
except smtplib.SMTPAuthenticationError:
print 'Exception SMTPAuthenticationError'
pass
except:
print 'Undefined exception'
print traceback.format_exc()
return
server = CustomSMTPServer(('127.0.0.1', 10025), None)
asyncore.loop()