72 行
1.5 KiB
Python
実行ファイル
72 行
1.5 KiB
Python
実行ファイル
#!/usr/bin/python
|
|
|
|
'''
|
|
## Description
|
|
|
|
A script to get mail from info@example.com and re-write the 'From"' Header
|
|
as "From: John Doe via info at example.com <noreply@example.com>'"
|
|
|
|
## Installation Instuctions
|
|
|
|
* Place the script under */usr/local/sbin/rewrite_from.py*.
|
|
* Edit */etc/aliases*:
|
|
```
|
|
info: "|/usr/local/sbin/rewrite_from.py"
|
|
```
|
|
* Run the `newaliases` command.
|
|
'''
|
|
|
|
import sys
|
|
import email
|
|
import smtplib
|
|
import re
|
|
|
|
DEBUG = 1
|
|
MAIL_SERVER = 'localhost'
|
|
TO = ["ceo@example.com", "ceoexternalmail@gmail.com"]
|
|
|
|
def main():
|
|
'''
|
|
Main Function
|
|
'''
|
|
|
|
raw_msg = ''
|
|
|
|
for line in sys.stdin:
|
|
raw_msg = raw_msg + line
|
|
|
|
msg = email.message_from_string(raw_msg)
|
|
reply_to_re = re.search(
|
|
r'([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-.]+\.[a-zA-Z0-9-.]+)',
|
|
msg['From'])
|
|
|
|
if reply_to_re:
|
|
reply_to = reply_to_re.group(1)
|
|
|
|
from_re = re.search(
|
|
r'^ *(.+) +<?([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-.]+\.[a-zA-Z0-9-.]+)>?',
|
|
msg['From'])
|
|
|
|
if from_re:
|
|
from_h = from_re.group(1)
|
|
|
|
if msg['Reply-To']:
|
|
msg['Reply-To'] = reply_to
|
|
msg.replace_header('Reply-To', reply_to)
|
|
else:
|
|
msg.add_header('Reply-To', reply_to)
|
|
|
|
msg.replace_header('From', from_h + ' via info at example.com <noreply@example.com>')
|
|
|
|
send_mail = smtplib.SMTP('localhost')
|
|
send_mail.sendmail(msg['From'], TO, msg.as_string())
|
|
send_mail.quit()
|
|
|
|
if DEBUG:
|
|
filename = "/tmp/msg.out"
|
|
out = open(filename, 'w')
|
|
out.write(msg.as_string())
|
|
|
|
if __name__ == "__main__":
|
|
main()
|