Home / Integrations / GSuite / How to use the Gmail API to update Signatures of GSuite users (PHP)

About The Moo Master

As today's world moves towards more cloud based solutions, it's not always easy to find an all-in-one solution to a business problem. We focus on business development. finding new, creative and innovative ways to use different IT systems to enhance and automate business processes. We're happy to share our knowledge and solutions we've come up with over the years to some of those unique IT/Business problems.

23 comments

  1. Thanks so much for this step-by-step. The instructions in Google’s documentation leave much to be desired as far as helping someone who’s not a hardcore developer to navigate this process.

    Something not mentioned in your procedure above, but which is part of the setup, is the download of the Google API client, and the inclusion of a “require_once” reference in the PHP code.

    Also, I initially got a cURL error about SSL certificates when I first ran your code. A little research revealed that I needed to download an updated cacert.pem file (from https://curl.haxx.se/docs/caextract.html) and reference it in my php.ini file (I saw a thread with a couple of different options as to how to reference it — one as openssl.cafile and one as curl.cainfo — so I used both).

    Another note: Once you “patch” someone’s signature, the end-user will need either to refresh the GMail view in his/her browser, or otherwise log out/in for the change to take effect.

    • Hi Vince,

      Thanks for your feedback – I had included a link to the Google PHP page which outlines how to install using composer. I did originally leave this out as everyone has their own way of installing and managing packages for a project. Bit of an assumption that you already know how to get the package installed 🙂

      If you’re looking for something a bit easier, I’ve created a library which helps in the whole process – Download on Github

      For the SSL certificate, this was something that worked by default when I was testing. But thank you for adding your comments in case anyone else comes across the same issue.

      Best,
      MooMaster

  2. Thanks for this doing this.
    I have your library installed and all the credentials in place. Including the domain-wide delegation. But when I execute: php index.php, I get this message.

    Fatal error: Uncaught Google_Service_Exception: {
    “error”: “unauthorized_client”,
    “error_description”: “Client is unauthorized to retrieve access tokens using this method.”
    }
    in /Applications/AMPPS/www/tests/gmail/vendor/google/apiclient/src/Google/Http/REST.php:118
    Stack trace:
    #0 /Applications/AMPPS/www/tests/gmail/vendor/google/apiclient/src/Google/Http/REST.php(94): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), ‘Google_Service_…’)
    #1 /Applications/AMPPS/www/tests/gmail/vendor/google/apiclient/src/Google/Task/Runner.php(176): Google_Http_REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), ‘Google_Service_…’)
    #2 /Applications/AMPPS/www/tests/gmail/vendor/google/apiclient/src/Google/Http/REST.php(58): Google_Task_Runner->run()
    #3 /Applications/AMPPS/www/tests/gmail/vendor/google/apiclient/src/Google/Client.php(788): Google_Http_REST::execute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), ‘Google_Service_…’, Array)
    #4 / in /Applications/AMPPS/www/tests/gmail/vendor/google/apiclient/src/Google/Http/REST.php on line 118

    I might be missing something simple but I’m not sure.

    • Hi Danny,

      Two things I would check.
      1. Make sure that your service account has domain wide delegation. As per the instructions above.
      2. Make sure the following scopes are set.
      Should look something like the below.
      Gsuite Oauth service account

      Feel free to also send a quick copy of what you have in the index.php file to help debug what else might be going wrong. Also don’t forget to update the index.php file with your own domain settings (otherwise it will use default vaules)

      Best,
      MooMaster

      • Hey Moo Master,
        Thanks for the help. I went through and checked all the setting above step by step. All is set up there.
        With the index.php file im using now I get :
        Fatal error: Uncaught Error: Call to a member function setSubject() on null in /Applications/AMPPS/www/tests/gmail/vendor/moometric/gsuite/src/mooSignature.php:82 Stack trace: #0 /Applications/AMPPS/www/tests/gmail/vendor/moometric/gsuite/src/mooSignature.php(171): Moometric\mooSignature->getUsersList() #1 /Applications/AMPPS/www/tests/gmail/index.php(30): Moometric\mooSignature->updateSignatures() #2 {main} thrown in /Applications/AMPPS/www/tests/gmail/vendor/moometric/gsuite/src/mooSignature.php on line 82

        my index.php right now is:

        addSettingRunTestMode(True);
        $mooSig->addSettingPreviewSignature(True);

        // OPTIONAL – Setting the service account path and signature path if not using default location
        //$mooSig->addSettingServiceAccountPath($serviceAccountPath);
        //$mooSig->addsettingSignaturePath($sigPath);

        // Setting test and preview mode so no changes are written
        $mooSig->addSettingRunTestMode(True);
        $mooSig->addSettingPreviewSignature(True);

        // Setting the default signature
        $mooSig->addSettingSetTemplate(“defaultSig.html”);
        echo “Updating a single user from domain”;

        // Example 1: setting a single user from domain
        $mooSig->addSettingGetUsersFromGsuite(True);
        $mooSig->addSettingFilterEmailsToUpdate([“$admin_email”]);
        $mooSig->updateSignatures();

        • Hi Danny,

          Sorry about my late reply.

          Please try testing the below snippet and make sure you update the parts with “REPLACE” with your own settings.

          < ?php require __DIR__ . '/vendor/autoload.php'; use Moometric\mooSignature; $admin_email = "[email protected]"; // REPLACE THIS WITH YOUR ADMIN EMAIL $domain = "domain.com"; //REPLACE THIS WITH YOUR DOMAIN $mooSig = new mooSignature($domain, $admin_email); $mooSig->addSettingServiceAccountPath(“/your/project/path/local_vars/”); // REPLACE WITH PATH TO SERVICE ACCOUNT DIR
          $mooSig->addsettingSignaturePath(“/your/project/path/signatures/”); // REPLACE WITH PATH TO SIGNATURES DIR
          $mooSig->addSettingSetTemplate(“defaultSig.html”);
          $mooSig->addSettingRunTestMode(True);
          $mooSig->addSettingPreviewSignature(True);
          $mooSig->addSettingGetUsersFromGsuite(True);
          $mooSig->addSettingFilterEmailsToUpdate([“$admin_email”]);
          $mooSig->updateSignatures();

    • @ Danny, how did you resolve this issue “Client is unauthorized to retrieve access tokens using this method”. Even i have the same errors that you had mentioned above.

  3. Hello,

    I’m trying to use your github instructions, but when I load index.php, I got the following erros:

    Warning: include(C:\xampp\htdocs\GSuiteSignatureManager-master\vendor\composer/../../src/Gsuite.php): failed to open stream: No such file or directory in C:\xampp\htdocs\GSuiteSignatureManager-master\vendor\composer\ClassLoader.php on line 444

    Warning: include(): Failed opening ‘C:\xampp\htdocs\GSuiteSignatureManager-master\vendor\composer/../../src/Gsuite.php’ for inclusion (include_path=’C:\xampp\php\PEAR’) in C:\xampp\htdocs\GSuiteSignatureManager-master\vendor\composer\ClassLoader.php on line 444

    Fatal error: Uncaught Error: Class ‘Moometric\mooSignature’ not found in C:\xampp\htdocs\GSuiteSignatureManager-master\index.php:11 Stack trace: #0 {main} thrown in C:\xampp\htdocs\GSuiteSignatureManager-master\index.php on line 11

    Please, could you help me this?

  4. Hi, Thank you for this code. How can i modify to update multiple signatures? Lets say you want to standardize signatures for a whole organization, by running the script once.

  5. Hola, me puedes ayudar con estos Errores:

    Warning: include(C:\xampp\htdocs\moometric\GSuite\vendor\composer/../../src/Gsuite.php): failed to open stream: No such file or directory in C:\xampp\htdocs\moometric\GSuite\vendor\composer\ClassLoader.php on line 444

    Warning: include(): Failed opening ‘C:\xampp\htdocs\moometric\GSuite\vendor\composer/../../src/Gsuite.php’ for inclusion (include_path=’C:\xampp\php\PEAR’) in C:\xampp\htdocs\moometric\GSuite\vendor\composer\ClassLoader.php on line 444

    Fatal error: Class ‘Moometric\mooSignature’ not found in C:\xampp\htdocs\moometric\GSuite\index.php on line 11

  6. I got that error 444 as well and found that if I renamed the file in the project/src/ folder from MooSignature.php to Gsuite.php it ran ok. Looks like there was an update a few months ago.

  7. Hi – is there an update on how to overcome these errors:

    $ php index.php

    Fatal error: Uncaught Google_Service_Exception: {
    “error”: “unauthorized_client”,
    “error_description”: “Client is unauthorized to retrieve access tokens using this method.”
    } in /Users/xxx/support/tmp/GSuiteSignatureManager/vendor/google/apiclient/src/Google/Http/REST.php:118
    Stack trace:
    #0 /Users/xxx/support/tmp/GSuiteSignatureManager/vendor/google/apiclient/src/Google/Http/REST.php(94): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), ‘Google_Service_…’)
    #1 /Users/xxx/support/tmp/GSuiteSignatureManager/vendor/google/apiclient/src/Google/Task/Runner.php(176): Google_Http_REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), ‘Google_Service_…’)
    #2 /Users/xxx/support/tmp/GSuiteSignatureManager/vendor/google/apiclient/src/Google/Http/REST.php(58): Google_Task_Runner->run()
    #3 /Users/xxx/support/tmp/GSuiteSignatureManager/vendor/google/apiclient/src/Google/Client.php(798): Google_Http_REST::execute(Ob in /Users/xxx/support/tmp/GSuiteSignatureManager/vendor/google/apiclient/src/Google/Http/REST.php on line 118

    My php:

    addSettingServiceAccountPath(“/Users/xxx/support/tmp/GSuiteSignatureManager/local_vars/”); // REPLACE WITH PATH TO SERVICE ACCOUNT DIR
    $mooSig->addsettingSignaturePath(“/Users/xxx/support/tmp/GSuiteSignatureManager/signatures/”); // REPLACE WITH PATH TO SIGNATURES DIR
    $mooSig->addSettingSetTemplate(“defaultSig.html”);
    $mooSig->addSettingRunTestMode(True);
    $mooSig->addSettingPreviewSignature(True);
    $mooSig->addSettingGetUsersFromGsuite(True);
    $mooSig->addSettingFilterEmailsToUpdate([“$admin_email”]);
    $mooSig->updateSignatures();
    ?>

  8. Hi, thank you so much for this, it does exactly what it says on the tin and was easy to put together using your instructions.

    However I do have one question, how are you maintaining email signatures on gmail apps?
    The signature that I have put together doesn’t display on android or IOS.

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: