MailApp sendEmail htmlBody clickable dynamic link - google-apps-script

I wrote a script that creates a new Spreadsheet from one of the Sheets of the original Spreadsheet and emails the new Spreadsheet with the PDF version attached and has an html body with some text and the link to the new Spreadsheet. The script fires each morning because the Sheet has dynamically chaning data, so the link also changes with every trigger.
The problem is that the link in the html body appears as a text and I would like it to be clickable. I tried formatting it with with A HREF but that wouldn't seem to do the trick
var napijelentes = SpreadsheetApp.create(fajlnev);
var napijelentesfajlid = napijelentes.getId();
var napijelentesfajl = DriveApp.getFileById(napijelentesfajlid);
var napijelentesurl = napijelentes.getUrl();
MailApp.sendEmail({
to: ellenorzes,
subject: subject,
htmlBody: "To download the spreadsheet please follow the link below: <br>" + napijelentesurl + "<br><br>",
attachments: [napijelentesfajl]
});
This way the link appears in the email as plain text, with [napijelentesurl] in front of it and is still not clickable.
Any solutions to my problem?

Create a variable for the html string.
var h;
h = 'To download <br><a href = "' + napijelentesurl + '">' +
napijelentesurl + "</a><br><br>";
Use single quotes on the ends of string that need double quotes inside the string.

Related

Auto Fill a Google Doc Template from AppScript including text with hyperlinks

I have a template made up of placeholders, example {{NAME}} {{SURNMANE}} {{WEBSITE}} where, through AppScript I insert the text. In the case of {{WEBSITE}} I ​​would need to enter the name of the clickable site, therefore with a HYPERLINK inside it.
emailBody = emailBody.replace(/{{NAME}}/g, ""+name);
emailBody = emailBody.replace(/{{SURNAME}}/g,""+surname);
emailBody = emailBody.replace(/{{WEBSITE}}/g,""+website);
//"CompanyWebSite" , but I would like to add "hyperlink=www.companywebsite.com/home/..."
Thanks!

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

Sendmail inlineimages are also attachments but should not be

I am using the final part of this answer to to send an images from Drive as an inlineimage. https://stackoverflow.com/a/41292754/1045794
function sendPicsInline() {
var picture1 = DriveApp.getFileById('0BxDqyd_bUCmvN1E3N0dQOWgycFE'); //public with link
var picture2 = DriveApp.getFileById('0BxDqyd_bUCmvTFNjRkRXbXA2Tms'); //public with link
var inlineImages = {};
inlineImages[picture1.getId()] = picture1.getBlob();
inlineImages[picture2.getId()] = picture2.getBlob();
MailApp.sendEmail({
to: 'testa#example.com, testb#example.com',
subject: "This is a test",
body:"Test message",
htmlBody: 'Test message with pics inline <br>' +
'first:<br><img src="cid:' + picture1.getId() + '" /><br>' +
'second:<br><img src="cid:' + picture2.getId() + '" />',
inlineImages: inlineImages
});
}
This is functioning correctly, however it is also listing an attachment on the email - which is particularly frustrating on the android gmail client.
I have tried setting a null attachment option but this does not work. Not that when I use the example from the documentation, but change the URL to any other image, I get an attachment as well as the inlineimage. Using the YouTube logo from the linked documentation, I get only the inlineimage without an attachment.
I cannot understand why - in all instances either from drive or another URL, I am using a PNG file with no other changes.
I have images that are stored in Google Drive as DataURI's and I just sent one like this:
function sendEmailWithInlineImage() {
var file=DriveApp.getFileById('FileId');
GmailApp.sendEmail('recipient email', 'Inline Images', null, {htmlBody:Utilities.formatString('<h3>Inline Images</h3><img src="%s" />',file.getBlob().getDataAsString())});
}
It was received as an inline image with no attachments.
The first part of the file looks like this: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAMR0lEQVR42u2dbWwUxxnHTYLS0ISURrRRlCDxJkWp+qEKUtK3RHzoh5QPqSLh5mMVJVHVRiBZoY5UVcLxva8NBJOCg5EABRtjDtu8GEPqyqYFXNvY4Ffs89l39l1tn8/2nV8AGwPTnfU+x+Px7N6d70z23Bnpr73bt5ud3z7PMzO7N5OWJlJqpfb29uc9bvcfvF7vCZ/PVyWrRogvv89X3ef1Or29vZ/Scksq

Google Script e.values error

I have looked at other similar posts and got some help with setting up trigger correctly but still have an error.
I have tried to automate sending a pdf doc to be emailed in response to a form submission in google apps. I used a script shared on web to get started and have fixed some deprecated terms.
I created a document template with keyItems embedded as placeholders and I created a form to capture data. The spreadsheet associated with the form is capturing data as planned. I edited the script associated with the spreadsheet to hopefully do the work required on submission of the form. The trigger looks correctly set up to run on form submit from the spreadsheet. My question is about the error I get repeatedly regarding the e.values being undefined. I assumed that I would be labelling these as e.values[0] and onwards when I declared the variables - i.e. they would be based on their order in the array drawn from the spreadsheet fields. Sorry to be long winded. If i place dummy content in the variables it works and sends the pdf so the actual mechanism works ok but my e.values are not working.
Here is my script
// Certificate of Completion
// Get template from Google Docs and name it
var docTemplate = "1V_PqCBZ9vWmmIELp47X12iNtdwRCqHyOyey6VPL0o0Q";
var docName = "CertificateOfCompletion";
//When form submitted get info from form and set as variables
function onFormSubmit(e) {
var name = e.values[2];
var email = e.values[3];
var date = e.values[4];
//make temporary working copy of template doc for the submitter
var copyId = DriveApp.getFileById(docTemplate).makeCopy(docName+' for '+name).getId();
//open the temporary doc
var copyDoc = DocumentApp.openById(copyId);
// tell script to go into the doc
var copyBody = copyDoc.getBody();
//replace placeholders in the doc with data from a submitted form
copyBody.replaceText('keyFullName', name);
copyBody.replaceText('keyDate', date);
copyDoc.saveAndClose();
//create PDf
var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");
//send email to recipient
var subject = "Certificate";
var body = "Congratulations!" + name + " Here is your certificate of completion " ;
MailApp.sendEmail(email, subject, body, {htmlBody: body, attachments: pdf});
//delete temporary document
DriveApp.getFileById(copyId).setTrashed(true); }
Have you tried using named values instead.
Heres an example
var handling = Number(e.namedValues["Call Handling"]);
var security = Number(e.namedValues["Security"]);
var agentName = String(e.namedValues["Agent Name"]);
These are questions in a form I have. The bit after the e.namedValues["text here"]); , replace text here with whatever your column is called in form responses, this will be the question name.
I'm not sure why e.values[0] onwards isn't working for you as it works for me.
My script is in the spreadsheet, with an onFormSubmit trigger installed via the menu.
Also bear in mind, to test your script, you must sumbit a form, hitting the play button won't work as there is no event to pass to your function.

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.