Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Triggers not sending emails in google scripts

248 views
Skip to first unread message

paddy lukwago

unread,
Apr 7, 2025, 11:24:52 AMApr 7
to Google Apps Script Community
Hello I have created a function to send emails to selected members. But when I go to triggers to try and run the function. In the executions it shows complete but in my inbox the email is not coming. What could be the problem?

Keith Andersen

unread,
Apr 7, 2025, 11:47:04 AMApr 7
to google-apps-sc...@googlegroups.com

"Go to triggers to run the code."

Can you explain this? Is it a timed trigger? onEdit()? onChange()?



My website: https://sites.google.com/view/klaweb/
Passions: God, Family, Scriptures, Learning, Data Management, Google Sheets + App Script and much more!

On Mon, Apr 7, 2025, 10:24 AM paddy lukwago <lukwag...@gmail.com> wrote:
Hello I have created a function to send emails to selected members. But when I go to triggers to try and run the function. In the executions it shows complete but in my inbox the email is not coming. What could be the problem?

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/google-apps-script-community/76a2766c-08b5-4111-b49d-e0eed6a7fce9n%40googlegroups.com.

paddy lukwago

unread,
Apr 7, 2025, 12:52:37 PMApr 7
to google-apps-sc...@googlegroups.com
Yes it is a time trigger 

You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/zBIj9m10tmo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/google-apps-script-community/CAFKgK%2BEj8gR3g8skLZYxz8OiCkyTgGbgAnmgX0tTRWL3dNg3WA%40mail.gmail.com.

Keith Andersen

unread,
Apr 7, 2025, 1:17:23 PMApr 7
to google-apps-sc...@googlegroups.com

How then are you "going to triggers" to run the code? Are you simply calling the function from the editor?

Are you getting log errors? What is your code?



My website: https://sites.google.com/view/klaweb/
Passions: God, Family, Scriptures, Learning, Data Management, Google Sheets + App Script and much more!

paddy lukwago

unread,
Apr 7, 2025, 1:30:28 PMApr 7
to google-apps-sc...@googlegroups.com
I am not getting any errors. 

I am say I am still a novice at this. 

Yes I am calling the function 



Keith Andersen

unread,
Apr 7, 2025, 1:32:25 PMApr 7
to google-apps-sc...@googlegroups.com

What is your code/function?

Have you checked spam to make sure the message wasn't sent there?



My website: https://sites.google.com/view/klaweb/
Passions: God, Family, Scriptures, Learning, Data Management, Google Sheets + App Script and much more!

paddy lukwago

unread,
Apr 7, 2025, 1:35:21 PMApr 7
to google-apps-sc...@googlegroups.com

paddy lukwago

unread,
Apr 7, 2025, 1:45:26 PMApr 7
to google-apps-sc...@googlegroups.com
I am going to share my code 

paddy lukwago

unread,
Apr 7, 2025, 6:02:39 PMApr 7
to google-apps-sc...@googlegroups.com
Hi,

Please find attached script/function as requested 

function myFunction() {
function sendMonthlyUpdates() {
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues(); // Get all data
var date = new Date();
var monthIndex = date.getMonth(); // 0 = July, 11 = June
var monthNames = ["July", "August", "September", "October", "November", "December",
"January", "Feburary", "March", "April", "May", "June"];
var currentMonth = monthNames[monthIndex];
var year = date.getFullYear();

// Loop through each member (skip header row)
for (var i = 1; i < data.length; i++) {
var name = data[i][0];
var email = data[i][1];
var totalDue = data[i][2];
var payments = data[i].slice(3, 15); // Jul to Jun payments (columns D to O)
var totalPaid = data[i][15]; // Total Paid (column P)
var balance = data[i][16]; // Balance Remaining (column Q)

// Calculate payments up to last month
var paidSoFar = 0;
for (var j = 0; j < monthIndex; j++) {
paidSoFar += payments[j];
}

// Email subject
var subject = "Rotary Club Payment Status - " + currentMonth + " " + year;

// Email body
var message = "Dear " + name + ",\n\n" +
"Here is your payment status as of " + currentMonth + " " + year + ":\n" +
"Total Due: " + totalDue.toLocaleString() + "\n" +
"Total Paid (Jan-" + monthNames[monthIndex - 1] + "): " + paidSoFar.toLocaleString() + "\n" +
"Overall Total Paid: " + totalPaid.toLocaleString() + "\n" +
"Balance Remaining: " + balance.toLocaleString() + "\n\n" +
"Please make your " + currentMonth + " payment at your earliest convenience.\n" +
"Contact the treasurer with any questions.\n" +
"Thank you,\nRotary Club Treasurer";

// Send email
MailApp.sendEmail(email, subject, message);
}
}
}

