Send HTML email from google spreadsheet using a button - google-apps-script

I am new to stack overflow and am looking for some advice and some guidance on a google spreadsheet i have been working on.
A demo of it can be found at
https://docs.google.com/spreadsheets/d/1t9WfcG_1_mAavpN0l3v58JdD04Y1lL3PhAEoTUkq0Z4/edit?usp=sharing
Basically the jist of it is that i require the following.
A sendEmail button in the tool bar at the top that will send a html formatted email to any email on the active page in a specific colour.
Essentially this is to allow me to send reminder to clients in groups and by colour status. Currently i have to mail each client individually using a canned response which is pretty time consuming.
So far i am using the following script as a basis, i can successfully send an email but i run into errors if email fields are empty. I am looking for it to overlook the empty fields and continue searching for an email address. Also i can only get it to locate a template of an active sheet but would require it to locate a template of another sheet and use that. Finally when i try adding in HTML the script successfully picks up the template but sends it as plain text and its not sent as HTML.
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 100; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 100)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[8]; // Eight column
var message = row[15]; // Fifteenth column
var subject = "Welcome To Phorest";
MailApp.sendEmail(emailAddress, subject, message);
}
}
Looking forward to speaking with someone that can help me work all this out and save me some data entry in the process.
Appreciated.

You can send an HTML-containing email the following way:
var email = "some#email.com";
var subject = "Hello!";
var html = '<p> What is your favourite fruit? </p> <br>' +
'<select>' +
'<option value="apple">Apple</option>' +
'<option value="orange">Orange</option>' +
'</select> ';
MailApp.sendEmail({
to: email,
subject: subject,
htmlBody: html
});

Related

I'm trying to use paypal IPN with Google Sheets and Gmail to set up an automated email response when a buyer purchases a digital download but too hard

I've been successful getting the paypal ipn data to my google sheets using a variation of a script i found online.
function doPost(e) {
var rowData = [];
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet()
rowData.push(new Date(e.parameter.payment_date));
rowData.push(e.parameter.payment_type);
rowData.push(e.parameter.payment_status);
rowData.push(e.parameter.payer_email);
rowData.push(e.parameter.item_name1);
rowData.push(e.parameter.item_number1);
rowData.push(e.parameter.quantity);
rowData.push(e.parameter.first_name);
rowData.push(e.parameter.last_name);
rowData.push(e.parameter.address_street);
rowData.push(e.parameter.address_city);
rowData.push(e.parameter.address_state);
rowData.push(e.parameter.address_zip);
sheet.appendRow(rowData);
}
The above seems to work great and what the next step and I'm trying to do is take the data from a few of the cells in my google sheet where this script is working and use that in another script where an email with a link or attachment to the digital download images they purchased. I need to extract the first name, last name, payer email, and a variation of the itemname1 from above function and send the email automatically. I've set up the google sheet with the IPN information above and have added a last column 'P' where the link to the image will be located online using data from itemname1 as mentioned. The next step would be to use something like the function below using my data, but I'm having a super difficult time. I need to tweak the below function to work with my data. Is that possible? I'm not having any luck and it's all so close at this point. Anyone out there now how to do this? Can you point me in the right direction?
Something like this is what I was thinking would work for me, but I'm not sure how to tweak it to do the job I need:
// This constant is written in column C for rows for which an email
// has been sent successfully.
var EMAIL_SENT = 'EMAIL_SENT';
/**
* Sends non-duplicate emails with data from the current spreadsheet.
*/
function sendEmails2() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 2; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 3);
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[0]; // First column
var message = row[1]; // Second column
var emailSent = row[2]; // Third column
if (emailSent !== EMAIL_SENT) { // Prevents sending duplicates
var subject = 'Sending emails from a Spreadsheet';
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT);
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
}
My problem is that I haven't been able to put all the data together and make it work yet. Any help?

how do i send an email with info from a row with criteria from one of its column using google app script

I want to write a script that will send an email based on the criteria of a single column.
This is for an inventory management system that will send emails to remind that certain items are low in count.
eg.
if row 5's column J is = "reminder", send values of row 5's column B and C to my email. (col B and C is item description and quantity count respectively)
I just started learning about google app script so I'm not sure how to write it myself, but from other forum posts, this is what I got.
var values = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
values.forEach(function(row) {
var indent = row[10]; //column j
if (indent !== '') {
var EditedRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("store list").getRange(indent);
var Edited = EditedRange.getValue();
}
// Fetch the email address
var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("mail").getRange("B2");
var emailAddress = emailRange.getValue();
// Send Alert Email.
var message = Edited;
var subject = 'For Action: Indent' ;
MailApp.sendEmail(emailAddress, subject, message);
});
}
I split the code up and tested emailing and apparently the email portion of the codes work, but it is the retrieving of data that is not working and I don't really know how to get it working.
To get started, have a look at the Apps Script reference
There you will see that to retrieve the value of a single cell it is bets to use the method getValue() instead of getValues()
You need to specify the range (cell) wrom ehre you want to get the value
The easiest way to do so is with the method getRange() specifying A1 notation
A simple sample would be:
var sheet = SpreadsheetApp.getActiveSheet();
var checkValue = sheet.getRange("J5").getValue();
if (checkValue == "reminder"){
var B5Value = sheet.getRange("B5").getValue();
var C5Value = sheet.getRange("C5").getValue();
var message = B5Value + " and " + C5Value;
var emailAddress = "you need to define the email address of the recipient either in the code or a cell of the sheet";
var subject = "Define here your subject";
MailApp.sendEmail(emailAddress, subject, message);
}
Please make sure to dedicate enough time to study the Apps Script documentation to understand this code and be able to troubleshoot and perform simple modifications.

Sending automated mail from a shared mail Google Mail box

I have a work shared Gmail box and would like to automate a process where I can send out a number of e-mails. I can send automated e-mails from my work personal Gmail address using the following:
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 1; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 4)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[0]; // First column
var body_message = row[1]; // Second column
var subject = row[2];
var name = row[3];
//SLE adding attachment variable
var attachment = row[4]
//var message = "Dear " + row[3] + ",\n\n" + row[1]; // Assemble the body text
var message ="Dear "+name+",\n\n"+body_message
MailApp.sendEmail(emailAddress, subject, message);
}
}
But if there a way I can send the mail so it looks like its come from a shared box?
So instead of the mail hitting someone's mail box from StaceyC#company.com the mail would be from SharedAddress#company.com.
Unfortunately there is no way to send emails from a shared google mailbox with the MailApp.sendEmail() method, but it is possible with GmailApp.sendEmail().
https://developers.google.com/apps-script/reference/gmail/gmail-app#sendEmail(String,String,String,Object)
To do so, you can specify the Advanced parameter “from”, which allows you to send the email from any of your aliases, which you can retrieve with GmailApp.getAliases().
https://developers.google.com/apps-script/reference/gmail/gmail-app#getAliases()
So, provided that your shared g-mail box is one of your aliases, you can send the e-mail in this way:
GmailApp.sendEmail(emailAddress, subject, message, {from: 'your_alias'})
To add your Gmail Collaborative Inbox as an alias, you have to adjust your Google Groups Settings:
Go to Manage Group->Permissions->Posting permissions->Post
and allow (temporarily) anyone on the web to post to the group.
This is necessary to receive the verification message for adding your group inbox as an alias to the user account(s) from which you want to send emails on behalf of the group.
To add the group inbox as an alias, go in Gmail to Settings->Accounts->Send mail as->Add another email address. Add the email address of the group, you will receive a verification code in the group inbox. After this procedure you can send emails with GmailApp as explained above.

How to add html email code template to javascript to send personalized mails using googlesheet

Hello friends at SOF am new here i have an issue i believe can be sorted here. Am am using google sheet to send email. I have a working code that is sending personalized emails to individual using google sheet and it works fine. the issue is that i want to add an html email template to the message instead of just plan text. Here is a picture of my setup below which is working fine presently but dont know how to add my html email template code to it as the mail message body. I marked the message body in yellow i want to replace that message with my html email code template. I also place sample codes for you to copy and try out you only need create the google spreadsheet and put emails field at your end to test execution.
VIEW SHEET AND SCRIPT FILE HERE
VIEW HTML EMAIL CODE TEMPLATE HERE
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // Start at second row because the first row contains the data labels
var numRows = 3; // Put in here the number of rows you want to process
// Fetch the range of cells A2:B4
// Column A = Email Address, Column B = First Name
var dataRange = sheet.getRange(startRow, 1, numRows, 2)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[0]; // First column of selected data
var message = "Hey " + row[1] + ",\n how are you doing today?"; // Assemble the body text
var subject = "Sending emails from a Spreadsheet";
MailApp.sendEmail(emailAddress, subject, message);
}
}

Google Script to email form answers and questions to a user

I am trying to make a simple script that sends an email to a user who fills out a survey through a Google Form.
They will take the survey, answers are recorded in a spreadsheet, and this script will email them a copy of the questions (the headings), along with their answers (Should be the last row of the table.)
The email it is sent to was going to be the last column, which is not included in the email.
This was the script I was working on to get this working:
function sendFormByEmail(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
var emailSubject = "EMAIL-SUBJECT-HERE";
var yourEmail = "CURRENTLY-JUST-A-STATIC-EMAIL-ADDRESS"; //get this from a field.value then just cut off that field from being included in the email (just do column-1)...
var headers = headersRange.getValues()[0]; //google api to get the header values.
var lastRow = s.getLastRow();
var message += headers.toString();
message += (headersRange.getValue()[last]).toString(); //should get the last row of data?
MailApp.sendEmail(yourEmail, emailSubject, message);
}
Add a trigger that executes whenever a form is submitted and attach this function to your trigger.
In your code, you can use e.values[] in your code to refer to the values submitted by the user. This way you will not have to read the spreadsheet
function sendFormByEmail(e) {
var email = e.values[0]; // assuming 1st field is email
var firstName = e.values[1]; // assuming 2nd field is first name
...
}
You will probably find it hard to read the results of an email with all the questions in one line, and all answers in another.
It's easy to loop through the headers & form values at once, and just skip over the email address if you wish.
function sendFormByEmail(e) {
var emailSubject = "EMAIL-SUBJECT-HERE";
var emailQ = 1; // Column number with email address
var headers = SpreadsheetApp.getActiveSheet().getDataRange().getValues()[0]; //google api to get the header values.
var message = "Your survey responses:\n\n";
// Print Questions & Answers in pairs
for (i=0; i < headers.length; i++) {
if (i == emailQ) continue; // skip email address
message += headers[i]+"\n"
+ e.values[i]+"\n\n";
}
MailApp.sendEmail(e.values[emailQ], emailSubject, message);
}