python - Attach file from Google Drive -


i have been trying create templated google drive document jinja2 , email document attached pdf document.

so far have managed of stuck on attachment part. error "invalidattachmenttypeerror: invalid attachment type".

and there way improve on bit more efficient.

class upload(webapp2.requesthandler):   @decorator.oauth_required   def get(self):     if decorator.has_credentials():       try:           body = {'title': 'my new text document',                   'description': 'hello world'}            template = jinja_environment.get_template('document.html')           template_values = {'name': 'simon'}           fh = stringio.stringio(template.render(template_values))            media_body = mediaiobaseupload(fh,                                          mimetype='text/html',                                          resumable=false)            http = httplib2.http(memcache)           http = decorator.http()            service = discovery.build('drive', 'v2', http=http)            file = service.files().insert(body=body,                                         media_body=media_body,                                         convert=true).execute(http=http)            m = mail.emailmessage()           m.sender = 'myfromemailaaddress@gmail.com'           m.to = 'mytoemailaddress@gmail.com'           m.subject = 'my subject'           m.html = '<p>my body.</p>'           m.attachments = [(file['title'],                             file['exportlinks']['application/pdf'])]           m.send()            self.redirect('/')          except client.accesstokenrefresherror:           self.redirect('/')      else:       self.redirect(decorator.authorize_url()) 

well in m.attachments property should have did tuple each attachment, first element being document title, , second 1 document data itself.

in case, document data string, it's not file, here issue. should retrieve pdf file drive first, put attachment.

you can check here how download file content using downloadurl property (or in case exportlinks.

also, please make sure set proper extension file. files without extension or specific extensions blacklisted , cannot sent attachments. check this

hope helps.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -