A python script to get mail from info@example.com and re-write the 'From"' Header as "From: John Doe via info at example.com '"
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

72 lignes
1.5KB

  1. #!/usr/bin/python
  2. '''
  3. ## Description
  4. A script to get mail from info@example.com and re-write the 'From"' Header
  5. as "From: John Doe via info at example.com <noreply@example.com>'"
  6. ## Installation Instuctions
  7. * Place the script under */usr/local/sbin/rewrite_from.py*.
  8. * Edit */etc/aliases*:
  9. ```
  10. info: "|/usr/local/sbin/rewrite_from.py"
  11. ```
  12. * Run the `newaliases` command.
  13. '''
  14. import sys
  15. import email
  16. import smtplib
  17. import re
  18. DEBUG = 1
  19. MAIL_SERVER = 'localhost'
  20. TO = ["ceo@example.com", "ceoexternalmail@gmail.com"]
  21. def main():
  22. '''
  23. Main Function
  24. '''
  25. raw_msg = ''
  26. for line in sys.stdin:
  27. raw_msg = raw_msg + line
  28. msg = email.message_from_string(raw_msg)
  29. reply_to_re = re.search(
  30. r'([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-.]+\.[a-zA-Z0-9-.]+)',
  31. msg['From'])
  32. if reply_to_re:
  33. reply_to = reply_to_re.group(1)
  34. from_re = re.search(
  35. r'^ *(.+) +<?([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-.]+\.[a-zA-Z0-9-.]+)>?',
  36. msg['From'])
  37. if from_re:
  38. from_h = from_re.group(1)
  39. if msg['Reply-To']:
  40. msg['Reply-To'] = reply_to
  41. msg.replace_header('Reply-To', reply_to)
  42. else:
  43. msg.add_header('Reply-To', reply_to)
  44. msg.replace_header('From', from_h + ' via info at example.com <noreply@example.com>')
  45. send_mail = smtplib.SMTP('localhost')
  46. send_mail.sendmail(msg['From'], TO, msg.as_string())
  47. send_mail.quit()
  48. if DEBUG:
  49. filename = "/tmp/msg.out"
  50. out = open(filename, 'w')
  51. out.write(msg.as_string())
  52. if __name__ == "__main__":
  53. main()