[string] function getMessage() { $blocks = [ "Today is ".date('l, F jS, Y') ]; array_push( $blocks, makeMessageBlock( "Next 10 days", makeCalendarRangeCommand(10, ((date('n') == 12) ? '-M' : null)) ) ); if (date('j') === '1') { array_push( $blocks, makeMessageBlock( "This Month", makeCalendarMonthCommand('this', ((date('n') == 12) ? '-M' : null)) ), makeMessageBlock( "Next Month", makeCalendarMonthCommand('next', ((date('n') == 12) ? '-M' : null)) ) ); } return implode("\n\n", $blocks); } // makeCalendarRangeCommand :: (numeric, string?) -> string function makeCalendarRangeCommand($days, $sortOpts = null) { return ( "(".CALENDAR_PATH." -A {$days} ; ". CALEVENTS_PATH." -A {$days} -co) ". "| sort ".($sortOpts ?? '') ); } // makeCalendarMonthCommand :: (which, string?) -> string // which = (string) must be 'this' or 'next' function makeCalendarMonthCommand($which, $sortOpts) { $start = [ date("Ymd", strtotime("first day of {$which} month")), date("Y-m-d", strtotime("first day of {$which} month")) ]; $end = date("Y-m-d", (strtotime("last day of {$which} month"))); $days = date("d", (strtotime("last day of {$which} month") - 86400)); return ( "(".CALENDAR_PATH." -t {$start[0]} -A {$days} ; ". CALEVENTS_PATH." -ge {$start[1]} -lt {$end} -co) ". "| sort ".($sortOpts ?? '') ); } // makeMessageBlock :: (string, string) -> string function makeMessageBlock($header, $command) { $output = `{$command}`; if ($output) { return "[{$header}]\n{$output}"; } return ''; } // getHeaders :: void -> [string => string] function getHeaders() { return [ 'to' => EMAIL_RECEIVER, 'extra' => "From: ".EMAIL_SENDER."\n". "X-Mailer: PHP/".phpversion(), ]; } // sendMail :: (string, string, [string => string]) -> void function sendMail($subject, $message, $headers) { mail( $headers['to'], $subject, $message, $headers['extra'] ); } sendMail( EMAIL_SUBJECT, getMessage(), getHeaders() );