Search code examples
javascriptgoogle-apps-scriptautomationgmailspreadsheet

Attach file from PC in Gmail Draft using Google App Script


Currently, I m using Google App script to create Gmail Draft from Spreadsheet, The last thing that is missing is I want to attach the file in "Gmail draft" directly from the computer instead of uploading G-Drive.

below is my current code.

here I want to add a column named "File Path" where I will enter the path of the file which I need to attach in said Email. then GS should automatically pick the file from my pc said path and attach it in Gmail Draft.

Thanks in Advance.

function Loop_Email_Drafts_with_Signature() {

const signature = Gmail.Users.Settings.SendAs.list("me").sendAs.filter(function(account){if(account.isDefault){return true}})[0].signature;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var index = 2;
var lastRow = sheet.getLastRow();

for (;index <= lastRow; index++){
var emailAddress = sheet.getRange(index, 1, 1, 1).getValue();
var ccmail = sheet.getRange(index, 2, 1, 1).getValue();
var subject = sheet.getRange(index, 3, 1, 1).getValue();
var message = sheet.getRange(index, 4, 1, 1).getValue();

  
const html = "<div> </div><br>" + signature;
const options = {
  htmlBody: message + html,
  cc: ccmail
}  
GmailApp.createDraft(emailAddress, subject, '', options);



}// End of For Loop
}// End of Function

PS: I'm new to Google App Script / Java script. so please help in detail by writing / amending my current code.


Solution

  • I got this code after many kicks :)

    Sharing for others for community Benifits.

    function DownloadAndSendWithSignature123() {
      
      
      //DECLARATIONS
      const signature = Gmail.Users.Settings.SendAs.list("me").sendAs.filter(function(account){if(account.isDefault){return true}})[0].signature;
      var sheetName = "Test email GAS";
      var sourceSpreadsheet = SpreadsheetApp.getActive();
      var sourceSheet = sourceSpreadsheet.getActiveSheet();
      var to = sourceSheet.getRange("B8").getValue();
      var ccmail = sourceSheet.getRange("B9").getValue();
      var body = sourceSheet.getRange("B13").getValue();
      var filename = sourceSheet.getRange("K5").getValue();
      var pdfName = filename;
      var subject = sourceSheet.getRange("B11").getValue();
      var link = sourceSheet.getRange("K3").getValue();
      
      
      //DOWNLOAD FILE
      var response = UrlFetchApp.fetch(link);
      var fileBlob = response.getBlob()
      //var folder = DocsList.getFolder('StackOverflow');
      var folders = DriveApp.getFolderById("Folder ID Goes here");
      
      var file =  folders.createFile(response);
       
      debugger;  // Stop to observe if in debugger
      
      
      
      
      
    
      //SAVE FILE TO DRIVE
      var listOfFiles = DriveApp.getFilesByName(filename); //returns FileIterator of files that match name given.
      if(listOfFiles.hasNext()){ //We should check if there is a file in there and die if not.
        var file = listOfFiles.next(); //gets first file in list.
            
        // SEND EMAIL THAT FILE
        
        const html = "<div> </div><br>" + signature;
        const options = {
          htmlBody: body + html,
          cc: ccmail,
          attachments: file
          
        }  
        GmailApp.createDraft(to, subject, '', options);
    
      }else{
        console.log("Error no file in listOfFiles. Email not sent.");
      }
     }