Sending an email in HTML and plain with a Gmail Apps Script - javascript

I'm writing an auto-replying bot for gmail using Google Apps Script (http://script.google.com). Each time I use GmailThread's Reply to reply to message:
var htmlbody = "Hello<br>This is a <b>test</b>.<br>Bye.";
var body = "Hello,\nThis is a test.\nBye.";
thread.reply(body, {htmlBody: htmlbody, from: "Myself <hello#example.com>"});
I need to write the message both in plain text body and HTML in htmlbody.
Would there be a way to write an email only in HTML (to avoid writing every email content twice, plain and HTML!), and let reply() automatically send the email both in HTML and plaintext version?
I tried
var body = htmlbody.replace(/<br>/g,'\n').replace(/<b>/g,'');
// we should also replace </b> by '', etc.
but this is a bit of a hack. Is there a better version?

Google Scripts cannot generate the plain text portion automatically but you write a simple regex based replaces that removes all the tags from the HTML for plain text.
var body = htmlBody.replace(/<.+?>/g, "");
thread.reply(body, {htmlBody: htmlbody, from: "Myself <hello#example.com>"});
Also, if you set the body to blank, most modern email clients would still be able to render the image.

Related

Send Form with MailApp after fetching html. Embed Google Form into email. Generated email contains only text

I want to embed a Google form in an email and send it with MailApp. I'm attempting to use code found at: https://stackoverflow.com/a/23671529/4305236:
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,
});
...
The logs show all the html for the form.
However, when I run the code, the email just looks like text, with the links at bottom. When I "show original" in the email, though, all that html for the form seems to be there, like in the log...
Would greatly appreciate any help.
What generated form looks like, want this to be embedded in email:
What they are getting instead:
Answered here: https://stackoverflow.com/a/60749897/3383907.
Copy/pasting for history purposes:
When Google sends the form to folks directly and embeds the form in the email they are using their new offering called AMP. You can read more about it here:
https://developers.google.com/amp/
https://www.blog.google/products/g-suite/bringing-power-amp-gmail/
https://developers.google.com/gmail/ampemail/
The code you're using gets the raw HTML of the form as it would be rendered for a user in a browser. This code is not in AMP format. Ergo it will not do what you want.
If you want that AMP experience you will need to create the AMP email yourself.
I don't think FormApp has a programatic way to send the form to folks, like you can from https://docs.google.com/forms/.
I hope that helps.

Display emails before sending with Javascript

I am trying to create a script that will open the default email client, fill in certain variables but still allow the user to make changes and send. Right now my script will send the email (email.send) but I am looking for a way for the email to be displayed rather than sent. Here is the code:
var email = new Email()
email.subject = item.PO_ID
email.to = [item.BUYER_EMAIL, "accounting#test.com", "user#test.com"]
email.body = "Test body"
email.send()
What do I change .send() to?
Untested, but have you tried using using window.location() and sending the browser to something like:
mailto:accounting#test.com?subject=Test&body=Test Body
Replacing the email, subject and body using JS.

Embedding a link in an email using MailApp

I am working on a project to embed a link (that will activate a function in my script when clicked) into an E-mail using JavaScript in the Google Sheet script editor. I'm not sure if my code is just wrong or if Google just doesn't allow links. Here is my code:
function doStuff(){
var email = "email#emai.com"
var subject = "hi"
var message = "lets hope this works"
MailApp.sendEmail(email, subject, message)
}
MailApp.sendEmail(email, subject,Run JavaScript Code);
First of all there is a syntax error. I do not know what is MailApp but I doubt is gonna work as it is, you should add quotes to handle as a string like this:
MailApp.sendEmail(email, subject,"<a href='javascript:doStuff' onclick='doStuff();'>Run JavaScript Code</a>");
Anyway I agree with #Szymon DziewoƄski, it is very unlikely that an email client interprets javascript. Actually the same email provider could strip it from the body of the email. Take a look to this guide, you will notice that they do not mention javascript at all. Only HTML and CSS.

HTML Function displaying as plain text

