is there a way to create automatic mails for Birthdays per Sheets and Scripts? So far, i got it to trigger with a button but now i would love to automate it. birthdate, mail and name are always listed on sheets and should trigger when the date is today, no matter the year (dd.mm). I will post my code below:
function GeburtstagsMail(e) {
// Add a custom menu to the spreadsheet.
SpreadsheetApp.getUi() // Or DocumentApp, SlidesApp, or FormApp.
.createMenu('Geburtstag')
.addItem('Senden', 'sendEmail')
.addToUi();
sendEmail();
SpreadsheetApp.getUi() // Or DocumentApp, SlidesApp, or FormApp.
.createMenu('Benachrichtigung')
.addItem('Senden', 'Bmail')
.addToUi();
Bmail();
}
function sendEmail(){
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const ui = SpreadsheetApp.getUi();
var startRow = 2; // First row of data to process
//(starting row index, starting column index, # or rows, # of columns)
var dataRange = sheet.getRange(startRow, 1, sheet.getLastRow()-1,200);
// Fetch values for each row in the Range.
var data = dataRange.getValues();
//Logger.log(data);
var name;
var birthdate;
var emailAddress;
var today = new Date();
var rowNum=2;
for (var i in data) {
var row = data[i];
name = row[0];
birthdate = row[2];
emailAddress = row[3];
Logger.log(" i = "+i);
/*
Logger.log("today.getDay() "+today.getDate()+" today.getMonth() "+today.getMonth()+"n");
Logger.log("birthdate.getDay() "+birthdate.getDate()+" birthdate.getMonth() "+birthdate.getMonth()+"n");
*/
Logger.log("name "+name+" email "+emailAddress+" birthdate "+birthdate);
if((today.getDate()==birthdate.getDate()) && (today.getMonth()==birthdate.getMonth())){
Logger.log("name "+name+" email "+emailAddress+" birthdate "+birthdate);
var body = "Hallo "+name+",nn"+
"wir wünschen Dir alles Gute zum Geburtstag <3 <3 <3 <3nn"+
"Feier schön mit Deinen Liebsten und lasse es Dir mal gut gehen. ;-)nn"+
"<3 <3 <3nn"+
"Viele Grüße senden Dirn"+
var subject = 'Geburtstagsgruß';
if(emailAddress!=null){
MailApp.sendEmail(emailAddress, subject, body);
sheet.getRange(rowNum, 6,1,1).setValue("Gesendet am "+new Date());
}
}
rowNum++;
}
}
As mentioned on top, i tried it so far with buttons which worked fine. After trying to set triggers it always ends up “failing”
1