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.
Related
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.
I'm setting up an email alert on my google sheet, in which if a value in one column goes below 2 I get an email alert on my mail.
I have mulitple domains and it is very tedious to check the domains expiry date every day. I created a sheet in which I put up domain name, registrar,hosted, todays date, expiry date, Expiry remaining days. Now I want to create an alert on "Expiry remaining days column", so that when the remaining days is below 2 days it send an alert email to my list of email addresses.
What I have already done is getting an email for one cell value but what I want to do is get it on whole column or number of column values.
function CheckSales() {
// Fetch the values
var domainexpireRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("domain list").getRange("F2");
var domainexpire = domainexpireRange.getValues();
// Check domain expiring
if ( domainexpire < 2){
var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Company emails").getRange("B2:B4");
var emailAddress = emailRange.getValues();
// Send Alert Email.
var message = '2019 smtp Domain Expiring in two days. Please check the sheet domain sheet '; // Second column
var subject = '2019 smtp Domain Expiring';
MailApp.sendEmail(emailAddress, subject, message);
}
}
I have a sheet which has multiple rows and columns, what I want to do is check column F and if any of the value on column F is less than 2 then send an alert email to my email addresses.
My code is working fine if I copy paste the code to multiple times and change the "getRange("F2");" to F2,F3,....F100 .
But it is not good way to put up a code. Can anyone tell me if I can edit some code and get the result.
I tried using "getRange("F2:F");" & getRange("F2:F54");but it doesn't worked out.
Thanks for your help.
Try using a for statement to loop through the whole sheet, the one below should work:
function checkSales() {
// Fetch the values
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName("domain list");
var dataRange = sheet.getDataRange();
var data = dataRange.getValues();
// loop through each row starting at row 2
for (var i = 1; i < data.length; i++) {
var domainExpire = data[i][5];
// Check domain expiring
if (domainExpire < 2) {
var emailRange = spreadsheet.getSheetByName("Company emails").getRange("B2:B4");
var emailAddress = emailRange.getValues();
// Send Alert Email.
var message = '2019 smtp Domain Expiring in two days. Please check the sheet domain sheet '; // Second column
var subject = '2019 smtp Domain Expiring';
MailApp.sendEmail(emailAddress, subject, message);
}
}
}
Try this:
function CheckSales() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('domain list');
var domainexpireRange=sh.getRange(1,6,sh.getLastRow()-1,1);
var domainexpire=domainexpireRange.getValues();
for(var i=0;i<domainexpire.length;i++) {
// Check domain expiring
if (domainexpire[i][5]<2){
var emailRange=ss.getSheetByName("Company emails").getRange("B2:B4");
var emailAddress=emailRange.getValues().map(function(r){return r[0];}).join(',');
// Send Alert Email.
var dateString=Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "yyyy");
var message=Utilities.formatString('%s smtp Domain Expiring in two days. Please check the sheet domain sheet.',dateString);
var subject=Utilities.formatString('%s smtp Domain Expiring',dateString);
MailApp.sendEmail(emailAddress, subject, message);
}
}
}
Background:
I am trying to create a google form that will be filled out by people, and then send a second form to be filled by someone else (couple). The form response needs to be anonymous, but the responses will also need to be linked.
My solution so far - using a unique ID for linking (kudos -> How to assign a unique ID to a google form input?) then using the email inputted to send off a message with a pre-made link to the next form with the ID and then delete the email address from the sheet.
The Question
The next sanitation which I am stuck on - is removing the email address form the Google Form response. One way would be to use deleteAllResponses() but that will also delete the basic data analysis pre-done by Google that I intend to use (to save time on creating some of the graphs).
I tried to find a way to access the response object and delete/modify the email field, but was unable to achieve that.
Would appreciate the help.
function myFunction() {
//delete the last email that was added
function deleteEmails(Row){
sheet.getRange('B'+Row + ':B'+Row).clearContent();
}
//delete all responses - would like to change this to only delete/modify the email field.
function deleteResponses() {
var form = FormApp.openById('The form ID');
form.deleteAllResponses();
}
//get the data on the sheet
var sheet = SpreadsheetApp.openById("The sheet ID").getSheetByName('Form Responses 1');
var StartRow = 1;
var LastRow = sheet.getLastRow();
var Range = sheet.getRange(LastRow, 2, 1, 62)
var AllValues = Range.getValues();
//if the person disagrees to fill the form - dont send an email
if (AllValues[0][1] != 'disagree') {
var SendTo = AllValues[0][0];
var body = "Thank you for answering pfa a link for the sedond form";
//send the actual email
MailApp.sendEmail({
to: SendTo,
cc: "",
subject: 'Thank you for answering',
htmlBody: body,
});
}
//delete emails and responses
deleteEmails(LastRow)
deleteResponses()
}
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);
}
}
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
});