- 1 Setup the gmail API
- 2 Grant Domain-Wide Delegation (GSuite)
- 3 Get/Update Signature in PHP
If you’re like me, you are an admin looking at how you can use the Gmail API to update signatures of gsuite users, using server side authorization to save all the fuss of OAuth prompts. The goal is to simply have your server talk to and be able to update gsuite users (gmail signatures in particular). If you know a little bit of PHP, then this will be very easy for most admins to follow. This solution is great if you are an admin and want everyone to have a standard email signature.
Setup the gmail API
For this first part of the process we want to setup a new project or enable the gmail API for an already existing project. After this has been done we will create a service account which will be delegated to access parts of your GSuite account. You can think of this service account as just another admin user with rights to modify certain resources on your GSuite account.
- Follow the getting started guide through the link here to create a new project, service account and key.
Create a new project and enable to API
Create a new Service Account and download the JSON file
Once you click ‘Continue’, the JSON Key file will automatically download. Store this in a safe place and NEVER share this file with anyone.
Enable GSuite OAuth/Domain Wide Delegation
Make a copy of your “Client ID”
Your client ID will be used in the next step and added into your GSuite Security Settings.
Grant Domain-Wide Delegation (GSuite)
Now that you have your client ID, you can browse into the Security settings (You must be a GSuite Admin) and add in your client ID and the following scopes.
- SCOPE 1: https://www.googleapis.com/auth/gmail.settings.basic
- SCOPE 2: https://www.googleapis.com/auth/gmail.settings.sharing
Both of these scopes are used to set the Signature (and other settings). If you want additional scopes you can check out and enable the ones you need in the Gmail Reference Guide.
Copy paste in your client ID and the two scopes
That’s the hardest part done. After completing this step you will now have a service account which can talk to the Gmail API
Get/Update Signature in PHP
If you don’t want to use PHP, then Google offers libraries for most of the popular languages.
If you’re already familiar with PHP, you can also follow the very easy guide on the Gmail API page to import the library and start making your own calls.
Otherwise, you can follow the below to utilize the basic commands of the Gmail library.
Set all your variables
Copy your service-account.json file which you downloaded earlier into a folder that is accessible by your local web server.
//This is the primary email of the person whos' signature you want to update
$user_to_impersonate = "[email protected]";
//Copy of your service-account.json file which includes the key for authentication
//Instantiates the Google client class with the scopes and variables to update the signature
$client = new Google_Client();
//Instantiates the Gmail services class with the client information
$gmail = new Google_Service_Gmail($client);
PATCH (update) a users signature
//Set the HTML for your new signature
$newSignature = "Enter your HTML here"
// Instantiate the Gmail SendAs class and set the signature value
$signature = new Google_Service_Gmail_SendAs();
//Send the request to update the signature