Sending email via GmailApp as HTML to retain signature & image - javascript

I have a project where i need to bulk send an email with text specific to each user, and with the signature and image from the associated gmail account. I've got this atm:
//get template/draft email & body
const scGmailTemplate = GmailApp.getDraft("r4329894329375089160");
const scGmailTemplateMessage = scGmailTemplate.getMessage();
const scGmailTemplateBody = scGmailTemplateMessage.getBody();
//create new gmail
let scGmailContactMessageBody = scGmailTemplateBody;
//create array for gmail find & replaces [find, replace]
const gmailFindReplace = [["INV_START", scVars.INV_START],
["INV_END", scVars.INV_END],
["DEM_DATE", scVars.DEM_DATE]
];
gmailFindReplace.forEach(x=>{scGmailContactMessageBody=scGmailContactMessageBody.replace(x[0], x[1])});
const scGmailSubject = "Service Charge Invoice ("+scVars.INV_START+"-"+scVars.INV_END+")";
let bodyHtml = HtmlService.createHtmlOutput(scGmailContactMessageBody);//didnt work
GmailApp.sendEmail("me#me.com", "test", scGmailContactMessageBody);
Issue is the resultant email is just the raw html with the image displayed at the bottom
I've tried adding {htmlBody: html} as an option but that throws an error html not defined.
Any help would be much appreciated

The htmlBody expects a string.
GmailApp.sendEmail("me#me.com", "test", scGmailContactMessageBody, {htmlBody: scGmailContactMessageBody});
No need for let bodyHtml = HtmlService.createHtmlOutput(scGmailContactMessageBody); as getBody() already returns an HTML string.

From the question
I've tried adding {htmlBody: html} as an option but that throws an error html not defined.
The error message is straight forward, the code doesn't include an statement declaring html
try replacing html by bodyHtml

Related

Creating a javascript search bar for website and having trouble linking to correct page based on user input

I am trying to take the user input from the search bar, translate it into a string and then open up the webpage i have created using the input they have given me.
Here is what i am looking to do.
if(userData){
icon.onclick = ()=>{
webLink = 'myPages/userData.html'
linkTag.setAttribute("href", webLink);
linkTag.click();
}
When i try to add in the variable userData, which is the user input from the search bar, doing it like this:
if(userData){
icon.onclick = ()=>{
webLink = 'myPages/' + userData + '.html'
linkTag.setAttribute("href", webLink);
linkTag.click();
}
This comes off as an error and says it cannot find the file to redirect to.
I am new to javascript and need to figure out how to pass the variable in and create the correct address to open up the new html file page.
I have already taken out the spaces and formatting from the user input, as well as tried to create a new variable containing the address but both did not work and i still got the same error saying there are quotations in the address so it cannot be found.

Javascript - Extract a link from inside an html code

I'm trying to extract the link inside href and store it in a variable. Please see my code snippet below. Note that the token id is expected to be different with every single run.
Thank you for your help.
const body = <html><head></head><body><p>This is SignUp Email with confirmation link</p><p></p></body></html>
const activation_link = ???
console.log(activation_link)
/*
The expected result to be printed on Console:
"http://www.company.com/ls/click?upn=tokenid-11111-22222-333333-444444-555555-xxxxxx"
*/
const body = `<html><head></head><body><p>This is SignUp Email with confirmation link</p><p></p></body></html>`
const matched_links = body.match(/(?<=")http.+(?=")/);
console.log(matched_links);

Protecting a URL with a Query Parameter when Using Google Apps Script

I'm having a really hard time sending an automated email (with Google Apps Script) that includes a URL that contains query parameter.
Expected Behavior
Google Apps Script (specifically, the Gmail service) sends an email, and part of the email body contains a URL with a query parameter. The URL will look something like this:
http://my.app/products?id=Bz9n7PJLg8hufTj11gMF
Observed Behavior
The Gmail service seems to be stripping out the = from my URL. So, the body of the email ends up looking like this:
...
http://my.app/products?idBz9n7PJLg8hufTj11gMF
...
Obviously, that link won't work.
I've checked other questions here on SO, and I've tried working with the base encoding tools from the GAS Utilities service, as well as working with the encodeURI() JavaScript method. No luck so far.
Email-sending Code
//////// GENERATING MESSAGE FROM ID ////////////
// Gets message from ID
var id = Gmail.Users.Drafts.get('me', 'r-1006091711303067868').message.id
var message = GmailApp.getMessageById(id)
var template = message.getRawContent()
// Replaces template variables with custom ones for the user using RegExes
let listingUrl = 'http://my.app/products?id=xyz'
let creatorEmail = 'hello#gmail.com'
let creatorUsername = 'Sam'
template = template.replace(/templates#my.app/g, creatorEmail)
template = template.replace(/firstName/g, creatorUsername)
//** Below is the string that gets modified and broken **//
template = template.replace(/listingUrl/g, listingUrl)
// Creates the new message
var message = Gmail.newMessage()
var encodedMsg = Utilities.base64EncodeWebSafe(template)
message.raw = encodedMsg
// Sends it
Gmail.Users.Messages.send(message, "me", Utilities.newBlob(template, "message/rfc822"))
Regex-based Solution
With the help of Tanaike and Rafa Guillermo, the solution that ended up working for me was to replace = with = by using a little .replace() like this:
listingUrl = listingUrl.replace(/=/, '=')

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.

Calling: GetRespondentEmail Specific

The following code is used to send an email to the respondent of a google form. The onsubmit call is from the spreadsheet that stores the responses.
I am not sure how to get the forms ID to use the solution given in several other places. Basically I just need to know how to call the getRespondentEmail(). I've tried calling it using FormApp.getActive() but I read that only works when you are working in the form it's self.
If you want to offer a solution that requires using the form ID, an explanation of how to get the ID would be great.
var GetUserEmail = getRespondentEmail().
var subject = "Skiver Production Report Successfully Submitted";
var textbody = "You have successfully submitted a Skiver Production Report. Do not reply to this email as it is not monitored.";
var message = "You have succesfully submitted a Skiver Production Report. Do not reply to this email as it is unmoinitored." ;
var cc = "techlab#worldwidefoam.com";
var sendername = "Quality Function";
GmailApp.sendEmail("bob#notArealemail.com", subject, textbody, {
cc: cc,
name: sendername,
htmlBody: message
});
To get the id of an element you can simply use the document.getElementById(arg) method. For example:
var myForm = document.getElementById("ID");
For simply calling getRespondentEmail you just place getRespondentEmail() in your code. Your first line is doing this correctly but the . at the end needs to be removed or replaced with a ;, which is likely causing your error:
var GetUserEmail = getRespondentEmail(); // replace . with ;
Lastly if you would like to use getRespondentEmail() anywhere make sure it is in a global scope. Meaning it's not defined in any functions or closures.

Categories