1

I sucessfully created a script to generate signatures to users in g-suite domain using this topic as a guide. Now my problem is that this script only changes signatures "for new email use" but i need that "on reply/forward use" signatures to be update also using this script.

Is it possible?

This is the code i used as example and it is working to change signatures when composing new e-mails.

function setSignatureWithHTTPRequest(email, signature) {
  var signatureSetSuccessfully = false;
  var authorizationScope = ['https://www.googleapis.com/auth/gmail.settings.sharing'];
  
  var service = getDomainWideDelegationService("Gmail: ", authorizationScope, email);

  if (!service.hasAccess()) {
    Logger.log("failed to authenticate as user " + email);
    Logger.log(service.getLastError());

    signatureSetSuccessfully = service.getLastError();

    return signatureSetSuccessfully;
  } else {
    Logger.log("successfully authenticated as user " + email);
  }
  
  var username = email.split("@")[0];
  var resource = { signature: signature };
  
  var requestBody                = {};
  requestBody.headers            = {"Authorization": "Bearer " + service.getAccessToken()};
  requestBody.contentType        = "application/json";
  requestBody.method             = "PUT";
  requestBody.payload            = JSON.stringify(resource);
  requestBody.muteHttpExceptions = false;

  var emailForUrl = encodeURIComponent(email);

  var url = "https://www.googleapis.com/gmail/v1/users/me/settings/sendAs/" + emailForUrl;

  try {
    var setSignatureResponse = UrlFetchApp.fetch(url, requestBody);
    signatureSetSuccessfully = true;
    Logger.log("setSignatureResponse on successful attempt:" + setSignatureResponse);
  } catch (e) {
    Logger.log("Set signature with HTTP request failed: " + e);
  }
  
  return signatureSetSuccessfully;
}

Thanks in advance.

EDIT: Just to make clear what i need help with: In gmail's configuration (gmail -> sell all settings) web interface you can see that the field "signature" has two options to set signatures, "for new emails use" and "on reply/forward use". When i run my script it automatically changes only the first option (for new emails use), which means that whenever i compose emails it will send with the right signature i created from script, but it dosen't change the second one (on reply/forward use), which means that when i reply or forward e-mails it will not use the signature i created, unless i manually change it.

Can someone help me with that please? I'm still stuck with this.

adolfpc
  • 11
  • 2
  • I have to apologize for my poor English skill. Unfortunately, I cannot understand about your goal of `i need that "on reply/forward use" signatures to be update also using this script.`. Do you want to modify the existing signatures? Or do you want to select the specific signature by changing from the existing signatures? Or can I ask you about the detail of your goal? – Tanaike Aug 12 '20 at 02:01
  • Hi Tanaike, thanks for you reply, my english is not very good so maybe i wasn't clear. In gmail's configuration (gmail -> sell all settings) web interface you can see that the field "signature" has two options to set signatures, "for new emails use" and "on reply/forward use". When i run my script it automatically changes only the first option (for new emails use), which means that whenever i compose emails it will send with the right signature i created from script, but it dosen't change the second one, which means that when i reply or forward e-mails it will not use the signature i created. – adolfpc Aug 12 '20 at 02:49
  • Thank you for replying. I understood that you want to select the signature from several registered signatures. In this case, although I was looking for the method for achieving this, unfortunately, I couldn't find it. When the method of "Users.settings.sendAs: list" in Gmail API is used, the default signature can be retrieved. But other registered signatures cannot be retrieved. I apologize for this. – Tanaike Aug 12 '20 at 06:37
  • Hi there @adolfpc! You can [list](https://developers.google.com/gmail/api/v1/reference/users/settings/sendAs/list) your *send as* addresses like Tanaike said. You'll receive your addresses as listed in the user interface settings `Settings > Accounts > Send email as`. I don't understand what is your question about it, could you please share your desired results? – Jacques-Guzel Heron Aug 13 '20 at 13:21
  • hello @Jacques-GuzelHeron, thank you for replying. I'm using the sendAs method to update signatures of my gsuite domain, but this method only update the signature when they write new e-mails. For the signature to appear when they are replying e-mails they need to manually add the signature in the gmail settings, scroll down and select the signature in the option "use on reply/forward emails". I want to know if there is a way to update both signatures for "new emails" and "on reply/forward" automatically with gmail api. – adolfpc Apr 18 '21 at 23:09
  • Any solution to this problem? I think we have the same problem. – Lee Aug 09 '21 at 11:38

0 Answers0