Gmail API for managing multiple signatures

Viewed 826

Google recently released an update to Gmail to bring support for multiple signatures. Ref: https://support.google.com/mail/answer/8395.

I do not see anything in the API documentation at https://developers.google.com/gmail/api/v1/reference that talks about how to manage those multiple signatures. How can I:

  • create a new signature
  • update a specific existing signature
  • associate a signature to an email address - both the "for new emails use" and "on reply/forward use"

Is there any documentation on this?

1 Answers

It is a bit hidden, the signature(s) need to be created with Users.settings.sendAs: create or update

As specified for the resource, signature is one of the parameters that can be modified and you do not need to create a new alias, but can also apply this method to your primaary email:

Settings associated with a send-as alias, which can be either the primary login address associated with the account or a custom "from" address.

Important: Access restricted to service accounts that have been delegated

Sample with Apps Script:

function createAlias() {
  var alias = 'your primary email';
  var signature = 'Your signature';

  var service = getOAuthService();
  service.reset();
  if (service.hasAccess()) {

    var url = 'https://www.googleapis.com/gmail/v1/users/me/settings/sendAs'
    var headers ={
      "Authorization": 'Bearer ' + service.getAccessToken(),
      "Accept":"application/json", 
      "Content-Type":"application/json",
    };

    var resource ={
      sendAsEmail: alias,
      signature: signature,

    };

    var options = {
      'headers': headers,
      'method': 'POST',
      'payload':JSON.stringify(resource),
      'muteHttpExceptions': true
    };

    var response = UrlFetchApp.fetch(url, options);
    Logger.log(response.getContentText());

  }
}

Necessary scope: https://www.googleapis.com/auth/gmail.settings.sharing

Related