I'm trying to send HTML emails using Google aps script. I've got the HTML on a google doc which i'm trying to send using the code below. But when I send it I receive it as unformatted text with all the HTML tags displayed. Can anyone tell me how to do this? I'd rather not include the HTML in the script, because there will eventually be an awful lot of it.
var html = UrlFetchApp.fetch('https://docs.google.com/document/d/documentID/export?format=html');
MailApp.sendEmail('emailaddress', subject, null , {htmlBody: html});
Thanks, Bryan
Here's an example of doing the same thing with a row of spreadsheet data. Link. Don't use css between <style></style> tags. Use inline style="" instead. That seems to work better.
Google Script Mail App
The format of your code seems not well-structured,
Make sure all the data passed to the:
.sendEmail() method is in Curly braces {}.
Please reference the code below to see how it works.:
MailApp.sendEmail({
to: "me#myservice.com",
subject: "Testing",
htmlBody:"<h1>Hello there</h1><p>Your HTML Here</p>"});
That's it. You can also send this HTML from external file as:
HTMLService.createTemplateFromFile("fileName")
Related
With the Google Apps Script Gmail library, when I use the function GmailMessage.getPlainBody(), the API seems to take what used to be one paragraph and break it up into multiple, potentially by using a character limit. For instance, a paragraph of my email reads:
From the link you sent me, I gleaned that Gmail refers to their secure email as confidential.
But when I call this function on the email, it becomes:
From the link you sent me, I gleaned that Gmail refers to their
secure email as confidential.
And, when I split the email text on a new line delimitor and do a bit of cleanup to create an array with my output, I end up with:
['From the link you sent me, I gleaned that Gmail refers to their', 'secure email as confidential.']
I viewed this Reddit post, which seemed to deal with the similar problem. But, I tried the resolution suggested by the person who posed the question:
body = message.getPlainBody().replace(/\r\n\r\n/gm,'aaaLINEBREAKERaaa').replace(/\r\n/gm,' ').replace(/aaaLINEBREAKERaaa/gm, '\r\r').replace(/ /gm,' ')
And it didn't quite give me what I need. Has anyone else encountered this problem, and if so, do you have a suggested workaround? Thanks!
I had the same issue. In that case, I used a workaround.
When I checked the email, I noticed that the HTML body is included in the message body and the HTML body has the original paragraph, and I used this situation. So, in this workaround, the original text is retrieved from the HTML body and the HTML is converted to a text. By this, the original paragraph is obtained. The sample script is as follows.
Sample script:
This script uses Drive API for converting HTML to text. So pelase enable Drive API at Advanced Google services.
var message = // Here, please use your "message".
var html = message.getBody();
var id = Drive.Files.insert({title: "temp", mimeType: MimeType.GOOGLE_DOCS}, Utilities.newBlob(html, MimeType.HTML)).id;
var text = DocumentApp.openById(id).getBody().getText(); // or DocumentApp.openById(id).getBody().getText().trim();
DriveApp.getFileById(id).setTrashed(true);
console.log(text)
References:
getBody()
Files: insert
Just a quick question, is there anyway that I can get the body of my google doc to be as exact as it is? For example, I have a bold sentence in my google doc, but whenever i'm trying to send it using via sendEmail in google script, it's being sent as plaint text, the bold letters an other formatted texts are being converted into plain, please see my code:
var body_hw1 = doc_hw1.getBody().getText();
MailApp.sendEmail(email, subject_hw1, body_hw1);
The method getBody() returns an object that is not usable outside of Google Docs. The method getText() returns plain text.
In order for use bold fonts, etc, in an email, it has to be formatted as HTML. So you need to convert Google document to HTML. There is no built-in function for this, but a third-party solution is available: it does not support full range of Docs format but certainly supports bold and italic fonts, and similar. See this answer.
Another solution would be to extract the plain text and format it into html yourself and send that. For example:
var myText = {header:'Dear sir,',message:'We are writing to inform you...'}
var message = ("<h3>"+ myText.header +"</h3>" +
"<p>"+ myText.message +"</p>")
MailApp.sendEmail(email, 'Important Message', message);
I have been looking all over the web and could not find a way to format the email sent from a Google spreadsheet application. I have tried using inline html elements, but the API is escaping them and sending them as plain text in the email. Does anyone have an idea how to format the text?
Try using the following code:
...
var options = {
htmlBody: '<b><i>TEST</i></b>'
};
MailApp.sendEmail('someone#domain.ext', 'TEST', 'TEST', options);
...
I am recoding a friend's artist/client booking system. I have little experience in programming, enough understanding to hack my way through and google has been a great help!
I've spent the last 3 days reading and searching... Havn't found anything on that... Maybe I'not using the proper terms....
Here's what I want to do.
I use Google Script and google spreadsheet. 1 row contains all the information about a specific contract.
I want to be able to use a menu item to generate the contract (completed)
Open 2 browser window
one to display the Contract PDF file ( completed )
one for a GMAIL New email
with the contract pdf attached ( completed )
generated email body from selected row in the spreadsheet( completed )
be able to customize the generated email text before
sending it. Adding attachements...
This is what I'm stucked at. I use
MailApp.sendEmail('xxx#yyy.com', 'transfer email as pdf : body
& attachment', 'see attachment', {attachments:[body_to_send]});
It send's the email ok, with attachement and generated body and all... but no way to edit the email before sending...
Is that possible with google script?
Explore the UiApp class in Google Apps Script. UiApp
You could build a simple UI where the user can enter additional content and upload files, which can then be appended to the email before sending.
It doesn't look like Apps Script supports creating email drafts, which would also provide a way for you to edit emails before sending them. (Sending an email to yourself with a fancy way to reference the ultimate recipient is another possibility using the native Gmail UI.)
Is this in the direction you were thinking?
The last step of your process can be achieved using a Google Doc template in which you can add whatever you want to improve presentation.
From there you get an html version of your doc and use it as a html body in your mail.
If you need more details about html import you'll find it easily on this forum and on the issue tracker.
You could also run this code from a document bound script in a sidebar which would be visually more convenient. (see my recent posts about mailmerge).
I have a form attached to a Google Apps spreadsheet. It's a form to let my coworkers submit agenda items to our weekly review meeting. I'm writing a script to automatically email a reminder to the relevant people.
To make it less annoying & tedious for them, I'd like to actually embed the form within the email. Google Docs provides a way to manually send a form: Spreadsheet > Form > Send form. However, I can't find any way in the Google Apps Scripts documentation that lets me trigger this functionality, e.g.
A method like sendFormInEmail
Access to the email-friendly form HTML, which I could assign to the htmlBody argument of the sendEmail method.
Trigger an arbitrary menu item in Google Apps
Something else?
I could do a workaround by extracting the generated HTML from an email and assigning it as the htmlBody argument, but then I'd have to manually update the html every time we want to make a change to the form -- not what I want to happen.
Any suggestions?
Joris, you're right. You can use the method fetch() to send the form's html to the user's mailbox:
var form = FormApp.create('New Form');
....
var url = form.getPublishedUrl();
var response = UrlFetchApp.fetch(url);
var htmlBody = HtmlService.createHtmlOutput(response).getContent();
MailApp.sendEmail({
to: email,
subject: subject,
htmlBody: htmlBody,
});
...
I have the exact same requirement as you do, but unfortunately there doesn't seem to be an API call that does this.
What I think might work (though I have yet to actually try this) is to use the Spreadsheet.getFormUrl method to get the form URL, then use UrlFetchAp.fetch to obtain the HTML for the spreadsheet form, and then use that HTML as the e-mail body.
Like I said, I don't know if this will work (though on paper it should!), but I'd be very interested to know if it did!