4
votes

I'm trying to use Apps script to embed a prefilled Form in an email. The emails will only be sent to Gmail users. I want something that works the same as pressing the 'include form in email' option when manually emailing a form.

I've successfully created the prefilledUrl and used UrlFetchApp and HtmlService to get the Html from the form and send email.

But when it arrives it looks like the screenshot below and user can not submit. Screenshot of form embedded in email

A stripped down version of the code I am using is the following:

var prefilledForm = 'prefilledForm link here');
Logger.log(prefilledForm);

var subject = 'Message from Office';
var response = UrlFetchApp.fetch(prefilledForm);
var htmlBody = HtmlService.createHtmlOutput(response).getContent();

var body = 'Dear '+mName+',\n\n'+mMessage+'\nPlease acknowledge by submitting the form below.\nRegards,\nSchool Office';

GmailApp.sendEmail(eEmail,subject,body,{
  noReply:true,
  htmlBody: htmlBody
});

Is there anyway to embed the form in an email just like it does when you select 'include form in email' like the screenshot below?

enter image description here

1
Apologies, I just noted that the two screenshots show different forms. But the point still stands that the one sent by Google is formatted better than the first oneBJG87

1 Answers

4
votes
  1. Send the form to yourself with the form included in the email.
  2. Open Gmail in the browser, press F12. Find the div of the form in the email body. enter image description here
  3. Paste the div code to the body of the Apps Script HTML file. Or paste it to email HTML body in your script, I didn't test it. enter image description here
  4. Send the email with the above HTML file, you should be able to get a similar result as below. enter image description here