Google Script: MailApp.sendEmail to multiple addresses?
Stefan Bogdanescu
Founder & Senior Architect
Google Apps Script: Sending Emails to Multiple Addresses – The Right Way
As developers working with automation tools like Google Apps Script, we often encounter scenarios where the seemingly simple task of sending an email needs to reach several different parties. The scenario you described—trying to pass multiple recipient variables into a single MailApp.sendEmail call—is a very common hurdle. It feels intuitive, but the function is designed for a singular recipient.
Let's dive into why this happens and explore the most robust, scalable solutions for sending mass or multi-recipient emails using Google Apps Script.
The Limitation of MailApp.sendEmail()
The core issue lies in how the built-in Google Apps Script services are structured. The function MailApp.sendEmail(recipient, subject, body, options) is strictly designed to handle one primary recipient address. When you attempt to pass multiple variables where a single string address is expected (like trying to pass row.shiftManager and row.shiftSupervisor), the script fails because it doesn't know how to interpret that combination as a list of destinations.
Your first attempt, passing two recipients directly:
MailApp.sendEmail(row.shiftManager, row.shiftSupervisor, "Holiday Approval Request", "", {htmlBody: message});
This failed because the function expects the second argument (the subject) and subsequent arguments to follow specific types, not a variable-list for recipients.
Your second attempt, which worked by calling the function twice, is functionally correct but leads to repetitive code:
MailApp.sendEmail(row.shiftManager, "Holiday Approval Request", "", {htmlBody: message});
MailApp.sendEmail(row.shiftSupervisor, "Holiday Approval Request", "", {htmlBody: message});
While this achieves the goal, it violates the principle of DRY (Don't Repeat Yourself). If you have ten supervisors to email, writing ten lines of repetitive code is inefficient and difficult to maintain.
Best Practice: Iterating Over Recipients
The most elegant and scalable solution in JavaScript environments like Apps Script is to use a loop to iterate over your list of recipients. This allows you to process each recipient individually while keeping the core logic clean and reusable.
Here is how you can refactor your code to handle multiple recipients gracefully:
function sendHolidayRequests(dataArray) {
const subject = "Holiday Approval Request";
const htmlBody = message; // Assume 'message' holds your HTML content
dataArray.forEach(row => {
// 1. Send email to the manager
MailApp.sendEmail(row.shiftManager, subject, "", {htmlBody: htmlBody});
// 2. Send email to the supervisor
MailApp.sendEmail(row.shiftSupervisor, subject, "", {htmlBody: htmlBody});
});
}
This approach is clear and explicitly handles each recipient separately. However, for scenarios involving very large datasets or complex logic, we can make it even more dynamic by grouping the recipients first.
Advanced Optimization: Using Arrays for Cleaner Iteration
For maximum efficiency and readability, you should structure your data so that you iterate over an array of recipients rather than scattering calls throughout your main processing block. This mirrors modern programming paradigms often seen in backend development, such as when structuring data flows in frameworks like those discussed at laravelcompany.com.
If you could restructure your row object or the input data to contain an array of recipients, the code becomes significantly cleaner:
function sendHolidayRequestsOptimized(dataArray) {
const subject = "Holiday Approval Request";
const htmlBody = message;
dataArray.forEach(row => {
// Define all recipients for this specific row
const recipients = [row.shiftManager, row.shiftSupervisor];
recipients.forEach(emailAddress => {
if (emailAddress) { // Ensure the address exists before sending
MailApp.sendEmail(emailAddress, subject, "", {htmlBody: htmlBody});
} else {
Logger.log(`Skipping email for row ID ${row.id}: Missing recipient address.`);
}
});
});
}
This optimized structure isolates the sending logic and makes it trivial to add or remove recipients in the future. It demonstrates a focus on clean, maintainable code, which is crucial whether you are working with Google Apps Script or any other system dealing with data processing.
Conclusion
While your initial instinct was to cram multiple addresses into one function call, understanding the limitations of built-in APIs is key to effective development. For sending emails to multiple addresses in Google Apps Script, the most reliable and maintainable method involves using iteration (like forEach) to execute the MailApp.sendEmail function for each recipient individually. By adopting this iterative approach, you ensure your scripts are robust, scalable, and easy for other developers—or future you—to understand and debug.