MailApp.sendEmail not working: sender can see the email on the sent folder but recipient doesn't see it on his Inbox folder

42 views
Skip to first unread message

Benjamin Jackson

unread,
Apr 1, 2023, 3:10:11 AM4/1/23
to Google Apps Script Community
Hi there! I've created a script that sends an email every time changes are made on the spreadsheet to a list of defined master users. 

I can see the emails on the sender's "Sent" folder but the recipients are not receiving it on their "Inbox" folder. 

This is the function's code that notifies the master users when changes are made:

function notifyMasterUsers(e) {
let range = e.range;
let source = e.source;
let spreadsheetName = source.getName();
let a1Notation = range.getA1Notation();
let oldValue = e.oldValue;
let newValue = e.value;
let user = e.user;
if (user != '') {
user = user;
}
else {
user = 'Unkown User';
}
let subject = user + ' edited the cell ' + a1Notation + ' on the spreadsheet ' + spreadsheetName;
let body = 'Old Value: ' + oldValue + '\n' + 'New Value: ' + newValue;
let masterUserEmails = getMasterUserEmails();
if (masterUserEmails.length != 0) {
for (let i = 0; i < masterUserEmails.length; i++) {
let masterUserEmail = masterUserEmails[i];
try {
MailApp.sendEmail(masterUserEmail, subject, body);
}
catch(error) {
ui.alert(error);
}
}
}
else {
ui.alert('No master user emails listed to nofity changes. Please add master user emails first.');
}
}

This is the function that gets the master user emails list from one of the sheets:
/**
* Gets email array from Master Users sheet
*/
function getMasterUserEmails(sheetName = masterUsersSheetName) {
let sheet = spreadsheet.getSheetByName(sheetName);
if (sheet != null) {
let dataRange = sheet.getDataRange();
let values = dataRange.getValues();
let masterUserEmailArray = [];
for (let i = 0; i < values.length; i++) {
masterUserEmailArray.push(values[i][0]);
};
masterUserEmailArray.shift();
// console.log(masterUserEmailArray.length);
return masterUserEmailArray;
}
else {
ui.alert('Couldnt get the "Master Users" sheet. Please run the function "Add Master Users Sheet" located on the "Automated Functions" custom menu and try again.');
}
}

This is the trigger: 

Untitled.png

This is the sender's "Sent" folder: 

Untitled.png

This is how the email looks: 

Untitled 2.png

This is the recipient "Inbox" folder (you can see there is no email with the previous subject): 

Untitled 3.png

As you can see, the email that notifies when changes are made on the spreadsheet shows up on the sender's "Sent" folder but not on the recipient "Inbox" folder. 

What's wrong?
Reply all
Reply to author
Forward
0 new messages