craftApprentice
craftApprentice

Reputation: 2777

Sending a file as e-mail attachment with Google Apps Script

Is there a way to use file upload class and mailApp to select a (pdf) file from your computer (like this tutorial shows) and stores it only temporarily (maybe using cache memory) to send it as an email attachment?

Upvotes: 0

Views: 6036

Answers (2)

sajid ali
sajid ali

Reputation: 37

var spreadsheetFile = DriveApp.getFileById(sheetId); 
     var blob = spreadsheetFile.getAs('application/pdf');
     //if daysleft =1 or 0 then send mail .      
     MailApp.sendEmail(emailAddress, subject, message, {
       name: 'Welcome mail- Perch Arbor Golf Course.pdf',
       attachments: [blob]
     });

Upvotes: 1

Serge insas
Serge insas

Reputation: 46822

the sample you refer to needs very few modifications to do what you want... the variable you get in the doPost is a blob, the argument needed in the attachment is a blob too so it is quite straightforward to put both together.

I only added a text on the button and a confirmation message when the mail is sent.

note that the file type will depend only on the filetype of the file, nowhere we convert it to pdf but that was not the point of your question.

if you upload a jpeg (for example) it will be a jpeg in the attachment of course !

code below with test on line:

function doGet(e) {
   var app = UiApp.createApplication().setTitle("Upload CSV to Sheet");
   var formContent = app.createVerticalPanel();
   formContent.add(app.createFileUpload().setName('thefile'));
   formContent.add(app.createSubmitButton('send'));
   var form = app.createFormPanel();
   form.add(formContent);
   app.add(form);
   return app;
 }

 function doPost(e) {
   // data returned is a blob for FileUpload widget
   var fileBlob = e.parameter.thefile;
   MailApp.sendEmail(Session.getActiveUser(),'test pdf attachment','see attachment',{attachments: [fileBlob]});
   var app = UiApp.getActiveApplication();
   return app.add(app.createLabel('mail sent'));
 }

test (asking for authorization)

Upvotes: 2

Related Questions