0
votes

Okay, so I'm working on a script that will basically pull information from a spreadsheet to then send out in an email once an email address is added into the spreadsheet. The issue is I'm trying to add a noReply and cc setting but it seems to then break my script to where it keeps sending emails or doesn't send any at all.

Theory: I think 'cc' is throwing the issue as the script is reading it as a recipient and just sending the email out instead of using the recipient that's added into the spreadsheet. This is the reference I'm following: https://developers.google.com/apps-script/reference/mail/mail-app#sendEmail(String,String,String,Object)

This is my code with the issue section being commented as such:

// This constant is written in column M for rows for which an email
// has been sent successfully.
var EMAIL_SENT = 'EMAIL_SENT';


/**
 * Sends non-duplicate emails with data from the current spreadsheet.
 */
function sendEmails2() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2; // First row of data to process
  var numRows = 3; // Number of rows to process
  // Fetch the range of cells
  var dataRange = sheet.getRange(startRow, 1, numRows, 20);
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var emailAddress = row[8]; // Column I
    var message = ('For the ticket below, please ensure that you call the user as the first resort before following up via email.' + '\n\n TimeStamp: ' + row[1] + '\n Agent Name: ' + row[2] + '\n Your site: ' + row[3] + '\n Zendesk Link: ' + row[4] + '\n Summary of User Issue: ' + row[5] + '\n De-Escalation Attempted:' + row[6] + '\n User Contact Number ' + row[7]); // Fetch of columns B-H
    var emailSent = row[12]; // Column M
    if (emailSent !== EMAIL_SENT) { // Prevents sending duplicates and everything after && emailAddress is just testing a fix but didn't work
      var subject = 'You have been assigned a Supervisor Call. Please Action Within 24 hours!';
      MailApp.sendEmail(emailAddress, subject, message, { // Section below gives the issue
        noReply: true,
        cc: 'cc@email.com'
      });
      sheet.getRange(startRow + i, 13).setValue(EMAIL_SENT);
      // Make sure the cell is updated right away in case the script is interrupted
      SpreadsheetApp.flush();
    }
  }
}

Screenshot of spreadsheet layout: enter image description here

This is my first time writing out a question on Stack but feel free to ask and I can detail more of what you need in order to help out.

1
I dont see the advanced parameter for priority in the documentation I believe it's expecting a comma-separated list of email addresses Cooper
Hey @Cooper ignore the priority piece for now. I forgot I even had it in there, so my fault. The main thing is how can I get noReply and CC to work? As mentioned, the second I add in that section the if statement for duplicates stops working and the behavior just gets all wonky.Codekraker95
noReply is a boolean not a stringCooper
Correct @Cooper hence mine being set as a boolean. What about CC? I know it's a comma separated list, but passing it breaks my script. Do you have a better way for me to pass cc within my if statement?Codekraker95
In your question noReply is a stringCooper

1 Answers

0
votes

I was able to get this working with Cooper's help. I had to do some small formatting changes to get it working but ultimately the code wasn't incorrect, just format issues that caused it to not be read appropriately.

Appreciate the help!