| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
''' |
|---|
| 5 |
ml-funnel.py |
|---|
| 6 |
subscribe an email based service with single email address, and deliver |
|---|
| 7 |
messages from the service to multiple users. |
|---|
| 8 |
|
|---|
| 9 |
Copyright (c) 2007 Shigeru KANEMOTO |
|---|
| 10 |
All rights reserved. |
|---|
| 11 |
''' |
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
LIST_TARGET = 'target@some-domain' |
|---|
| 17 |
LIST_ADDRESS = 'user@example.com' |
|---|
| 18 |
LIST_SUBSCRIBERS = ( |
|---|
| 19 |
|
|---|
| 20 |
'somebody@example.com', |
|---|
| 21 |
) |
|---|
| 22 |
|
|---|
| 23 |
import sys |
|---|
| 24 |
import re |
|---|
| 25 |
import email.FeedParser |
|---|
| 26 |
import email.Header |
|---|
| 27 |
import email.Utils |
|---|
| 28 |
import smtplib |
|---|
| 29 |
|
|---|
| 30 |
BUFFER_SIZE = 1024 |
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
class MLError(RuntimeError): |
|---|
| 34 |
pass |
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
def message_from_fileobj(fileobj): |
|---|
| 38 |
parser = email.FeedParser.FeedParser() |
|---|
| 39 |
while True: |
|---|
| 40 |
buffer = fileobj.read(BUFFER_SIZE) |
|---|
| 41 |
if not buffer: |
|---|
| 42 |
return parser.close() |
|---|
| 43 |
parser.feed(buffer) |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
def forward_message(message): |
|---|
| 47 |
|
|---|
| 48 |
recipients = message.get_all('to', []) |
|---|
| 49 |
recipients += message.get_all('cc', []) |
|---|
| 50 |
recipients += message.get_all('resent-to', []) |
|---|
| 51 |
recipients += message.get_all('resent-cc', []) |
|---|
| 52 |
recipients = email.Utils.getaddresses(recipients) |
|---|
| 53 |
for (realname, address) in recipients: |
|---|
| 54 |
if address.lower() == LIST_ADDRESS: |
|---|
| 55 |
break |
|---|
| 56 |
else: |
|---|
| 57 |
raise MLError, 'Not sent to this list.' |
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
sender = message['from'] |
|---|
| 61 |
if not sender: |
|---|
| 62 |
raise MLError, 'No "from" header found.' |
|---|
| 63 |
(realname, sender) = email.Utils.parseaddr(sender) |
|---|
| 64 |
if not sender: |
|---|
| 65 |
raise MLError, '"from" header not recognized.' |
|---|
| 66 |
sender = sender.lower() |
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
del message['to'] |
|---|
| 70 |
del message['cc'] |
|---|
| 71 |
del message['resent-to'] |
|---|
| 72 |
del message['resent-cc'] |
|---|
| 73 |
del message['from'] |
|---|
| 74 |
del message['reply-to'] |
|---|
| 75 |
|
|---|
| 76 |
if sender == LIST_TARGET: |
|---|
| 77 |
message['To'] = LIST_ADDRESS |
|---|
| 78 |
message['From'] = LIST_ADDRESS |
|---|
| 79 |
smtp = smtplib.SMTP('localhost') |
|---|
| 80 |
smtp.sendmail(LIST_ADDRESS, LIST_SUBSCRIBERS, message.as_string()) |
|---|
| 81 |
smtp.close() |
|---|
| 82 |
elif sender in LIST_SUBSCRIBERS: |
|---|
| 83 |
message['To'] = LIST_TARGET |
|---|
| 84 |
message['From'] = LIST_ADDRESS |
|---|
| 85 |
smtp = smtplib.SMTP('localhost') |
|---|
| 86 |
smtp.sendmail(LIST_ADDRESS, LIST_TARGET, message.as_string()) |
|---|
| 87 |
smtp.close() |
|---|
| 88 |
else: |
|---|
| 89 |
raise MLError, 'Sender "%s" not allowed.' % sender |
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
if __name__ == '__main__': |
|---|
| 93 |
try: |
|---|
| 94 |
forward_message(message_from_fileobj(sys.stdin)) |
|---|
| 95 |
except MLError, e: |
|---|
| 96 |
print >>sys.stderr, str(e) |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
sys.exit(0) |
|---|