无法发送电子邮件:没有收件人 [英] Failed to send email: no recipient
问题描述
美好的一天!我需要调试以下错误的帮助:无法发送电子邮件:没有收件人".该功能根据范围的值发送电子邮件.请检查下面的代码.
Good day! I need help in debugging this error: "Failed to send email: no recipient". The function sends emails based on the values of a range. Please check the code below.
function sendArticleCountEmails() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.setActiveSheet(ss.getSheetByName("Send-Emails"));
var sheet = SpreadsheetApp.getActiveSheet();
var dataRange = sheet.getRange(2, 1, sheet.getLastRow(), 5);
var data = dataRange.getValues();
for (i in data) {
var rowData = data[i];
var subject = 'DepEd Email/Account';
var FirstName = rowData[0];
var LastName = rowData[1];
var GeneratedEmail = rowData[2];
var Password = rowData[3];
var emailAddress = rowData[4];
MailApp.sendEmail({
to: emailAddress,
subject: subject,
htmlBody: '<h3>' + 'Good day Mr./Mrs./Ms. '+ FirstName + ' ' + LastName +'!</h3>' +
'<p>Here is your email/account (email and password):</p>' +
'<p><span style="background-color: #ffff00;"><strong><a name="gEmail">' + GeneratedEmail + '</a></strong></span></p>' +
'<p><span style="background-color: #ffff00;"><strong><a name="pword">' + Password + '</a></strong></span></p>' +
'<p>You can now login to <a href="https://mail.google.com">mail.google.com</a> to check your account.</p>' +
'<p>If you want to change password, login first then follow this <a href="https://myaccount.google.com/intro/signinoptions/password">link</a>.</p>'
});
}
ss.getSheetByName('Form Responses 2').getRange(254, 12, sheet.getLastRow(), 1).copyTo(ss.getSheetByName('Form Responses 2').getRange(254, 14, sheet.getLastRow(), 1));
}
该函数运行并发送所有电子邮件,但随后出现错误.我怀疑它循环到一个空行并且找不到收件人,但是我已经具有带有sheet.getLastRow()的dataRange.
The function runs and sends all emails but then it gives the error. I suspect it loops to an empty row and can't find a recipient but I already have dataRange with sheet.getLastRow().
推荐答案
通常在Range#getValues()
调用的末尾会返回一个空白行.如果一行中没有数据,我们可以添加一条if语句以跳过发送:
Often there will be a blank row returned at the end from a Range#getValues()
call. We can add an if statement to skip sending if there is no data in a row:
function sendArticleCountEmails() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.setActiveSheet(ss.getSheetByName("Send-Emails"));
var sheet = SpreadsheetApp.getActiveSheet();
var dataRange = sheet.getRange(2, 1, sheet.getLastRow(), 5);
var data = dataRange.getValues();
for (i in data) {
var rowData = data[i];
if(!rowData[4]) continue; //Skip this iternation if no email
var subject = 'DepEd Email/Account';
var FirstName = rowData[0];
var LastName = rowData[1];
var GeneratedEmail = rowData[2];
var Password = rowData[3];
var emailAddress = rowData[4];
MailApp.sendEmail({
to: emailAddress,
subject: subject,
htmlBody: '<h3>' + 'Good day Mr./Mrs./Ms. '+ FirstName + ' ' + LastName +'!</h3>' +
'<p>Here is your email/account (email and password):</p>' +
'<p><span style="background-color: #ffff00;"><strong><a name="gEmail">' + GeneratedEmail + '</a></strong></span></p>' +
'<p><span style="background-color: #ffff00;"><strong><a name="pword">' + Password + '</a></strong></span></p>' +
'<p>You can now login to <a href="https://mail.google.com">mail.google.com</a> to check your account.</p>' +
'<p>If you want to change password, login first then follow this <a href="https://myaccount.google.com/intro/signinoptions/password">link</a>.</p>'
});
}
ss.getSheetByName('Form Responses 2').getRange(254, 12, sheet.getLastRow(), 1).copyTo(ss.getSheetByName('Form Responses 2').getRange(254, 14, sheet.getLastRow(), 1));
}
或者,我们无法获取最后一行:var dataRange = sheet.getRange(2, 1, sheet.getLastRow()-1, 5);
Alternatively, we can not grab the last row: var dataRange = sheet.getRange(2, 1, sheet.getLastRow()-1, 5);
这篇关于无法发送电子邮件:没有收件人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!