title: How do I upload an email attachment sent as application/octet-stream to google drive
body: <p>I receive nightly emails which contain excel files as attachments, yet the sending company do not define the content type so that the files arrive with content type=application/octet-stream</p>
  
  <p>My script is attempting to upload the files to my google drive as Google Sheets, however the upload fails as the script does not know what type of file it is.</p>
  
  <p>I have tried forwarding the attachment and giving it the correct content type but the forwarded file also arrives as octet-stream.</p>
  
  <p>Any ideas what I can do to get past this?</p>
  
  <p>The gist of the code I am using is as follows:</p>
  
  <pre><code>var ureadMsgsCount = GmailApp.getInboxUnreadCount();
  if (ureadMsgsCount &gt; 0) {
  
  var threads = GmailApp.search("Excel Files")
  for (var i = 0; i &lt; threads.length; i++) {
    if (threads[i].isInInbox()) {
  
      messages = threads[i].getMessages();
      for (var j = 0; j &lt; messages.length; j++) {
  
        if (messages[j].isUnread()) {
  
          var attachments = messages[j].getAttachments();
          for (var k = 0; k &lt; attachments.length; k++) {
  
            // Is the attachment an excel file
            var excelFile = attachments[k];
            var fileName = attachments[k].getName();
            var contentType = attachments[k].getContentType();
  
            if (fileName.indexOf("xls") &gt; -1) {
              var folderId = getFolderID('saved files');
              var resource = {
                title: fileName.replace(/.xls?/, ""),
                mimeType: 'application/vnd.google-apps.spreadsheet',
                parents: [{ id: folderId }]
              };
              Drive.Files.insert(resource, excelFile);
            }
           }
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>many thanks.</p>
  
  <p>The error which is thrown is "GoogleJsonResponseException: Invalid mime type provided"</p>