5

I have created an apps script that will do a simple mail merge using contact details to create a new email draft. It works as expected, but I would like to use the current user's signature in the template.

Documentation on this is dated and incomplete. I created the code below from what I have found, but have had to make a total guess as to what it needs because I can't find the official documentation.

var params;
params = {method:"post",
          contentType: "application/json",
          headers: {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
          muteHttpExceptions:true
        };

var resp = UrlFetchApp.fetch("https://apps-apis.google.com/a/feeds/emailsettings/2.0/{domain}/me/signature", params);
var rCode = resp.getResponseCode();
var rText = resp.getContentText();

This is the response:

rCode = 400
rText = Invalid request URI

What is the correct request URI? Is there a new API for this?

Rubén
  • 34,714
  • 9
  • 70
  • 166
davids
  • 5,397
  • 12
  • 57
  • 94

1 Answers1

8

Gmail signatures are now accessible from the gmail API. I added a one liner below to get the signature of the current user. I used list instead of get because a user may send email as a different user by default then their logged in account. So I list all accounts and filter out the default one.

https://developers.google.com/gmail/api/v1/reference/users/settings/sendAs

var signature = Gmail.Users.Settings.SendAs.list("me").sendAs.filter(function(account){if(account.isDefault){return true}})[0].signature;
Spencer Easton
  • 5,642
  • 1
  • 16
  • 25
  • I figured there had to be a way to do it, but Google search didn't bring me to `settings/sendAs`. – davids Jan 18 '17 at 21:48
  • Yea its pretty new so most of the links out there probably still point to the depreciated api. – Spencer Easton Jan 19 '17 at 12:57
  • Do you know if the new API allows apps script to send a draft without copying the draft body into a new email before sending? I want to send a draft without creating a new thread. – davids Jan 23 '17 at 17:26
  • I do not know. Sorry – Spencer Easton Jan 23 '17 at 21:37
  • @SpencerEaston Thanks, this is working well for me! Any idea if there is a way to access this without requesting the settings.basic scope? It makes getting my app much harder to approve since it is restricted. You mentioned a deprecated API. Maybe that still works? I can't find it's documentation as I'm not sure which API you're referring to. – Luke Feb 11 '21 at 16:48
  • 1
    Perhaps you already know this but your link is not working any longer. – Cooper May 08 '21 at 07:08