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!

4
  • 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. Commented Jul 23, 2015 at 15:50
  • @HDCerberus, I think a little bit of both. Commented Jul 23, 2015 at 16:05
  • @HDCerberus, when I've tried to implement it using 'signa = UrlFetchApp.fetch("apps-apis.google.com/a/feeds/emailsettings/2.0/merig.com/me/…);', I get the following error: 'Request failed for apps-apis.google.com/a/feeds/emailsettings/2.0/merig.com/me/… returned code 401. Truncated server response: <HTML> <HEAD> <TITLE>Authorization required</TITLE> </HEAD> <BODY BGCOLOR="#FFFFFF" TEXT="#000000"> <H1>Authorization required</H1> <H2>Error 401</... (use muteHttpExceptions option to examine full response) (line 56, file "")' Commented Jul 23, 2015 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: stackoverflow.com/questions/30135119/… Commented Jul 23, 2015 at 22:33

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.