def incoming(request): """ Accept a new email message directly via the AppEngine email facility. The entire email message is contained in the POST body of *email*. :param HttpRequest request: A web request. :rtype: An HttpResponse object. """ logging.info('Incoming email received.') try: msg = InboundEmailMessage(request.raw_post_data) usetting = UserSetting.gql('WHERE email = :1', msg.sender) if usetting.count() == 0: logging.warn('Received email from an unrecognized sender: ' + msg.sender) return render_to_response('msg_receipt.email', mimetype='text/plain') if not usetting.get().is_contrib: logging.warn('Received email from an unauthorized contributor: ' + msg.sender) return render_to_response('msg_receipt.email', mimetype='text/plain') content = '' for content_type, body in msg.bodies('text/plain'): headers = True date = False for line in str(body).split('\n'): if not date: parts = line.split(' ') line = ' '.join(parts[len(parts)-5:]) date = datetime.strptime(line, '%a %b %d %H:%M:%S %Y') logging.debug(str(date)) if headers and line == '': headers = False elif not headers: content += '%s\n' % line if content == '': logging.warn('Received an email, but no text/plain bodies.') else: logging.info('Compiled plain-text email: body length=%d' % len(content)) newtitle = msg.subject.replace('\n','').replace('\r','') content = content.lstrip('\t\n\r ') email = Email(title=newtitle, body=content, date=date, views=0, rating=0) email.put() logging.info('Processing new data for tokens & tags') _process_new(email) except Exception, ex: logging.error('Error processing new email. %s' % ex)
def getFirstBatch(self): user = users.get_current_user() if user: message_array = [] query = UserSubscription.query( UserSubscription.userId == user.user_id()) for userSubscription in query.iter(): key = ndb.Key(MailMessage, userSubscription.serviceId) mailQuery = MailMessage.query( ancestor=key).order(-MailMessage.create_date) mails = mailQuery.fetch(limit=2) for mail in mails: mime_message_text = mail.mime_message mime_message = InboundEmailMessage( mime_message=mime_message_text) plaintext_bodies = mime_message.bodies('text/plain') #html_bodies = mime_message.bodies('text/html') for content_type, body in plaintext_bodies: decoded_body = body.decode() #for content_type, body in html_bodies: # decoded_body = body.decode() text = decoded_body[:160] if len(text) < len(decoded_body): text += '...' message_array.append({ 'imageUrl': 'https://via.placeholder.com/60x60', 'title': 'message from ' + str(mail.create_date), 'text': text, 'body': decoded_body }) self.response.content_type = 'application/json' self.response.write(json.encode(message_array)) else: self.error(403)
def post(self): in_msg = InboundEmailMessage(self.request.body) logging.info('You\'ve got mail: "%s" %s' % (in_msg.subject, in_msg.date)) body = in_msg.bodies().next() if body[0] != "text/html": logging.warn("HTML body not found") # Unescape because Gmail escapes HTML tags when forwarding body_decoded = HTMLParser().unescape(body[1].decode()) msg = Email( parent=Email.ANCESTOR_KEY, subject=in_msg.subject, sender=in_msg.sender, to=in_msg.to, body_html=body_decoded, read=False, ) msg.put() parse_email(msg)
def incoming(request): """ Accept a new email message directly via the AppEngine email facility. The entire email message is contained in the POST body of *email*. :param HttpRequest request: A web request. :rtype: An HttpResponse object. """ logging.info('Incoming email received.') try: msg = InboundEmailMessage(request.raw_post_data) usetting = UserSetting.gql('WHERE email = :1', msg.sender) if usetting.count() == 0: logging.warn('Received email from an unrecognized sender: ' + msg.sender) return render_to_response('msg_receipt.email', mimetype='text/plain') if not usetting.get().is_contrib: logging.warn('Received email from an unauthorized contributor: ' + msg.sender) return render_to_response('msg_receipt.email', mimetype='text/plain') content = '' for content_type, body in msg.bodies('text/plain'): headers = True date = False for line in str(body).split('\n'): if not date: parts = line.split(' ') line = ' '.join(parts[len(parts) - 5:]) date = datetime.strptime(line, '%a %b %d %H:%M:%S %Y') logging.debug(str(date)) if headers and line == '': headers = False elif not headers: content += '%s\n' % line if content == '': logging.warn('Received an email, but no text/plain bodies.') else: logging.info('Compiled plain-text email: body length=%d' % len(content)) newtitle = msg.subject.replace('\n', '').replace('\r', '') content = content.lstrip('\t\n\r ') email = Email(title=newtitle, body=content, date=date, views=0, rating=0) email.put() logging.info('Processing new data for tokens & tags') _process_new(email) except Exception, ex: logging.error('Error processing new email. %s' % ex)