paddy lukwago

unread,
Apr 7, 2025, 6:03:22 PMApr 7
to google-apps-sc...@googlegroups.com

George Ghanem

unread,
Apr 7, 2025, 9:16:19 PMApr 7
to google-apps-sc...@googlegroups.com
The nested functions is useless. Remove the MyFunction all together along with its curly brackets.

What function were you calling in your triggers?

paddy lukwago

unread,
Apr 7, 2025, 10:48:00 PMApr 7
to google-apps-sc...@googlegroups.com

Keith Andersen

unread,
Apr 7, 2025, 10:59:56 PMApr 7
to google-apps-sc...@googlegroups.com
Put:
console.log( email );

directly under your email variable and then run the code in the editor and let us know the output in the console.




--

Passions: God, Family, Friends, Scripture, Data Management, Google Sheets + App Script, MS Access, Programing, sharing and much more.

paddy lukwago

unread,
Apr 7, 2025, 11:23:47 PMApr 7
to Google Apps Script Community
Image 08-04-2025 at 06.21.jpeg
Is this Okay?

Keith Andersen

unread,
Apr 7, 2025, 11:41:32 PMApr 7
to google-apps-sc...@googlegroups.com
function myFunction() {
function sendMonthlyUpdates() {
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues(); // Get all data
var date = new Date();
var monthIndex = date.getMonth(); // 0 = July, 11 = June
var monthNames = ["July", "August", "September", "October", "November", "December",
"January", "Feburary", "March", "April", "May", "June"];
var currentMonth = monthNames[monthIndex];
var year = date.getFullYear();

// Loop through each member (skip header row)
for (var i = 1; i < data.length; i++) {
var name = data[i][0];
var email = data[i][1];//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Keith Andersen

unread,
Apr 7, 2025, 11:43:13 PMApr 7
to google-apps-sc...@googlegroups.com
It should look like this:

function myFunction() {
function sendMonthlyUpdates() {
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues(); // Get all data
var date = new Date();
var monthIndex = date.getMonth(); // 0 = July, 11 = June
var monthNames = ["July", "August", "September", "October", "November", "December",
"January", "Feburary", "March", "April", "May", "June"];
var currentMonth = monthNames[monthIndex];
var year = date.getFullYear();

// Loop through each member (skip header row)
for (var i = 1; i < data.length; i++) {
var name = data[i][0];
var email = data[i][1];//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
console.log( email );//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Keith Andersen

unread,
Apr 8, 2025, 12:52:25 AMApr 8
to google-apps-sc...@googlegroups.com
Please run the code and let us know what is output in the console.

paddy lukwago

unread,
Apr 8, 2025, 1:39:28 AMApr 8
to google-apps-sc...@googlegroups.com
Image 08-04-2025 at 08.38.jpeg

Keith Andersen

unread,
Apr 8, 2025, 1:42:11 AMApr 8
to google-apps-sc...@googlegroups.com

It should show emails in the console. If it's showing nothing - your for loops are not retrieving data correctly.

The only way to help you with this is to view the spreadsheet and the code. Can you create a mock spreadsheet with dummy data and share the sheet?



My website: https://sites.google.com/view/klaweb/
Passions: God, Family, Scriptures, Learning, Data Management, Google Sheets + App Script and much more!

Keith Andersen

unread,
Apr 8, 2025, 1:42:53 AMApr 8
to google-apps-sc...@googlegroups.com

If you need to share it privately. Just share it to my email address.



My website: https://sites.google.com/view/klaweb/
Passions: God, Family, Scriptures, Learning, Data Management, Google Sheets + App Script and much more!

paddy lukwago

unread,
Apr 8, 2025, 1:44:02 AMApr 8
to google-apps-sc...@googlegroups.com

paddy lukwago

unread,
Apr 8, 2025, 1:46:30 AMApr 8
to google-apps-sc...@googlegroups.com
Sent to your private email address the spreadsheet 
On Tue, 8 Apr 2025 at 8:42 AM, Keith Andersen <contact...@gmail.com> wrote:

Keith Andersen

unread,
Apr 8, 2025, 1:59:29 AMApr 8
to google-apps-sc...@googlegroups.com
Delete your MyFunction and replace it with:
Then run this function with the editor.
Then adjust your timed triggers to call the above function.

Keith Andersen

unread,
Apr 8, 2025, 2:20:11 AMApr 8
to google-apps-sc...@googlegroups.com

Did you get it working?



My website: https://sites.google.com/view/klaweb/
Passions: God, Family, Scriptures, Learning, Data Management, Google Sheets + App Script and much more!

paddy lukwago

unread,
Apr 8, 2025, 2:21:38 AMApr 8
to google-apps-sc...@googlegroups.com
Yes I did now. I want further enhance it to include my club logo.

Kind regards 

Keith Andersen

unread,
Apr 8, 2025, 3:06:45 AMApr 8
to google-apps-sc...@googlegroups.com

What is your logo? Can you attach it?



My website: https://sites.google.com/view/klaweb/
Passions: God, Family, Scriptures, Learning, Data Management, Google Sheets + App Script and much more!

paddy lukwago

unread,
Apr 8, 2025, 5:34:51 AMApr 8
to Google Apps Script Community
Thank You all,

Problem was resolved. The issue was with the script.

Thanks 

paddy lukwago

unread,
Apr 8, 2025, 5:40:33 AMApr 8
to google-apps-sc...@googlegroups.com

Brent Guttmann

unread,
Apr 8, 2025, 10:05:48 PMApr 8
to Google Apps Script Community
Saw a couple things that looked incorrect to me... namely the month calculations. Could be wrong though. But, here is the updated script including a logo.... you'll need to add the logo to google drive, share it publically, get the url for the shared image, then copy only the ID of the Google drive file from the url and replace it in the script. Hope that helps ya, I realize I'm chiming in late... was just taking a gander.

function sendMonthlyUpdates() {
  var sheet                  = SpreadsheetApp.getActiveSheet();
  var data                    = sheet.getDataRange().getValues();
  var date                    = new Date();
  var monthIndex       = date.getMonth(); // 0 = Jan, 11 = Dec
  var year                     = date.getFullYear();

  var logoFileId            = "1MH3T0iN5ESfsulpdi6UGbc0FJWqISECY";
  var logoUrl                 = `https://drive.google.com/uc?export=view&id=${logoFileId}`;

  var monthNames      = ["January", "February", "March", "April", "May", "June",
                          "July", "August", "September", "October", "November", "December"];

  var currentMonth      = monthNames[monthIndex];
  var previousMonth    = monthNames[(monthIndex + 11) % 12];
  var fiscalMonthIndex = (monthIndex + 5) % 12; // Fiscal year starts July = 0


  for (var i = 1; i < data.length; i++) {
    var name          = data[i][0];
    var email          = data[i][1];
    var totalDue.    = Number(data[i][2]) || 0;
    var payments   = data[i].slice(3, 15).map(p => Number(p) || 0);
    var totalPaid.    = Number(data[i][15]) || 0;
    var balance       = Number(data[i][16]) || 0;

    var paidSoFar  = 0;
    for (var j = 0; j < fiscalMonthIndex; j++) {
      paidSoFar += payments[j];
    }

    var subject = `Rotary Club Payment Status – ${currentMonth} ${year}`;

    var messageHtml = `
      <div style="margin: 0; padding: 0; background: #f9f9f9; font-family: Arial, sans-serif;">
        <table align="center" cellpadding="0" cellspacing="0" width="100%" style="max-width: 600px; margin: auto; background: #ffffff; border: 1px solid #e0e0e0; border-radius: 8px; overflow: hidden;">
          <tr>
            <td style="padding: 20px;">
              <table width="100%" cellpadding="0" cellspacing="0">
                <tr>
                  <td style="vertical-align: top; text-align: left;">
                    <img src="${logoUrl}" alt="Rotary Club Logo" style="width: 120px; max-width: 100%; height: auto; display: block;">
                  </td>
                  <td style="text-align: right; font-size: 14px; color: #999;">
                    ${currentMonth} ${year}
                  </td>
                </tr>
              </table>
            </td>
          </tr>
          <tr>
            <td style="padding: 20px; font-size: 15px; color: #333;">
              <p style="font-size: 16px;">Dear <strong>${name}</strong>,</p>
              <p>We hope this message finds you well. Below is your current payment status as of <strong>${currentMonth} ${year}</strong>:</p>

              <table width="100%" cellpadding="0" cellspacing="0" style="margin-top: 15px; margin-bottom: 20px;">
                <tr>
                  <td style="padding: 8px; border-bottom: 1px solid #eee;"><strong>Total Due:</strong></td>
                  <td style="padding: 8px; border-bottom: 1px solid #eee;">$${totalDue.toLocaleString()}</td>
                </tr>
                <tr>
                  <td style="padding: 8px; border-bottom: 1px solid #eee;"><strong>Total Paid (Jul–${previousMonth}):</strong></td>
                  <td style="padding: 8px; border-bottom: 1px solid #eee;">$${paidSoFar.toLocaleString()}</td>
                </tr>
                <tr>
                  <td style="padding: 8px; border-bottom: 1px solid #eee;"><strong>Overall Total Paid:</strong></td>
                  <td style="padding: 8px; border-bottom: 1px solid #eee;">$${totalPaid.toLocaleString()}</td>
                </tr>
                <tr>
                  <td style="padding: 8px;"><strong>Balance Remaining:</strong></td>
                  <td style="padding: 8px;">$${balance.toLocaleString()}</td>
                </tr>
              </table>

              <p>Please make your <strong>${currentMonth}</strong> payment at your earliest convenience. If you have any questions, feel free to reach out to the club treasurer.</p>

              <p style="margin-top: 30px;">
                Thank you,<br>
                <strong>Rotary Club Treasurer</strong>
              </p>
            </td>
          </tr>
          <tr>
            <td style="padding: 20px; text-align: center; font-size: 12px; color: #999; background: #f1f1f1;">
              This message was sent to ${email}.<br>
              Please disregard if payment has already been made.
            </td>
          </tr>
        </table>
      </div>
    `;

    try {
      MailApp.sendEmail({
        to: email,
        subject: subject,
        htmlBody: messageHtml
      });
    } catch (err) {
      Logger.log(`Failed to send to ${email}: ${err}`);
    }
  }
}

Keith Andersen

unread,
Apr 8, 2025, 10:30:35 PMApr 8
to google-apps-sc...@googlegroups.com

Brent - 👍👍



My website: https://sites.google.com/view/klaweb/
Passions: God, Family, Scriptures, Learning, Data Management, Google Sheets + App Script and much more!

paddy lukwago

unread,
Apr 9, 2025, 8:59:14 AMApr 9
to google-apps-sc...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages