I'm trying to copy a document I have in my drive, save it as a PDF, and then email the PDF as an attachment. However, even though the PDF is always copied and sent correctly as an attachment, and is properly readable form the gmail "preview" when viewing the email chain, it is unreadable when downloaded (filetype is simply "file"). Both the original document, when viewed from my drive, and the PDF (when the document is saved as a PDF and downloaded from the drive) are readable.
To me this indicates that there is either a problem with the way I convert the document into a PDF, or the way I attach it.
Here is how I convert it to a PDF:
var newFile = DriveApp.createFile(copyFile.getAs('application/pdf'));
And here is how I attach it:
MailApp.sendEmail(email, subject, message, {
name : "senderEmail",
attachments : [invoicePdf]
});
I know that using the Document
Google Scripts class it is possible to get a Document as a pdf using the application/pdf
keyword.
I have been sending emails with attached PDFs in this manner for a while now to several people; I find it unlikely, but possible, that none of them brought this issue to my attention until recently. The attachments from both recent emails (from people who have confirmed to me that the attachments are unreadable) and older emails are unreadable for my when I go to those conversations in my gmail and download the attached PDFs.
Kindly change your this line
var newFile = DriveApp.createFile(copyFile.getAs('application/pdf'));
as follows
var newFile = DriveApp.createFile(copyFile.getAs('application/pdf')).setName("fileName.pdf");
So that drive name it with .pdf extension, so that it can be opened properly.