Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emails are rejected when using MailApp.sendEmail

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;
    }
}
like image 568
Marek Domański Avatar asked Mar 21 '18 13:03

Marek Domański


1 Answers

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:

  1. Final-Recipient: rfc822; [email protected] - just a pointer to the type and address of the recipient after all modifications of forwarding or gatewaying.
  2. Action: failed - an enum representing delivery status, can be failed, delayed, delivered, relayed, or expanded. In your case it indicates that the email failed to be delivered.
  3. Status: 5.0.0 - email delivery status codes are governed by RFC 3463, and the 500 error just means "Other or undefined protocol status" without further clarification.
  4. Diagnostic-Code: smtp; - contains the type and reason of failure (is present only for "action" field with failed or delayed value). Nothing too specific in your case, Google simply points to the list of possible bounce reasons.
  5. Last-Attempt-Date: Wed, 21 Mar 2018 05:41:31 -0700 (PDT) - finally, this field contains a timestamp of the last time of attempting to, quoting the standard, "relay, gateway, or deliver" the email.

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.

like image 135
Oleg Valter is with Ukraine Avatar answered Oct 04 '22 22:10

Oleg Valter is with Ukraine