Formatting the automated email send from google spreadsheet - google-apps-script

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);
...

Related

The output for "createHtmlOutputFromFile" is very literal and not utilizing the code I wrote

So I wrote a script that has an HTML file associated with it. The script is long so I won't post it, but at the end I have this:
// Send HTML content in email
var htmlBody = HtmlService.createHtmlOutputFromFile("AttendanceInfractionsHTMLTemplate").getContent();
MailApp.sendEmail({
to: Session.getActiveUser().getEmail(),
subject: 'EI Email',
htmlBody: htmlBody,
});
}
The problem is that when I receive the email in my inbox, the code in the HTML template hasn't run successfully and it shows the scriptlets in all their glory, such as my if/else statements and all that. This doesn't happen when I deploy a test of the HTML file. Any ideas? Can't seem to find anything about this in the Google Apps Script reference guide.
You need to use createTemplateFromFile() and evaluate() it:
var htmlBody = HtmlService.createTemplateFromFile("AttendanceInfractionsHTMLTemplate")
.evaluate()
.getContent()

Google Apps Script Gmail getPlainBody Line Breaks

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

Sending HTML emails using google ap script

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")

Embedded Google form in email?

I'm embedded Google form in email by Google apps script but the form doesn't submit correctly from Outlook and from iPhone. It works from Google Mail client only?
Is there's away to solve this problem of submission from iphone or outlook?
Thanks
Amany
To use forms embedded in emails, the email client needs to support html forms. Outlook has its own forms, but does not support the html form element (reference). I haven't found a reference for iPhone, but it's likely that it also limits the html support. (I use the gmail client on my phone... it supports forms.)
If you're using the code from Send form by email and track responses in spreadsheet, then you can extend your script to provide a doGet function for users that don't have HTML Forms support in their email clients.
The previous gist has been updated with these changes. (Actually, the gist already had the doGet() function.)
Code.gs
Extend this code by adding a doGet() function, which will host the exact same form that you're embedding in the email.
/**
* GET handler to provide alternate online form.
*/
function doGet() {
// Build survey body
var template = HtmlService.createTemplateFromFile('emailTemplate');
template.scriptUrl = ScriptApp.getService().getUrl();
template.serialNumber = getGUID(); // Generate serial number for this response
var app = template.evaluate();
return app;
}
Make the following modification to the sendSurvey() function:
var plainText = 'Please complete this survey online at: ' + scriptUrl;
html += '<p>Alternatively, you may complete this survey online.';
// Send email form
GmailApp.sendEmail(recipient, subject, plainText, {htmlBody:html} );
Now, recipients without HTML support in their email client will have a plain text message with an address they can paste into a browser, while HTML clients without <FORM> support will have a clickable link to the online form.

How to access or call "Send this form to others"?

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!