Customize invitation email in Google Forms - google-apps-script

I want to custom the invitation email when I share a google form. I have looking up in Google Script documentation and I haven't found some method or class useful. How can I customize this invitation email?.
Normally this email looks like:
I want to add a corporative image in the footer and maybe some text.

There isn't a method to create your own customized message. Below I am going to propose an alternate solution.
You can create a function that sends a customized mail. Just create an HTML file with your customized HTML content.
// i called this share.html in the apps script project
<h1>This is a header 1</h1>
<p>This is a paragraph</p>
<img src="<<SOME IMAGE SRC HERE>>" />
Then back in your code.gs file create a function like below:
function shareForm() {
var html = HtmlService.createHtmlOutputFromFile('share').getContent();
var recipient = '<<EMAIL ADDRESS>>';
var subject = 'this is the subject';
var body = 'this will get overridden by the HTML service';
var options = {
htmlBody: html
};
MailApp.sendEmail(recipient, subject, body, options)
}
You can run this function from the script editor, or you can build a button to add into the add-ons drop down options.

Related

Disable/Remove Signature from draft created with Apps Script / Google Card Service

I'm using Google Card Service to create a button, that when clicked should create a new Gmail draft; the draft should open for editing with a pre-populated body, and without a signature.
This is my code:
function onHomepage(e) {
var builder = CardService.newCardBuilder();
var section = CardService.newCardSection()
.addWidget(CardService.newTextButton()
.setText('Button')
.setComposeAction(CardService.newAction().setFunctionName('doStuff'),
CardService.ComposedEmailType.STANDALONE_DRAFT));
builder.addSection(section);
return builder.build();
}
function doStuff(e){
var body = 'This is a <b>test</b>'
var draft = GmailApp.createDraft('','Subject','',{htmlBody: body});
return CardService.newComposeActionResponseBuilder()
.setGmailDraft(draft)
.build();
}
The draft is created and opened for editing, the only issue is that the signature of the user is appended after the html body. I've seen other questions about adding a signature to the draft; I however need to remove it.
Unfortunately this isn't currently possible.
I would suggest filing a feature request on Google's Issue Tracker detailing the behaviour you would like to see, I would file it either under the Apps Script component or the Workspace Add-ons component.

Adding a button to an email

I have a Google Apps Script that is contained in a Sheet (which is tied to a Form). When a person fills in the Form, it triggers a series of events within the Sheet. The details of that aren't relevant (I don't think).
I would like to create a script that sends a follow-up email to everyone who filled out the form with a "Yes" or "No" question. To keep it simple, I would like to have two buttons in the email. When they click the button, it logs their response onto a cell in the Sheet.
I am okay with the apps script (.gs) coding, but I'm not very good with html. I can put together the email (shown below) and send it to the recipient, but I don't know how to get the response back from their click. Right now I have the 'myFunction()' script tied to onclick and I have that script ready to go, I just don't know how to get the 'myFunction()' to actually trigger when they click the button in the email.
If you have any solutions for getting the responses from the email, I'd appreciate the help.
Here is the email file (checkIN.html):
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<style>
button {
color: white;
width: 250 px;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
}
</style>
</head>
<body>
<p style="font-size:150%;">Good afternoon,<br><br>
Yesterday you assigned <?= info.name ?> to academic recovery for <?= info.subject?>.<br>
Did the student complete the assignment?</p>
<button style = "background-color:#4CAF50;" id='yesButton' onclick='myFunction(True)'> Yes </button>
<button style = "background-color:#f44336;" id='noButton' onclick='myFunction(False)'> No </button>
</body>
</html>
Here are the scripts that send the email and the function ready for a response:
function emailTest() {
var infoDict =
{
name: "Teacher",
subject: "Computer Science",
};
sendEmail(infoDict);
}
function sendEmail(info) {
var templ = HtmlService.createTemplateFromFile('checkIn.html');
templ.info = info;
var ssMessage = templ.evaluate().getContent();
MailApp.sendEmail({
to: "somebody#something.com",
subject: "Academic Recovery",
htmlBody: ssMessage,
noReply: true
});
}
function myFunction(response) {
var ss = SpreadsheetApp.openById("ID goes here");
var tracking = ss.getSheetByName('Tracking');
if (response) {
tracking.getRange("I12").setValue("Yes!");
}
else {
tracking.getRange("I12").setValue("No");
}
}
You can publish the app as web app (accessible to anyone with link) and change the buttons to <a> tags linked to the web app with the query string
The doGet function can accept query parameters. You can embed the row number or other identification method in the URL through some encryption and then decrypt in the doGet. So you will have to rename myFunction to doGet and
e.parameter should give you the query params in doGet.
Workflow: Link generated e.g. http://appurl?complete=yes&row=20 for email.
User clicks the link and goes to the App URL, script gets both params values and updates the range in the row.
The first thing regarding sending buttons on email is that each email client has it's own limitations and that most modern email-services do not allow certain type of content including JavaScript, so it's not possible to use the following type of buttons on the email HTML body:
<button style = "background-color:#4CAF50;" id='yesButton' onclick='myFunction(True)'>
If you fill confortable with Google Apps Script server-side code (.gs) but not with HTML you might be more confortable by sending a Google Form by email with an on form submit trigger to pass the collected response to the corresponding place in your spreadsheet.
Another option, if the email recipients are using Gmail you might use one-click actions. Also you might opt to embed an html form, or make the buttons to open a link on the email HTML body.
Related (from oldest to newest)
How to Embed a Google Form in Email in Google App Script
Embed Google Form in Gmail using Apps Script
How to access or call "Send this form to others"?

How to have Google Forms send automated email with new responses

I have a Google Form which I would like to automatically email someone when a new response is submitted. So far, I have just a simple HTML page with text in the body, however I would like the email content to include the form data as well.
Currently, this is what I have written:
function sendEmail(e) {
//response
var html = HtmlService.createTemplateFromFile("email.html");
var htmlText = html.evaluate().getContent();
var emailTo = "jeffreyabr#gmail.com"
var subject = "New SAP Role Request"
var textBody = "This email requires HTML support. Please make sure you open it with an email client that supports HTML"
var options = {htmlBody: htmlText};
GmailApp.sendEmail(emailTo, subject, textBody, options);
This came from following this basic YouTube tutorial.
Is there more Google Apps Script that I can add to accomplish this? Can I do this from Forms or must I do it from within Sheets?
The e.response object also contains the form data, which can be accessed by using e.response.getItemResponses().
Then to get the question, use getItem().getTitle(). To get the answer, use getResponse().
If you do not need the HTML response, then you can append the questions and answers to the textBody to display them on the email. Otherwise, you would have to add a script in your email.html using HTML scripts or google.script.run.
References:
Event Objects | onFormSubmit(e)
Class FormResponse

Copy Google Doc template and use as body for email

I have quite a long email template I want to automatically send to welcome new users.
The script copies the body of a Google Doc and uses that as the body of the email using MailApp.sendEmail.
The issue is that the email that arrives is very narrow and doesn't copy exactly what is in the template. Is there any way of formatting this to make it the same as the Google Doc template?
Any help much appreciated
var doc = DocumentApp.openByUrl("https://docs.google.com/document/d/"doc id"/edit");
var body = doc.getBody().getText();
var message = body;
var subject = "subject line";
MailApp.sendEmail (user.primaryEmail, subject, message)
I learned something new with this:
When you send an email as a plain text email it won't let you control where the line breaks are
It actually adds line breaks when you call the .getText() method, as well as part of the .sendEmail method when you are sending it just as plain text.
The easiest solution is to send it as an HTML message. I experimented with this and have what I believe the easiest solution for it below:
//this will replace the line breaks with html line breaks
var htmlBody = doc.getBody().getText().replace(/\n/g,'<br/>');
var message =
{
to: user.primaryEmail,
subject: 'subject line',
htmlBody: htmlBody
}
MailApp.sendEmail ({message})
I've tested this and this should fix your problem.

Google Apps Script - inline images example doesn't work

I'm trying to get this example script working from this Google Developer page.
I put the below code, which is copy/pasted verbatim (except for the "recipient#example.com" of course) into a function which runs when a form is submitted.
// This code fetches the Google and YouTube logos, inlines them in an email
// and sends the email
function inlineImage() {
var googleLogoUrl = "http://www.google.com/intl/en_com/images/srpr/logo3w.png";
var youtubeLogoUrl = "https://developers.google.com/youtube/images/YouTube_logo_standard_white.png";
var googleLogoBlob = UrlFetchApp
.fetch(googleLogoUrl)
.getBlob()
.setName("googleLogoBlob");
var youtubeLogoBlob = UrlFetchApp
.fetch(youtubeLogoUrl)
.getBlob()
.setName("youtubeLogoBlob");
MailApp.sendEmail({
to: "recipient#example.com",
subject: "Logos",
htmlBody: "inline Google Logo<img src='cid:googleLogo'> images! <br>" +
"inline YouTube Logo <img src='cid:youtubeLogo'>",
inlineImages:
{
googleLogo: googleLogoBlob,
youtubeLogo: youtubeLogoBlob
}
});
}
The email is not sent at all and the code doesn't appear to execute.
When I remove the first 4 statements (setting up the URLs and blobs), and the inlineImages section of the sendEmail function, the email is sent.
What am I doing wrong?
I have no problem with your code.