A python script to get mail from staff-questions@example.com and resend it anonymously to ACME Management
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

84 wiersze
1.7KB

  1. #!/usr/bin/python
  2. '''
  3. ## Description
  4. A script to get mail from staff-questions@example.com and resend it anonymously to
  5. ACME Management
  6. ## Installation Instructions
  7. * Upload the script in */usr/local/sbin/anon_mail.py*.
  8. * Add the following line in */etc/aliases*:
  9. ```
  10. staff-question: "|/usr/local/sbin/anon_mail.py"
  11. ```
  12. * Run the `newaliases` command.
  13. '''
  14. import sys
  15. import email
  16. import smtplib
  17. DEBUG = 0
  18. MAIL_SERVER = 'localhost'
  19. if DEBUG:
  20. TO = ["dev@example.com"]
  21. else:
  22. TO = ["board@example.com", "ceo@example.com", "cto@example.com"]
  23. def main():
  24. '''
  25. Main Function
  26. '''
  27. raw_msg = ''
  28. for line in sys.stdin:
  29. raw_msg = raw_msg + line
  30. msg = email.message_from_string(raw_msg)
  31. if DEBUG:
  32. filename = "/tmp/msg-before.out"
  33. out = open(filename, 'w')
  34. out.write(msg.as_string())
  35. if msg['Reply-To']:
  36. msg.__delitem__('Reply-To')
  37. if msg['Received']:
  38. msg.__delitem__('Received')
  39. if msg['Return-Path']:
  40. msg.replace_header('Return-Path', None)
  41. if msg['X-Virus-Scanned']:
  42. msg.__delitem__('X-Virus-Scanned')
  43. if msg['DKIM-Signature']:
  44. msg.__delitem__('DKIM-Signature')
  45. if msg['Message-ID']:
  46. msg.__delitem__('Message-ID')
  47. if msg['User-Agent']:
  48. msg.__delitem__('User-Agent')
  49. if msg['From']:
  50. msg.replace_header('From', 'Anonymous Staff <noreply@puri.sm>')
  51. send_mail = smtplib.SMTP('localhost')
  52. send_mail.sendmail(msg['From'], TO, msg.as_string())
  53. send_mail.quit()
  54. if DEBUG:
  55. filename = "/tmp/msg-after.out"
  56. out = open(filename, 'w')
  57. out.write(msg.as_string())
  58. if __name__ == "__main__":
  59. main()