I've google docs template with logo and some images plus some text instructions, and I want to send it over email exactly how it appear in Google Docs, I know how to send plain text from google docs but can't figure out how to send templates with images.
Here is the code I am using currently :-
var body = doc.getBody().getText();
var message = body;
var subject = "subject line";
MailApp.sendEmail (user.primaryEmail, subject, message)
Updated script as suggested by Tanaike :-
function getDocAsHtml(docId){
var doc = DocumentApp.getActiveDocument()
var url = "https://docs.google.com/feeds/download/documents/export/Export?exportFormat=html&id=" + doc.getId();
var html = UrlFetchApp.fetch(url, { headers: { authorization: "Bearer " + ScriptApp.getOAuthToken() } }).getContentText();
var body = doc.getBody().getText();
var message = body;
var subject = "subject line";
MailApp.sendEmail('abc#xyz.com', subject, message, { htmlBody: html });
}
I believe your goal is as follows.
You want to send an email as the HTML body of the Google Document.
In this case, how about the following modification? From your showing script, I suppose that doc is the object of Document.
Modified script:
var url = "https://docs.google.com/feeds/download/documents/export/Export?exportFormat=html&id=" + doc.getId();
var html = UrlFetchApp.fetch(url, { headers: { authorization: "Bearer " + ScriptApp.getOAuthToken() } }).getContentText();
var body = doc.getBody().getText();
var message = body;
var subject = "subject line";
MailApp.sendEmail(user.primaryEmail, subject, message, { htmlBody: html });
// DriveApp.getFiles(); // This is used for automatically detecting the scope of Drive API.
When this script is run, the Google Document of doc is sent as the HTML body.
Note:
In this case, the mail client cannot show the HTML body, the text body of message is shown.
References:
fetch(url, params)
sendEmail(recipient, subject, body, options)
Related
I'm trying to output my custom google sheet to a set email address
I have tried adding into a blob before sending but every time the email is sent the excel sheet that is attached is blank. Any help would be appreciated
var file = DriveApp.getFilesByName(nSheet.getName());
var url = 'https://docs.google.com/spreadsheets/d/'+nSheet.getId()+'/export?format=xlsx';
var token = ScriptApp.getOAuthToken();
var response = UrlFetchApp.fetch(url, {
headers: {
'Authorization': 'Bearer ' + token
}
});
var fileName = (nSheet.getName()) + '.xlsx';
var blobs = [response.getBlob().setName(fileName)];
var emailAddress = "testg#gmail.com"
var subject = "PCS v5 Validated Sheet"
var emailbody = "The XLSX file is attached"
GmailApp.sendEmail(emailAddress, subject, emailbody, {attachments: blobs});
You script works fine. I tried it for simple sheet and I'm getting nice xlsx on my email.
https://docs.google.com/spreadsheets/d/1p8NzX0J1XhHFq6C1iSNWbs7h3OXzHKC7cDiQGYmuD_0/edit#gid=0
So, if your is blank it might be related to what data you have in sheet.
Are you using some =QUERY of =IMPORTXML function to get data which isn't diplayed?
Using the script below on a Google Doc, I'm trying to send the document as HTML in an email body. It's converting the document correctly (when I check the exported document via the url) and sending the email with the same content, but it loses the following formatting at some point: font format (e.g., size, color) and table format (e.g., borders, background color)
function sendGoogleDocAsHTML(){
var id = DocumentApp.getActiveDocument().getId() ;
var url = "https://docs.google.com/document/d/"+id+"/export?format=html"
var param = {
method : "get",
headers : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
muteHttpExceptions:true,
};
var html = UrlFetchApp.fetch(url, param);
var email = <EMAIL>;
var subject = <SUBJECT>;
GmailApp.sendEmail(email, subject,"", {htmlBody:html});
}
How do I preserve the format in the email?
This worked fine for me. Hope this may help
function sendGoogleDocAsHTML(){
var id = DocumentApp.getActiveDocument().getId() ;
var url = "https://docs.google.com/document/d/"+id+"/export?format=html"
var param = {
method : "get",
headers : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
muteHttpExceptions:true,
};
var html = UrlFetchApp.fetch(url, param);
var raw = Utilities.base64EncodeWebSafe("Subject: Test\r\n" +
"To: test#gmail.com\r\n" +
"Content-Type: text/html; charset=UTF-8\r\n\r\n" +
html+"\r\n\r\n");
var message = Gmail.newMessage();
message.raw = raw;
var sentMsg = Gmail.Users.Messages.send(message, 'me');
}
I’m trying to send a Google Doc, with all of its formatting, in an email.
function sendGoogleDocInEmail() {
var doc = DocumentApp.openById("example_Id");
var body = doc.getBody();
var text = body.getText();
GmailApp.sendEmail("emailaddress#gmail.com", "Hi", text);
This code works fine, but the email is sent as plain text.
I have descriptive hyperlink text pieces in the Google Doc, and they lose their hyperlinks when converted to plain text.
Is there any way I can keep all of the hypertext formatting when sending the email?
I’ve tried passing the body object to the method instead, but that just sends an email with DocumentBodySection in the body.
Thanks!
Trying using a combination of this script: https://stackoverflow.com/a/28503601/3520117
And then using the htmlBody parameter of the MailApp.sendEmail method.
Untested, but should work:
function emailGoogleDoc(){
var id = DocumentApp.getActiveDocument().getId() ;
var forDriveScope = DriveApp.getStorageUsed(); //needed to get Drive Scope requested
var url = "https://docs.google.com/feeds/download/documents/export/Export?id="+id+"&exportFormat=html";
var param = {
method : "get",
headers : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
muteHttpExceptions:true,
};
var html = UrlFetchApp.fetch(url,param).getContentText();
Logger.log(html);
var email = person#domain.tld;
var subject = 'Subject line';
var body = "To view this email, please enable html in your email client.";
MailApp.sendEmail(
email, // recipient
subject, // subject
body, { // body
htmlBody: html // advanced options
}
);
}
I am trying to send google doc content to email , but could not get it in html format, please help.
Mail is going through but it does not contain the font and colors.
Code Follows:
function getGoogleDocumentAsHTML(){
var id = DocumentApp.getActiveDocument().getId() ;
var forDriveScope = DriveApp.getStorageUsed(); //needed to get Drive Scope requested
var url = "https://docs.google.com/feeds/download/documents/export/Export?id="+id+"&exportFormat=html";
var param = {
method : "get",
headers : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
muteHttpExceptions:true,
};
var html = UrlFetchApp.fetch(url,param).getContentText();
var subject = "November 19 2015";
MailApp.sendEmail("xxxxxx#gmail.com", subject,"", {htmlBody:html, name:"Work Program", cc:"", replyTo:"xxxxx#gmail.com"});
}
Add this....
body { -webkit-print-color-adjust: exact; }
Reason for this solution is explained here
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 Drafts 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!
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
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;