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 receive(IMAP_SERVER, IMAP_PORT, username, password): msgs = [] M = imaplib.IMAP4_SSL(IMAP_SERVER, IMAP_PORT) #M.debug = 4 M.login(username, password) M.select() typ, data = M.search(None, 'UNSEEN') #typ, data = M.search(None, 'ALL') for mid in data[0].split(): #print mid try: status, response = M.fetch(mid,"(RFC822)") M.store(mid, '+FLAGS', '\\SEEN') mailText = response[0][1] #print mailText mail_message = email.message_from_string(mailText) msg = email_parser.parse(mail_message) #print msg["subject"] #print msg["from"] #print msg["to"] #print msg["body"] #print msg["html"] msgs.append(msg) except Exception,x: traceback.print_exc()
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)
flag = True except Exception, x: traceback.print_exc() if one: break try: #M.close() #QQ error! M.logout() except Exception, x: traceback.print_exc() if flag: break for mail_message in raw_msgs: msg = email_parser.parse(mail_message) #print msg["subject"] # print msg["from"] # print msg["to"] # print msg["body"] # print msg["html"] msgs.append(msg) return msgs if __name__ == '__main__': msgs = receive("imap.exmail.qq.com", 993, "*****@*****.**", "gbkr0419",
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)