Update Gmail Signatures of Employees with Google Apps Script

U
Published in: Gmail - 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();

}
📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Gmail - Google Apps Script

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
A
Amit Agarwal

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. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account
G

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube