def process_email (email_data): """Take the data dict returned by the smtp server for this email message and process it according to the rules defined in config.py""" eml = parse(email_data['contents']) if email_data.has_key('inbox'): inbox = email_data['inbox'] if email_data.has_key('subject'): subject = email_data['subject'] else: subject = 'Your email' body_text = "" if eml['body'] is not None and len(eml['body']) > 0: body_text = eml['body'] body_html = None if eml['html'] is not None and len(eml['html']) > 0: if len(body_text) == 0: body_text = get_text_from_html(eml['html']) body_html = eml['html'] if inbox in pass_through_mailboxes: # treat this email as a regular incoming message and pass it along to the intended inbox responders.pass_through(eml, email_data['sender'], pass_through_target, subject, body_text, body_html) elif inbox in action_mailboxes.keys(): # this email represents a command that requires a specific threaded class instantiated and invoked # use the string representation of the class name -- defined by action_mailboxes[inbox] in config.py -- to call it try: response_class = getattr(responders, action_mailboxes[inbox]) obj = response_class(eml, email_data['sender'], subject, body_text, body_html) obj.start() # kick off the request processor as a child thread so that the smtp server can close the connection immediately except AttributeError, e: log.exception(e) else: body_text = unicode(body_text.strip()) if body_text else u"" body_html = unicode(body_html.strip()) if body_html else u"" msg = Message(subject.strip(), body_text, body_html) local, domain = email_data['sender'].split('@') sender = Address(local, domain) recipients = [ ] for r in email_data['recipients']: local, domain = r.split('@') recipients.append(Address(local, domain)) try: session.add(msg) result = session.query(Address).filter(Address.local==sender.local).filter(Address.domain==sender.domain) if result.count() == 0: session.add(sender) else: sender = result[0] for idx,r in enumerate(recipients): if r.local == sender.local and r.domain == sender.domain: next result = session.query(Address).filter(Address.local==r.local).filter(Address.domain==r.domain) if result.count() == 0: session.add(r) else: r = result[0] recipients[idx] = result[0] if session.dirty or session.new: session.commit() except OperationalError, e: log.exception(e) except Exception, e: log.exception(e)
def process_email(email_data): """Take the data dict returned by the smtp server for this email message and process it according to the rules defined in config.py""" eml = parse(email_data['contents']) if email_data.has_key('inbox'): inbox = email_data['inbox'] if email_data.has_key('subject'): subject = email_data['subject'] else: subject = 'Your email' body_text = "" if eml['body'] is not None and len(eml['body']) > 0: body_text = eml['body'] body_html = None if eml['html'] is not None and len(eml['html']) > 0: if len(body_text) == 0: body_text = get_text_from_html(eml['html']) body_html = eml['html'] if inbox in pass_through_mailboxes: # treat this email as a regular incoming message and pass it along to the intended inbox responders.pass_through(eml, email_data['sender'], pass_through_target, subject, body_text, body_html) elif inbox in action_mailboxes.keys(): # this email represents a command that requires a specific threaded class instantiated and invoked # use the string representation of the class name -- defined by action_mailboxes[inbox] in config.py -- to call it try: response_class = getattr(responders, action_mailboxes[inbox]) obj = response_class(eml, email_data['sender'], subject, body_text, body_html) obj.start( ) # kick off the request processor as a child thread so that the smtp server can close the connection immediately except AttributeError, e: log.exception(e) else: body_text = unicode(body_text.strip()) if body_text else u"" body_html = unicode(body_html.strip()) if body_html else u"" msg = Message(subject.strip(), body_text, body_html) local, domain = email_data['sender'].split('@') sender = Address(local, domain) recipients = [] for r in email_data['recipients']: local, domain = r.split('@') recipients.append(Address(local, domain)) try: session.add(msg) result = session.query(Address).filter( Address.local == sender.local).filter( Address.domain == sender.domain) if result.count() == 0: session.add(sender) else: sender = result[0] for idx, r in enumerate(recipients): if r.local == sender.local and r.domain == sender.domain: next result = session.query(Address).filter( Address.local == r.local).filter( Address.domain == r.domain) if result.count() == 0: session.add(r) else: r = result[0] recipients[idx] = result[0] if session.dirty or session.new: session.commit() except OperationalError, e: log.exception(e) except Exception, e: log.exception(e)
sender = result[0] for idx,r in enumerate(recipients): if r.local == sender.local and r.domain == sender.domain: next result = session.query(Address).filter(Address.local==r.local).filter(Address.domain==r.domain) if result.count() == 0: session.add(r) else: r = result[0] recipients[idx] = result[0] if session.dirty or session.new: session.commit() except OperationalError, e: log.exception(e) except Exception, e: log.exception(e) try: session.add(Sender(msg.id, sender.id)) for r in recipients: recip = Recipient(msg.id, r.id) session.add(recip) if session.dirty or session.new: session.commit() except OperationalError, e: log.exception(e)
for idx, r in enumerate(recipients): if r.local == sender.local and r.domain == sender.domain: next result = session.query(Address).filter( Address.local == r.local).filter( Address.domain == r.domain) if result.count() == 0: session.add(r) else: r = result[0] recipients[idx] = result[0] if session.dirty or session.new: session.commit() except OperationalError, e: log.exception(e) except Exception, e: log.exception(e) try: session.add(Sender(msg.id, sender.id)) for r in recipients: recip = Recipient(msg.id, r.id) session.add(recip) if session.dirty or session.new: session.commit() except OperationalError, e: log.exception(e)