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 = '(no subject)' 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, '['+inbox+'@] '+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 module.class name -- defined by action_mailboxes[inbox] in config.py -- to call it _invoke_action(action_mailboxes[inbox], eml, email_data['sender'], subject, body_text, body_html) elif default_mailbox is not None: # use the default response action, if it is defined _invoke_action(default_mailbox, eml, email_data['sender'], subject, body_text, body_html)
def domain_recipients_valid (domain_recipients=[]): """Confirm that the first email recipient @smtp_server_domain could correspond to a valid project (i.e., it a new project or an int) and return it""" result = None try: if domain_recipients[0] in action_mailboxes.keys() or domain_recipients[0] in pass_through_mailboxes: result = domain_recipients[0] except IndexError: pass return result
def domain_recipients_valid(domain_recipients=[]): """Confirm that the first email recipient @smtp_server_domain could correspond to a valid project (i.e., it a new project or an int) and return it""" result = None try: if domain_recipients[0] in action_mailboxes.keys( ) or domain_recipients[0] in pass_through_mailboxes: result = domain_recipients[0] except IndexError: pass return result
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 = '(no subject)' 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, '[' + inbox + '@] ' + 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: print 'Exception:', time.strftime( "%a, %d %b %Y %H:%M:%S +0000", time.gmtime()), 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 = '(no subject)' 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, '['+inbox+'@] '+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: print 'Exception:', time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()), 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)
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)