google-apps-scriptgmail-api

Send mails from alias using alias' SMTP in Google Script


I have a Google script that sends mails given some data in a Spreadsheet. The mail is sent with GmailApp using an alias set on Gmail settings. This alias is a PEC, the italian "certified mail account".

This is the function that sends the mail

var aliases = GmailApp.getAliases()
var fileXML = DriveApp.getFileById(xmlId);

var subject = fileXML.getName().slice(0, fileXML.getName().length - 4);

GmailApp.sendEmail
(
  toMailPECAddress,
  subject,
  '',
  {
    // Send from alias (PEC account)
    // Alias must be configured via GMail settings
    'from': aliases[0],
    'attachments': [fileXML]
  }
);

This always worked. Since a couple of weeks ago, when I started getting a "service unavailable: gmail" error.

So far I did these test:

What can be the problem?

Is there any new limitation that doesn't allow to send mails from aliases, or from non standard SMTP (from a PEC in my case)?


Solution

  • There seems to be an issue on Google's end that's causing the error since removing the attachment from the code sends the email. What I recommend is to submit a bug report about it and for the meantime, I've noticed that if you add something on the body, the code works.

    From:

    var aliases = GmailApp.getAliases()
    var fileXML = DriveApp.getFileById(xmlId);
    
    var subject = fileXML.getName().slice(0, fileXML.getName().length - 4);
    
    GmailApp.sendEmail
    (
      toMailPECAddress,
      subject,
      '',
      {
        // Send from alias (PEC account)
        // Alias must be configured via GMail settings
        'from': aliases[0],
        'attachments': [fileXML]
      }
    );
    

    To:

    var aliases = GmailApp.getAliases()
    var fileXML = DriveApp.getFileById(xmlId);
    
    var subject = fileXML.getName().slice(0, fileXML.getName().length - 4);
    
    GmailApp.sendEmail
    (
      toMailPECAddress,
      subject,
      ' ', // Add a blank space or any character
      {
        // Send from alias (PEC account)
        // Alias must be configured via GMail settings
        'from': aliases[0],
        'attachments': [fileXML]
      }
    );