anon-mail/anon_mail.py

84 wiersze
1.7 KiB
Python

2018-06-07 15:05:56 +03:00
#!/usr/bin/python
'''
## Description
A script to get mail from staff-questions@example.com and resend it anonymously to
ACME Management
## Installation Instructions
* Upload the script in */usr/local/sbin/anon_mail.py*.
* Add the following line in */etc/aliases*:
```
staff-question: "|/usr/local/sbin/anon_mail.py"
```
* Run the `newaliases` command.
'''
import sys
import email
import smtplib
DEBUG = 0
MAIL_SERVER = 'localhost'
if DEBUG:
TO = ["dev@example.com"]
else:
TO = ["board@example.com", "ceo@example.com", "cto@example.com"]
def main():
'''
Main Function
'''
raw_msg = ''
for line in sys.stdin:
raw_msg = raw_msg + line
msg = email.message_from_string(raw_msg)
if DEBUG:
filename = "/tmp/msg-before.out"
out = open(filename, 'w')
out.write(msg.as_string())
if msg['Reply-To']:
msg.__delitem__('Reply-To')
if msg['Received']:
msg.__delitem__('Received')
if msg['Return-Path']:
msg.replace_header('Return-Path', None)
if msg['X-Virus-Scanned']:
msg.__delitem__('X-Virus-Scanned')
if msg['DKIM-Signature']:
msg.__delitem__('DKIM-Signature')
if msg['Message-ID']:
msg.__delitem__('Message-ID')
if msg['User-Agent']:
msg.__delitem__('User-Agent')
if msg['From']:
msg.replace_header('From', 'Anonymous Staff <noreply@puri.sm>')
send_mail = smtplib.SMTP('localhost')
send_mail.sendmail(msg['From'], TO, msg.as_string())
send_mail.quit()
if DEBUG:
filename = "/tmp/msg-after.out"
out = open(filename, 'w')
out.write(msg.as_string())
if __name__ == "__main__":
main()