2

It is possible to mail the contents of a google document via apps script? I can see how to mail a link to the document, which works fine if the recipient has access.

It is possible to do this manually - checkmark the document - from the More.. button select Share... then Email as Attachment... - from the next screen select the attach as drop down and select Paste the item itself into the email.

However, I can't figure out how to do this through a script.

1 Answer 1

3

This is fairly straightforward and there are a few examples available on the internet but basically here is how to proceed to send it as a pdf attachment:

var docName = DocumentApp.openById(docID).getName();
var pdf = DocsList.getFileById(docID).getAs('application/pdf').getBytes();
var attach = {fileName: docName+".pdf",content:pdf, mimeType:'application/pdf'};
MailApp.sendEmail(emailadress, 'Your document as PDF ('+docName+')', 'see attachment', {attachments:[attach]});

I hope this example is clear enough.

EDIT : following your comment, I think this post by Henrique on the old Google group forum should fulfill your needs.( It's a workaround that I use a lot in many scripts and it works beautifully... the only annoying detail is that it needs to be authorized from the script editor (not the usual red border popup).

Here is how it goes :

function emailDocTest() {
  var id = 'Doc-Very-Long-ID-Here';
  var url = 'https://docs.google.com/feeds/';
  var doc = UrlFetchApp.fetch(url+'download/documents/Export?exportFormat=html&format=html&id='+id,
                              googleOAuth_('docs',url)).getContentText();
  var emailAdress = Session.getEffectiveUser().getEmail();
  MailApp.sendEmail(emailAdress, 'test doc send by mail as html', 'html only', {htmlBody:doc});
}

function googleOAuth_(name,scope) {
  var oAuthConfig = UrlFetchApp.addOAuthService(name);
  oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
  oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
  oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
  oAuthConfig.setConsumerKey('anonymous');
  oAuthConfig.setConsumerSecret('anonymous');
  return {oAuthServiceName:name, oAuthUseToken:"always"};
}
Sign up to request clarification or add additional context in comments.

5 Comments

Sorry, I wasn't clear enough in my original message. I want the document to be in the message body, not as an attachment. Although I am highly appreciative of the attachment code as I need that for something else. I almost had it, but was not formatting the attach value correctly.
Do you want it as plain text or as rich text / html ?
Thanks so much for sharing this. I've also gone to the issue asking for .getAs to be expanded to included more conversion types and starred it. While this UrlFetchApp call is returning formatted html, when I receive it in my email (I've tried a couple of different accounts that both accept html) all the formatting is gone. However, I definitely have enough to play around with now.
Strange... from my experience I get formatting correctly, including images, tables, different fonts etc...
This doesn't seem to work anymore because of changes to OAuth -- gsuite-developers.googleblog.com/2015/03/…

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.