from __future__ import absolute_import, unicode_literals from celery import task, shared_task import sys import datetime import fileinput import smtplib #from mailbox import Maildir from email.feedparser import FeedParser from email.message import EmailMessage #from demoapp.models import Widget #from celery import Celery #app = Celery('procat2', broker='amqp://localhost') #@app.task @shared_task def add(x, y): return x + y body_ok = """Hi, Attached is your marked up catalog. In the near future you'll also get a spreadsheet and a new catalog with just the marked up items. Enjoy, ProCatalog Markup Bot""" body_missing = """Hi, I couldn't find a pdf attached to your message. I can't do much without a marked up catalog pdf, so please include that when you try again. Thanks, ProCatalog Markup Bot""" # def log_message(msg): # with open('/var/log/markup_submit.log', 'a') as f: # f.write("{} - {} / {}\n".format(datetime.datetime.now().isoformat(), # msg['From'], msg['Subject'])) def reply(frm, subj, attach): print('ok: {} - {}'.format(frm, subj)) # msg = EmailMessage() # msg.set_content(body_ok) # msg.add_attachment(attach.get_payload(decode=True), # maintype=attach.get_content_maintype(), # subtype=attach.get_content_subtype(), # filename=attach.get_filename()) # send(frm, subj, msg) def reply_missing(frm, subj): print('missing: {} - {}'.format(frm, subj)) # msg = EmailMessage() # msg.set_content(body_missing) # send(frm, subj, msg) def send(frm, subj, msg): msg['From'] = 'Keen ProCatalog Markup Bot ' msg['To'] = frm msg['Bcc'] = 'alx-markup@procatalog.io' msg['Subject'] = 'Re: {}'.format(subj) maildir = Maildir('/tmp/markup_submit_mail') maildir.add(msg) with smtplib.SMTP('localhost') as s: s.starttls() s.login('remotevm', '5gW311IOs') s.send_message(msg) @shared_task def process_message(path): parser = FeedParser() with open(path) as f: for line in f: parser.feed(line) msg = parser.close() #print('read message: {}'.format(msg)) #store_message(msg) #log_message(msg) if msg.is_multipart(): for attach in msg.walk(): if attach.get_content_type() == 'application/pdf': reply(msg['From'], msg['Subject'], attach) break else: reply_missing(msg['From'], msg['Subject'])