Skip to content

Instantly share code, notes, and snippets.

@f2hex
Created August 23, 2019 13:38
Show Gist options
  • Save f2hex/f6cbdb26f966d109c8c0e79218818a49 to your computer and use it in GitHub Desktop.
Save f2hex/f6cbdb26f966d109c8c0e79218818a49 to your computer and use it in GitHub Desktop.
"""
Very simple SMTP server to process image stills received from ip-cams and GDS3710.
Franco Fiorese <franco.fiorese@gmail.com>
May 2017
"""
from datetime import datetime
import asyncore, sys, email, os
from smtpd import SMTPServer
class Attachment(object):
def __init__(self):
self.data = None;
self.content_type = None;
self.size = None;
self.name = None;
class EmailServer(SMTPServer):
def parse_attachment(self, message_part):
content_disposition = message_part.get("Content-Disposition", None);
if content_disposition:
dispositions = content_disposition.strip().split(";");
if bool(content_disposition and dispositions[0].lower() == "attachment"):
attachment = Attachment();
attachment.data = message_part.get_payload(decode=True);
attachment.content_type = message_part.get_content_type();
if attachment.content_type == "image/jpeg":
attachment.size = len(attachment.data);
attachment.name = message_part.get_filename();
return attachment;
return None;
def process_message(self, peer, mailfrom, rcpttos, data):
msg = email.message_from_string(data);
print(msg["Subject"])
attachments = list();
print("email from: %s" % mailfrom)
user, domain = mailfrom.split('@')
print("user=%s domain=%s" % (user, domain))
if (msg.is_multipart()):
for part in msg.walk():
att = self.parse_attachment(part);
if (att):
attachments.append(att);
nx = 1
for att in attachments:
fdir = '/events/%s/%s' % (user, datetime.now().strftime("%Y-%m-%d"))
filename = '%s_%d.jpg' % (datetime.now().strftime("%H-%M-%S"), nx)
fspec = fdir + "/" + filename
if not os.path.exists(fdir):
os.makedirs(fdir)
file = open(fspec, 'w');
file.write(att.data);
file.close();
nx += 1
def run():
es = EmailServer(('0.0.0.0', 1025), None)
try:
asyncore.loop()
except KeyboardInterrupt:
pass
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment