1

I've created a script in google apps script which reads the contents of a google doc into a draft message in gmail. It doesn't, however, append the user's signature.

So my plan would be to retrieve the signature, and then append to the contents of the google doc, and then put into a draft message.

I see that there is information for retrieving a users gmail signature here: https://developers.google.com/admin-sdk/email-settings/#manage_signature_settings, but I am am having trouble trying to implement it in my existing script.

How should I proceed? (current script follows)

function doGet() {
createDraft()
return HtmlService.createHtmlOutput('<b>Your catering email template can now be found in your <a href="https://mail.google.com/mail/#drafts">Drafts</a> folder!</b>');
}

function createDraft() {

var forScope = GmailApp.getInboxUnreadCount(); // needed for auth scope

var doc = DocumentApp.openById('1fsRMxtLx3IBEYvmVy9W8uHLw3Uf2OIh4L7ZSxpkixbY');

var body = doc.getBody();

var mbody = body.getText();



var raw = 
  'Subject: Catering Proposal\r\n' + 
  'Content-Type: multipart/alternative; boundary=1234567890123456789012345678\r\n' + '\r\n' +
  mbody + '\r\n' +
  '--1234567890123456789012345678--\n';

var draftBody = Utilities.base64Encode(raw);

Logger.log(draftBody);

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

var resp = UrlFetchApp.fetch("https://www.googleapis.com/gmail/v1/users/me/drafts", params);
Logger.log(resp.getContentText());
}

I greatly appreciate any help that can be provided!

justbriman
  • 23
  • 1
  • 5
  • What trouble are you having specifically? Is it you don't know how to use the API to retrieve the signature or are you getting an error. – HDCerberus Jul 23 '15 at 15:50
  • @HDCerberus, I think a little bit of both. – justbriman Jul 23 '15 at 16:05
  • @HDCerberus, when I've tried to implement it using 'signa = UrlFetchApp.fetch("https://apps-apis.google.com/a/feeds/emailsettings/2.0/merig.com/me/signature");', I get the following error: 'Request failed for https://apps-apis.google.com/a/feeds/emailsettings/2.0/merig.com/me/signature returned code 401. Truncated server response: Authorization required

    Authorization required

    Error 401

    – justbriman Jul 23 '15 at 16:07
  • 401 indicates that you're not authorized to perform the action. I'm not that familiar with using these APIs myself, but it sounds like you've not got the correct permissions to use the API. this answer might be useful: http://stackoverflow.com/questions/30135119/how-to-use-the-google-email-settings-api-and-the-oauth2-for-apps-script-library – HDCerberus Jul 23 '15 at 22:33

2 Answers2

1

The user signature is handled by a separate API, not by the Gmail API. You need to add the scope for this first :

https://apps-apis.google.com/a/feeds/emailsettings/2.0/

and then use GET to retrieve the signature

domain =gmail.com, for example
user = my.user, or whatever
https://apps-apis.google.com/a/feeds/emailsettings/2.0/domain/user/signature
Baron Daniel
  • 43
  • 1
  • 1
  • 10
1

There is an easier way to do it now covered in this post:

Apps Script to get the users signature

Basically:

var signature = Gmail.Users.Settings.SendAs.list("me").sendAs.filter(function(account){if(account.isDefault){return true}})[0].signature;
Community
  • 1
  • 1
davids
  • 5,397
  • 12
  • 57
  • 94