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.
- 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>
- 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, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/'/g, ''')
.replace(/"/g, '"');
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
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