Skip to content

Instantly share code, notes, and snippets.

@Max-Makhrov
Created December 3, 2020 08:23
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Max-Makhrov/646f4fc78f8c78b58ccd6449544d4e5d to your computer and use it in GitHub Desktop.
Save Max-Makhrov/646f4fc78f8c78b58ccd6449544d4e5d to your computer and use it in GitHub Desktop.
Write emails to myself from Google Script functions
function someImportantFunction() {
// [ 1 ]. do some stuff
//
//
//
Logger.log('Success!');
// [ 2 ]. Report the execution
var recipient = 'makhrov.max@gmail.com'; // change!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//
// get function name: https://stackoverflow.com/a/1013304/5372400
var myFunctionName = arguments.callee.name;
var subject = 'Running function: ' + myFunctionName;
writeMailToAdmin(recipient, subject);
}
// writes email to you with some info about execution
function writeMailToAdmin(recipient, subject) {
// user
var user = Session.getActiveUser();
// time
var d = new Date();
var t = Utilities.formatDate(d, Session.getScriptTimeZone(), "yyyy-MM-dd HH:mm:ss");
// script url
var id = ScriptApp.getScriptId();
var url = 'https://script.google.com/d/' + id + '/edit';
// logs
var logs = Logger.getLog();
// msg
var msg = 'User: ' + user + ' runs the script:<br>' + url + '<br><br>Script time:<br>' + t + '<br><br>Logs:<br>' + logs;
// send a message
GmailApp.sendEmail(
recipient,
subject,
'body', {
htmlBody: msg,
}
);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment