As you can see I am using this code to send an email to whoever submit the form.
However, I do get the error that var emailTo = e.namedValues[2].toString(); has the error of
"TypeError: Cannot read property 'namedValues' of undefined (line 5,
file "Code")"
function sendEmail(e) {
var html = HtmlService.createTemplateFromFile("htmlemail.html")
var htmlText = html.evaluate().getContent();
var emailTo = e.namedValues[2].toString(); //column B is for the email
var subject = "Thanks for participation";
var textBody = "This email requires HTML support. Please make sure you open with a client that supports it."
var options = { htmlBody : htmlText };
if (emailTo!== undefined){
GmailApp.sendEmail(emailTo, subject, textBody, options)
}
}
Please help. Note that column B in the spread sheet is where the email should be filled.
Related
I am currently using google script to submit a google form that is then auto-populates to google doc, the document then is emailed to the recipient. I have the main structure working, but I am experiencing couple of issues, noted below:
Error 'Script function not found: atosGoogleDocForm' once the form has been submitted, I cannot understand what I am doing something wrong, but the document still get's populated and saved.
When the email has been sent, the email isn't coming from the sender of who's completed the google form, but the email is received from the email that had sent the form/created the google form (not the form filler). I don't know what I am missing here.
I would really appreciate your help.
const googleDocTemplete = DriveApp.getFileById('1-MLB3MSn0-hb11vGkRM29KEiHpAeJFybhsTGYnytSRA');
const destinationFolder = DriveApp.getFolderById('1Kdbi08wkL3gH1y8c8ghp34E8vxpaj6qQ');
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form responses');
const rows = sheet.getDataRange().getValues();
function atosGoogleDocForm(e) {
var timestamp = e.values[0];
var email = e.values[1];
var name = e.values[5];
var access_date = e.values[10];
var access_time = e.values[11];
var business_reason = e.values[12];
rows.forEach(function(row, index) {
if (index === 0) return;
if (row[15]) return;
const copy = googleDocTemplete.makeCopy(`${row[5]} Atos Form`, destinationFolder);
const doc = DocumentApp.openById(copy.getId());
var body = doc.getBody();
body.replaceText("{{Full Name}}", row[5]);
body.replaceText("{{Organisation}}", row[3]);
body.replaceText("{{DAS ID}}", row[4]);
body.replaceText("{{Emp Status}}", row[2]);
body.replaceText("{{Contact Address}}", row[6]);
body.replaceText("{{Contact Tel}}", row[7]);
body.replaceText("{{Atos Sponsor}}", row[8]);
body.replaceText("{{Sponsor DAS ID}}", row[9]);
body.replaceText("{{Access Date}}", access_date);
body.replaceText("{{Access Reason}}", business_reason);
body.replaceText("{{Access Time}}", access_time);
body.replaceText("{{timestamp}}", timestamp);
doc.saveAndClose();
const url = doc.getUrl();
sheet.getRange(index + 1, 16).setValue(url);
var recipient = "var5an1#hotmail.com";
var subject = name + " Atos PKI Form"
const html = '<p>Dear Reception, <br><br> Please find attached the application form for the ATOS PKI Card for ' + name + ' who will be attending to access the building on ' + access_date + ' at ' + access_time + '.' + "<br><br>Please let me know if you're experiencing any issues opening the file. The applicant will be collecting the PKI Card at Reception. <br><br>Kind regards<br><br></p>" + name + '<br><br><br>';
MailApp.sendEmail({
//from: email,
replyTo: email,
to: recipient,
cc: email,
subject: subject,
htmlBody: html,
attachments: [copy.getAs(MimeType.PDF)]
});
})
}
Can you please help me, How to send dynamic mail every latest entry in google sheet?
function CustomEmail() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A2:C4");
var UserData = range.getValues();
for (i in UserData) {
var row = UserData[i];
var name = row[0];
var email = row[1];
var score = row[2];
MailApp.sendEmail(row[1], "Custom mail", "Hello " + name + ", This is an email report of your score. Your score is " + score);
}
}
Setup a trigger in your project: Trigger for "form submission". Whenever someone submits the form, script will send out the email to those whom the email wasn't sent. Also have a helper column in the sheet to update status for the script to filter out the people who already received the email, else others will keep receiving their scores everytime there's a form submission.
This is a script to send emails to the contacts I saved in googlespreadsheets. However, I want it only to send it to the last added email (not all the list). It keeps sending it to everyone included in the list and I want only to send to the last added email in row[2] where are the emails registered.
Thanks.
function sendEmail(e) {
var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form Responses 1");
var data = ws.getRange("A2:D" +ws.getLastRow()).getValues();
data.forEach(function(row) {
var html = HtmlService.createTemplateFromFile("htmlemail.html")
var htmlText = html.evaluate().getContent();
var subject = "Thanks for participation";
var textBody = "This email requires HTML support. Please make sure you open with a client that supports it.";
var options = { htmlBody : htmlText };
GmailApp.sendEmail(row[2], subject, textBody, options);
});
}
This will only email the last row every time.
function sendEmail(e) {
var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form Responses 1");
var data = ws.getRange(2,1,ws.getLastRow()-1,4).getValues();
var html = HtmlService.createTemplateFromFile("htmlemail.html")
var htmlText = html.evaluate().getContent();
var subject = "Thanks for participation";
var textBody = "This email requires HTML support. Please make sure you open with a client that supports it.";
var options = { htmlBody : htmlText };
GmailApp.sendEmail(data[data.length-1][2], subject, textBody, options);
}
if you do it this way then it will only use that line once unless you edit it with additional emails.
function sendEmail(e) {
var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form Responses 1");
var data = ws.getRange(2,1,ws.getLastRow()-1,4).getValues();
var html = HtmlService.createTemplateFromFile("htmlemail.html")
var htmlText = html.evaluate().getContent();
var subject = "Thanks for participation";
var textBody = "This email requires HTML support. Please make sure you open with a client that supports it.";
var options = { htmlBody : htmlText };
if(data[data.length-1][2]) {
GmailApp.sendEmail(data[data.length-1][2], subject, textBody, options);
ws.getRange(data.length-1+2,3).setValue('');
}
}
I'm trying to add an image into the body of the emails. I've been able to get everything else working fine besides the image part. The images I'm trying to use are in the "M Column".
What do I need to add to get the value from the "M Column" and put it into the body of the emails?
-code is provided below
Thanks in advance. Everything I've tried has failed so I just removed everything I attempted and went to what I originally had before I decided to add images to the emails.
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;
var numRows = 15;
var dataRange = sheet.getRange(startRow, 1, numRows, 15);
var data = dataRange.getValues();
for (var i in data) {
var row = data[i];
var emailAddress = row[4];
var message = row[10];
var subject = "Subject Test";
MailApp.sendEmail(emailAddress, subject, message);
}
}
[error message][1]
[1]: https://i.stack.imgur.com/nwAJG.png
If you want to send an email which contains images, you could use one of the following options:
If your images are stored as urls in your Spreadsheet
The following code snippet is used to create the attachment containing the image gathered from a url stored in your Spreadsheet.
var imageUrl = sheet.getRange("YOUR_RANGE_FOR_THE_IMAGE").getValue();
var imageBody = "<img src='cid:myImage'>";
var imageBlob = UrlFetchApp.fetch(imageUrl)
.getBlob()
.setName("imageBlob");
var message = row[10];
var subject = "Subject Test";
MailApp.sendEmail({
to: emailAddress,
subject: subject,
htmlBody: message +"<br/><br/>"+ imageBody,
inlineImages:{
myImage: imageBlob
}
});
The above code snippet uses InlineImages in order to attach the image as an attachment into the email you want to send.
If your images are stored in drive and you have their file ids
The following code snippet is used to create the attachment containing the image gathered from the id of the file which is stored in Drive.
var imageId = sheet.getRange("YOUR_RANGE_FOR_THE_IMAGE").getValue();
var image = DriveApp.getFileById(imageId);
var imageBlob = Utilities.newBlob('', 'image/jpeg');
var message = row[10];
var subject = "Subject Test";
MailApp.sendEmail({
to: emailAddress,
subject: subject,
body: message,
attachments: [image.getAs(MimeType.JPEG), imageBlob]
});
If your images are stored inside a cell and/or as overgrid images
Unfortunately, Google Sheets doesn't have the capacity to return the blob of an image, only a reference to the image which means that you cannot retrieve the images and attach them afterwards in an e-mail using Google Apps Script.
In this case, an alternative would be to use the =IMAGE() function to insert the images in a cell and afterwards you can retrieve them by using the Sheets API V4.
Reference
MailApp Class Apps Script;
Sheets Class Apps Script getRange;
DrivApp Class Apps Script getFileById;
Apps Script Reference;
Sheets API.
I have a Google form which submits to a spreadsheet. I then have written this code which sends an email with the results. Can anyone tell me how I make the email be submitted by the person who submitted the form?
function Initialize() {
var triggers = ScriptApp.getScriptTriggers();
for (var i in triggers) {
ScriptApp.deleteTrigger(triggers[i]);
}
ScriptApp.newTrigger("SendConfirmationMail")
.forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
.onFormSubmit()
.create();
}
function SendConfirmationMail(e) {
try {
var ss, cc, subject, columns;
var message, value, textbody, sendername, sender;
// This is my email address and I will be in the CC
cc = e.namedValues["Username"].toString();
// This will show up as the sender's name
sendername = e.namedValues["Name"].toString();
subject = "Ticket Submitted";
message = "We have received your details.<br>Thanks!<br><br>";
ss = SpreadsheetApp.getActiveSheet();
columns = ss.getRange(1, 1, 1, ss.getLastColumn()).getValues()[0];
// This is the submitter's email address
sender = e.namedValues["Username"].toString();
// Only include form values that are not blank
for ( var keys in columns ) {
var key = columns[keys];
if ( e.namedValues[key] ) {
message += key + ' :: '+ e.namedValues[key] + "<br />";
}
}
textbody = message.replace("<br>", "\n");
GmailApp.sendEmail(sender, subject, textbody,
{cc: cc, name: sendername, htmlBody: message});
} catch (e) {
Logger.log(e.toString());
}
}
This is not possible, nor it should be. You want to impersonate someone else without their consent.
The archaic email system unfortunately allows that, therefore making easier the life of spammers and phishing scams. But Google and other Internet companies work hard to prevent and filter this kind of emails.
Bottom line, you will not be able to send an email as somebody else from a Google app unless it's an authorized account in your Gmail.
The best you can do is set the reply-to address.