0
votes

I am having a lot of trouble with actually getting the file to attach to the email I'm sending, I have been using the code below and the email is sending but the file is not being included.

function myCoding() {
  var array = SpreadsheetApp.getActiveSheet().getDataRange().getValues();      
  for(var i=1;i<array.length;i++){
    var loopEmail = array[i][0];
    var loopName = array[i][1];
    var loopNN = array[i][2];
    var loopSize = array[i][3];
    var loopColour = array[i][4];      
    text = "Hey " + loopName;
    text1 = "\n Your full name for the hoodie is: "+ loopName + ", your nickname for your hoodie is " + loopNN + " and your size is " +loopSize + ". You have chosen the colour "+loopColour + ".";
    text3 = "\n"
    var file = DriveApp.getFilesByName('DrinksShopPartnerProgram.pdf');
    if (file.hasNext()) {
    GmailApp.sendEmail(loopEmail, "DGS", text + text3 + text1 + text3, file);
    attachments: [file.next().getAs(MimeType.PDF, )]

    } 
}
}
1
Do you have several files with that name? if not, use only the Id so you won't need the File iterator. Also, attachments is after the ;, so it's outside the GmailApp method.Jescanellas
No its just the one file.Manraj Sandhu

1 Answers

1
votes

You can use the example from the documentation:

var file = DriveApp.getFileById('1234567890abcdefghijklmnopqrstuvwxyz');
GmailApp.sendEmail('mike@example.com', 'Attachment example', 'Please see the attached file.', {
    attachments: [file.getAs(MimeType.PDF)],
    name: 'Automatic Emailer Script'
});

So you can modify your code to:

function myCoding() {
  var array = SpreadsheetApp.getActiveSheet().getDataRange().getValues();      
  for(var i=1;i<array.length;i++){
    var loopEmail = array[i][0];
    var loopName = array[i][1];
    var loopNN = array[i][2];
    var loopSize = array[i][3];
    var loopColour = array[i][4];      
    text = "Hey " + loopName;
    text1 = "\n Your full name for the hoodie is: "+ loopName + ", your nickname for your hoodie is " + loopNN + " and your size is " +loopSize + ". You have chosen the colour "+loopColour + ".";
    text3 = "\n"
    var file = DriveApp.getFileById('ID of DrinksShop...');

    GmailApp.sendEmail(loopEmail, "DGS", text + text3 + text1 + text3, {
    attachments: [file.getAs(MimeType.PDF)]});


    }
}

As I said there is no need to use the File iterator. As it is only one file and you can get the id with a List.