Update Gmail Signatures of Employees with Google Apps Script

Google Apps allows domain administrators to update the Gmail signatures programatically. This helps you maintain a standard email signature for all users of your organisation but certain fields like employee’s name, email address, title or phone number can be variable.

The Email Settings API is used for creating or retrieving Gmail Signatures and it is only available for Google Apps for Work accounts. Also, you need to have administrator privileges to update company-wide Gmail settings such as signatures. You can use both plain text and rich HTML signatures.

  1. Create a HTML file with the signature template. The entry tag contains the apps:property tag for signature.
<?xml version="1.0" encoding="utf-8"?>
<atom:entry xmlns:atom="http://www.w3.org/2005/Atom" xmlns:apps="http://schemas.google.com/apps/2006">
    <apps:property name="signature" value="SIGNATURE" />
</atom:entry>
  1. Paste this in the code.gs file. If you would like to update the signature of all Google Apps users, use the Google Admin SDK AdminDirectory.Users.list() to get a list of all users and loop through the list.
function updateGmailSignature() {
  var email = 'amit@labnol.org'; // User's email address
  var html = 'Howdy! My <em>email</em> signature!'; // HTML signature

  setEmailSignature(email, html);
}

// Create an HTML encoded string
function createPayload_(html) {
  var str = html
    .replace(/&/g, '&amp;')
    .replace(/</g, '&lt;')
    .replace(/>/g, '&gt;')
    .replace(/'/g, '&#39;')
    .replace(/"/g, '&quot;');

  return HtmlService.createHtmlOutputFromFile('template').getContent().replace('SIGNATURE', str);
}

function getAPI_(email) {
  var scope = 'https://apps-apis.google.com/a/feeds/emailsettings/2.0/',
    user = email.split('@');

  return Utilities.formatString('%s%s/%s/signature', scope, user[1], user[0]);
}

function updateEmailSignature(email, html) {
  var response = UrlFetchApp.fetch(getAPI_(email), {
    method: 'PUT',
    muteHttpExceptions: true,
    contentType: 'application/atom+xml',
    payload: createPayload_(html),
    headers: {
      Authorization: 'Bearer ' + getSignaturesService_().getAccessToken(),
    },
  });

  if (response.getResponseCode() !== 200) {
    Logger.log('ERROR: ' + response.getContentText());
  } else {
    Logger.log('Signature updated');
  }
}

You would also need to include the Oauth2 library with the scope set as https://apps-apis.google.com/a/feeds/emailsettings/2.0/ for the email signature service.

You can also append standard legal disclaimers to the email signatures with this technique. First retrieve the existing Gmail signature of a Google App user, append the text and update the signature.

/* Retrieve existing Gmail signature for any Google Apps user */

function getEmailSignature(email) {
  var response = UrlFetchApp.fetch(getAPI_(email), {
    method: 'GET',
    muteHttpExceptions: true,
    headers: {
      Authorization: 'Bearer ' + getSignaturesService_().getAccessToken(),
    },
  });

  if (response.getResponseCode() !== 200) {
    Logger.log('ERROR: ' + response.getContentText());
  }

  return response.getContentText();
}
Amit Agarwal

Amit Agarwal

Google Developer Expert, Google Cloud Champion

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India.

Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

0

Awards & Titles

Digital Inspiration has won several awards since it's launch in 2004.

Google Developer Expert

Google Developer Expert

Google awarded us the Google Developer Expert award recogizing our work in Google Workspace.

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards in 2017.

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional (MVP) title for 5 years in a row.

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator title recognizing our technical skill and expertise.

Email Newsletter

Sign up for our email newsletter to stay up to date.

We will never send any spam emails. Promise.