Example #1
0
 def receive(self, message):
     logging.info("received email from %s, sent to %s, actual address: %s" 
                  % (message.sender, message.to, self.request.path[10:]))
     # check for attachments
     if not hasattr(message, 'attachments'):
         logging.info("message does not have attachments " + 
                      "nothing to do here.")
         return
         
     # to_address could be masked, so get from handler.
     to_address = self.request.path[10:]
     attachment_keys = []
     for f_name, f_contents in message.attachments:
         # determine mime type
         extension = f_name.split('.')[-1].lower()
         mime_type = None
         if extension in ['jpg', 'jpeg']:
             mime_type = 'image/jpeg'
         elif extension == 'png':
             mime_type = 'image/png'
         elif extension == 'gif':
             mime_type = 'image/gif'
         elif extension == 'bmp':
             mime_type = 'image/bmp'
         
         if not mime_type:
             continue # file type not recognized
             
         file_name = files.blobstore.create(mime_type=mime_type)
         with files.open(file_name, 'a') as f:
             f.write(f_contents.decode())
             
         files.finalize(file_name)
         
         attachment_keys.append(files.blobstore.get_blob_key(file_name))
         
     unprocessed_email = UnprocessedEmail(to_address=to_address, 
                                          attachment_keys=attachment_keys)
     unprocessed_email.put()
     logging.info('stashed new unprocessed email id %s with %s attachments' 
                  % (unprocessed_email.key().id(), len(attachment_keys)))
     defer(process_email, unprocessed_email.key().id())
Example #2
0
def process_email(emailid):
    #get email
    email = UnprocessedEmail.get_by_id(emailid)
    email_key = email.to_address.split('@')[0]
    # find mapping
    mapping = AlbumEmailMapping.all().filter('email_address =', email_key) \
                               .get()
    if not mapping:
        logging.info('mapping not found: %s' % email_key)
        return

    # get credential
    credential = UserCredential.get_by_user(mapping.owner)
    if not credential:
        logging.info('No credential found for user %s, cannot upload.' %
                     mapping.owner)
        return

    # gotta grab url. check links on album data
    url = None
    for link in mapping.album['link']:
        if link['rel'] == 'http://schemas.google.com/g/2005#feed':
            url = link['href'].split('?alt')[0]

    if not url:
        logging.info('couldn\'t find feed url')
        return

    logging.info('here\s my uri!: %s' % url)

    h = httplib2.Http()
    h = credential.authorize(h)
    for key in email.attachment_keys:
        blobinfo = blobstore.BlobInfo.get(key)
        value = blobstore.BlobReader(blobinfo).read()
        response = h.request(url,
                             method="POST",
                             headers={
                                 'Content-Type': blobinfo.content_type,
                                 'Content-Length': str(blobinfo.size)
                             },
                             body=value)

        logging.info('posted some shit, see the response:')
        logging.info(response)

    UserCredential.set_credentials_for_user(mapping.owner, credential)

    # clean up
    blobinfo.delete()
    email.delete()
Example #3
0
def process_email(emailid):
    #get email   
    email = UnprocessedEmail.get_by_id(emailid)
    email_key = email.to_address.split('@')[0]
    # find mapping
    mapping = AlbumEmailMapping.all().filter('email_address =', email_key) \
                               .get()
    if not mapping:
        logging.info('mapping not found: %s' % email_key)
        return

    # get credential
    credential = UserCredential.get_by_user(mapping.owner)
    if not credential:
        logging.info('No credential found for user %s, cannot upload.' 
                     % mapping.owner)
        return

    # gotta grab url. check links on album data
    url = None
    for link in mapping.album['link']:
      if link['rel'] == 'http://schemas.google.com/g/2005#feed':
        url = link['href'].split('?alt')[0]
    
    if not url:
        logging.info('couldn\'t find feed url')
        return

    logging.info('here\s my uri!: %s' % url)

    h = httplib2.Http()
    h = credential.authorize(h)
    for key in email.attachment_keys:
        blobinfo = blobstore.BlobInfo.get(key)
        value = blobstore.BlobReader(blobinfo).read()
        response = h.request(url, method="POST", 
                       headers={
                           'Content-Type': blobinfo.content_type, 
                           'Content-Length': str(blobinfo.size)}, 
                       body=value)
                   
        logging.info('posted some shit, see the response:')
        logging.info(response)
    
    UserCredential.set_credentials_for_user(mapping.owner, credential)

    # clean up
    blobinfo.delete()
    email.delete()
Example #4
0
    def receive(self, message):
        logging.info("received email from %s, sent to %s, actual address: %s" %
                     (message.sender, message.to, self.request.path[10:]))
        # check for attachments
        if not hasattr(message, 'attachments'):
            logging.info("message does not have attachments " +
                         "nothing to do here.")
            return

        # to_address could be masked, so get from handler.
        to_address = self.request.path[10:]
        attachment_keys = []
        for f_name, f_contents in message.attachments:
            # determine mime type
            extension = f_name.split('.')[-1].lower()
            mime_type = None
            if extension in ['jpg', 'jpeg']:
                mime_type = 'image/jpeg'
            elif extension == 'png':
                mime_type = 'image/png'
            elif extension == 'gif':
                mime_type = 'image/gif'
            elif extension == 'bmp':
                mime_type = 'image/bmp'

            if not mime_type:
                continue  # file type not recognized

            file_name = files.blobstore.create(mime_type=mime_type)
            with files.open(file_name, 'a') as f:
                f.write(f_contents.decode())

            files.finalize(file_name)

            attachment_keys.append(files.blobstore.get_blob_key(file_name))

        unprocessed_email = UnprocessedEmail(to_address=to_address,
                                             attachment_keys=attachment_keys)
        unprocessed_email.put()
        logging.info(
            'stashed new unprocessed email id %s with %s attachments' %
            (unprocessed_email.key().id(), len(attachment_keys)))
        defer(process_email, unprocessed_email.key().id())