I have been working on this for quite some time, and have basically been teaching myself HTML, so I apologize if the code is sloppy or if this is a simple fix. Here is what I am attempting to do, and the problem I am running into:
Take Google Form responses, generate an email based on those responses and dynamically email a certain person in my organization based on the location response(this part is done and working, just adding for context). Then create a survey response that sends info back to the original responder, sent from the administrator that the form was sent to. This is the js that I have running, that is working when it is ran in the google project:
function getid() {
var spreadsheet = SpreadsheetApp.openByUrl('https://docs.google.com/a/raytownschools.org/spreadsheets/d/1YWHu_yKn5bqq63x1A4e4-vBUtZANj-xjeF07IBpHP64/edit?usp=sharing');
SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
var sheet = spreadsheet.getActiveSheet();
var lastRow = sheet.getLastRow();
}
When I attempt to run that in my HTML code, and insert it into the element, it is simply inserting that code as raw text. HTML isn't running the function, or returning the data that it should be (and does return when ran outside the HTML code as a js app).
I can post the full HTML code if that would be helpful. Hopefully someone on here can help me out.
What you have there is a Javascript function. There aren't functions in HTML, HTML is a markup language.
You must add that function inside Javascript tags like this:
<script type="text/javascript">
function getid() {
var spreadsheet = SpreadsheetApp.openByUrl('https://docs.google.com/a/raytownschools.org/spreadsheets/d/1YWHu_yKn5bqq63x1A4e4-vBUtZANj-xjeF07IBpHP64/edit?usp=sharing');
SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
var sheet = spreadsheet.getActiveSheet();
var lastRow = sheet.getLastRow();
}
alert( getid() );
</script>
Take a look at here, on how to use javascript.
Edit
Seems like that code you're trying to execute is for Google Apps Script. I think you must execute it inside the Google script editor, because they don't make this API available for regular websites. Here is a running example with your code.

Sending html emails with JavaScript [duplicate]

This question already has answers here:
Is it possible to add an HTML link in the body of a MAILTO link [duplicate]
(7 answers)
Closed 10 years ago.
I'm creating a form (using only HTML and JavaScript) that sends emails. I want to insert a simple HTML table inside the body of the email. Something like this:
<table border=1>
<tr><td>blabla</td></tr>
</table>
After I click on the "Send" button a JavaScript function is called which sends the email via URL and the POST data appears in my email client - Outlook (which is okay for me) but there is a problem with the formatting. The data in body of the email is in plain text.
Is there any other way to do this only with JavaScript?
Is it necessary to use some kind of server-side scripts (like PHP or others) to format it properly?
You can use JavaScript mailto function. Try this, hope it'll help you
<div id="mailBody"><table border=1> <tr><td>blabla</td></tr> </table></div>
<input type="button" onclick="sendMail();">
Under the script tag
function sendMail()
{
var mailBody=document.getElementById('mailBody').innerHTML;
window.location="mailto:yourmail#domain.com?subject=hii&body="+mailBody;
}
If you want to use only the java script to send HTML email try like below...
But i have NOT Recommended this type of Mail sending....
Sending HTML Email by Javascript :
function SendEmail()
{
var outlookApp = new ActiveXObject("Outlook.Application");
var nameSpace = outlookApp.getNameSpace("MAPI");
mailFolder = nameSpace.getDefaultFolder(6);
mailItem = mailFolder.Items.add('IPM.Note.FormA');
mailItem.Subject = "Me";
mailItem.To = "me#me.com";
mailItem.HTMLBody = "<table border=1> <tr><td>blabla</td></tr> </table>";
mailItem.display(0);
}
if you want to just send a email as plain text by javascript then try like below...
Sending Plain Text by Javascript :
window.open('mailto:test#example.com?subject=subject&body=Testing Email');
Better you use a Server Side Coding to Send a email
What are the < characters are like in if you look at the source? if they are like < or < then you need to disable encoding on these. Also, you need to set formatting to HTML mail. Sorry I am not a PHP guy but I guess you will need server side code for that.

Categories