I have problem with MailApp.sendEmail()
.
I try to send email by script in Google Spreadsheet. My script works on my account, but does not work on my friend's account. I get this message:
Message rejected. See https://support.google.com/mail/answer/69585 for more information. Final-Recipient: rfc822; [email protected] Action: failed Status: 5.0.0 Diagnostic-Code: smtp; Message rejected. See https://support.google.com/mail/answer/69585 for more information. Last-Attempt-Date: Wed, 21 Mar 2018 05:41:31 -0700 (PDT)
I used the Martin Hawksey tutorial script (with some modifications):
var TO_ADDRESS = "[email protected]";
// spit out all the keys/values from the form in HTML for email
function formatHtmlMailBody(obj, order) {
var result = "html text";
// loop over all keys in the ordered form data
for (var idx in order) {
var key = order[idx];
result += key + " " + obj[key];
}
return result;
}
function formatTextMailBody(obj, order) {
var result = "text";
for (var idx in order) {
var key = order[idx];
result += key + "\n" + obj[key] + "\n\n";
}
return result;
}
function doPost(e) {
try {
Logger.log(e); // the Google Script version of console.log see: Class Logger
record_data(e);
var mailData = e.parameters;
var dataOrder = JSON.parse(e.parameters.formDataNameOrder);
var sendEmailTo = (typeof TO_ADDRESS !== "undefined") ? TO_ADDRESS : mailData.formGoogleSendEmail;
MailApp.sendEmail(
String(sendEmailTo),
'title',
formatTextMailBody(mailData, dataOrder), { // body
htmlBody: formatHtmlMailBody(mailData, dataOrder), // advanced options
name: "name",
replyTo: String(mailData.Email)
}
);
//second mail
try {
var htmlBodyToSender = "html text";
var textBodyToSender = "text";
MailApp.sendEmail(
String(mailData.Email), //to
'title', //subject
textBodyToSender, { // body
htmlBody: htmlBodyToSender, // advanced options
name: "name",
replyTo: String(sendEmailTo)
}
);
}
catch (error) {
Logger.log(error);
}
return ContentService // return json success results
.createTextOutput(
JSON.stringify({
"result": "success",
"data": JSON.stringify(e.parameters)
}))
.setMimeType(ContentService.MimeType.JSON);
} catch (error) { // if error return this
Logger.log(error);
return false;
}
}
Delivery Status Notification
The message content is governed by "An Extensible Message Format for Delivery Status Notifications" since it is a delivery status notification (DSN) for the failed email. Let's parse the error message field by field:
failed
, delayed
, delivered
, relayed
, or expanded
. In your case it indicates that the email failed to be delivered.500
error just means "Other or undefined protocol status" without further clarification.failed
or delayed
value). Nothing too specific in your case, Google simply points to the list of possible bounce reasons.The problem
As you can see from the above, the error does not mean there is anything wrong with your code. This also explains why the script "works" for some accounts: the problem is either with Google's infrastructure or with the intended recipient's security configuration.
Since the first is usually accompanied by a more specific DSN, you should check the email security configuration of the account(s) affected. In case everything looks normal, switch to GmailApp
's similar sendEmail
method that gives you a more fine-grained control or use the advanced service which allows you to construct the message from scratch.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With