简体   繁体   English

在 Google 电子表格中向多个地址发送一封电子邮件

[英]Send one Email to Multiple Addresses in Google Spreadsheet

I'm trying to create code that takes a line that contains the address and sends an email to all addresses in the same email.我正在尝试创建代码,该代码采用包含地址的行并向同一电子邮件中的所有地址发送电子邮件。 But my code sends the email only to the first address on the list and does not add the others.但是我的代码仅将电子邮件发送到列表中的第一个地址,而不会添加其他地址。

 function sendMail(){ var sheetMail = SpreadsheetApp.getActiveSpreadsheet(); var ecrvc = sheetMail.getSheetByName('List'); var lastecrvc = ecrvc.getLastRow(); //var email = []; var people = ","; var subject = "Hello All"; var message = "This is a test"; var email = ecrvc.getRange("B2:B"+lastecrvc).getValues(); for(var i = 9; i<lastecrvc; i++){ var emailTo = ecrvc.getRange(i,2).getValues(); email = emailTo + "@gmail.com"; MailApp.sendEmail(email, subject, message) } }

If you'd like MailApp to send a single email to multiple recipients, you can use a comma separated email string as the first argument of MailApp.sendEmail .如果您希望MailApp向多个收件人发送一封电子邮件,您可以使用逗号分隔的电子邮件字符串作为MailApp.sendEmail的第一个参数。 For example:例如:

const emails = [ "john.doe@email.com", "jane.doe@email.com" ]

// Join the emails into a single string separated by commas
const combinedEmails = emails.join() // "john.doe@email.com,jane.doe@email.com"

MailApp.sendEmail(combinedEmails, subject, message)

That ought to work.那应该有效。 But do verify that you are correctly reading the email addresses from the sheet.但请确认您正确阅读了表格中的电子邮件地址。 If you aren't sure, you could share a sample sheet with identical arrangement as the one you are using.如果您不确定,您可以共享一张与您正在使用的样本表具有相同排列的样本表。

Assuming that you are reading the email addresses from the spreadsheet correctly, you can modify your last for loop slightly to generate a combined email string, and then send the email outside the loop:假设您正确读取电子表格中的电子邮件地址,您可以稍微修改最后一个for循环以生成组合的电子邮件字符串,然后将电子邮件发送到循环之外

var emailsList = [];
for(var i = 9; i<lastecrvc; i++) {
  var emailTo = ecrvc.getRange(i,2).getValues(); 
  email = emailTo + "@gmail.com";
  emailsList.push(email);
}
var combinedEmails = emailsList.join();
MailApp.sendEmail(combinedEmails, subject, message);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM