4
* Utility to debug mailouts - will save the content of emails to a
5
* logfile instead of sending them out. Use it as a sendmail
6
* "stand-in" when testing mailouts.
8
* It is not Moodle specific - use it anywhere by setting the php
9
* "sendmail_path" setting to this file with a logfile parameter.
11
* - Set in php.ini (not settable in config.php):
12
* sendmail_path=/path-to-moodle/admin/mailout-debugger.php');
13
* Or from the commandline
14
* php -d sendmail_path='/path-to-moodle/admin/mailout-debugger.php' /path/to/cron.php
16
* - Create a file in admin called mailout-debugger.enable
17
* (this is a security check to prevent execution in prod environments)
18
* touch /path/to/moodle/admin/mailout-debugger.enable
20
* - Mark as executable: chmod ugo+rx mailout-debugger.php
22
* - Run your admin/cron.php
24
* - Read /tmp/moodle-mailout.log
27
* This script will create logfiles in /tmp/ or in $TMPDIR if set.
28
* On windows, use php -r 'print sys_get_temp_dir()' to see where the file is saved.
32
if (!file_exists(dirname(__FILE__).'/mailout-debugger.enable')) {
35
$tmpdir=sys_get_temp_dir(); // default
37
if (isset($_SERVER['REMOTE_ADDR'])) {
38
mdie("should not be called from web server!");
41
if (isset($_ENV['TMPDIR']) && is_dir($_ENV['TMPDIR'])) {
42
$tmpdir = $_ENV['TMPDIR'];
45
$tmpfile = $tmpdir . '/moodle-mailout.log';
46
$fh = fopen($tmpfile, 'a+', false)
47
or mdie("Error openning $tmpfile on append\n");
48
fwrite($fh, "==== ".strftime("%a %b %e %H:%M:%S %Y", time())." ====\n");
49
fwrite($fh, "==== Commandline: " . implode(' ',$argv) . "\n");
51
$stdin = fopen('php://stdin', 'r');
53
while ($line = fgets($stdin)) {
61
* Print an error to STDOUT and exit with a non-zero code. For commandline scripts.
62
* Default errorcode is 1.
64
* Very useful for perl-like error-handling:
66
* do_somethting() or mdie("Something went wrong");
68
* @param string $msg Error message
69
* @param integer $errorcode Error code to emit
72
function mdie($msg='', $errorcode=1) {