This commit is contained in:
itismadness
2018-04-10 13:26:43 -07:00
parent 032536ccc9
commit 0e95cd9656
4 changed files with 71 additions and 7 deletions

52
postman.py Normal file
View File

@@ -0,0 +1,52 @@
#!/usr/bin/env python3
import argparse
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import formatdate
from email import encoders
import os
import smtplib
import requests
def send_mail(to, fro, subject, text, files=None, server='localhost'):
if files is None:
files = []
assert type(to) == list
assert type(files) == list
msg = MIMEMultipart()
msg['From'] = fro
msg['To'] = ', '.join(to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach(MIMEText(text))
for file in files:
part = MIMEBase('application', "octet-stream")
part.set_payload(open(file, "rb").read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"'
% os.path.basename(file))
msg.attach(part)
smtp = smtplib.SMTP(server)
smtp.sendmail(fro, to, msg.as_string())
smtp.close()
def main():
parser = argparse.ArgumentParser()
parser.add_argument('host', type=str)
args = parser.parse_args()
messages = requests.get('http://{}/'.format(args.host)).json()
for message in messages:
send_mail(message['rcpt_tos'], message['mail_from'], message['subject'], message['body'])
if __name__ == '__main__':
main()

View File

@@ -1,3 +1,4 @@
falcon
aiosmtpd
gunicorn
gunicorn
mail-parser

View File

@@ -16,7 +16,10 @@ class Resource(object):
if entry.name == '.gitkeep':
continue
with open(entry.path) as open_file:
response.append(json.load(open_file))
try:
response.append(json.load(open_file))
except json.decoder.JSONDecodeError:
pass
os.remove(entry.path)
resp.body = str.encode(json.dumps(response))
resp.status = falcon.HTTP_200

View File

@@ -1,26 +1,30 @@
#!/usr/bin/env python3
import argparse
from datetime import datetime
import json
import os
import uuid
from aiosmtpd.controller import Controller
import mailparser
HOST = '127.0.0.1'
PORT = 10025
DATA_PATH = 'messages'
class Handler(object):
# noinspection PyPep8Naming,PyMethodMayBeStatic
async def handle_DATA(self, _, session, envelope):
print('Recieved message...')
# noinspection PyBroadException
try:
with open(os.path.join('messages', str(uuid.uuid4().hex)), 'w') as open_file:
message = mailparser.parse_from_string(envelope.content.decode('utf-8'))
json.dump({'mail_from': envelope.mail_from,
'rcpt_tos': envelope.rcpt_tos,
'message': envelope.content.decode('utf-8'),
'subject': message.subject,
'body': message.body,
'data': envelope.content.decode('utf-8'),
'timestamp': datetime.utcnow().isoformat()},
open_file)
return '250 OK'
@@ -29,11 +33,15 @@ class Handler(object):
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('host', type=str)
parser.add_argument('port', type=int)
args = parser.parse_args()
handler = Handler()
controller = Controller(handler, hostname='127.0.0.1', port=10025)
controller = Controller(handler, hostname=args.host, port=args.port)
# noinspection PyBroadException
try:
controller.start()
input('Running smtpd server on {}:{}'.format(HOST, PORT))
input("Running smtpd server on {}:{}\n".format(args.host, args.port))
except Exception:
controller.stop()