source: moodle/trunk/fuentes/admin/mailout-debugger.php @ 136

Last change on this file since 136 was 136, checked in by mabarracus, 4 years ago

Ported code to xenial

File size: 2.2 KB
Line 
1#!/usr/bin/php
2<?php
3  /*
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.
7   *
8   * It is not Moodle specific - use it anywhere by setting the php
9   * "sendmail_path" setting to this file with a logfile parameter.
10   *
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
15   *
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
19   *
20   * - Mark as executable: chmod ugo+rx mailout-debugger.php
21   *
22   * - Run your admin/cron.php
23   *
24   * - Read /tmp/moodle-mailout.log
25   *
26   *
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.
29   */
30
31// Security check.
32if (!file_exists(dirname(__FILE__).'/mailout-debugger.enable')) {
33    mdie("Disabled.");
34}
35$tmpdir=sys_get_temp_dir(); // default
36
37if (isset($_SERVER['REMOTE_ADDR'])) {
38    mdie("should not be called from web server!");
39}
40
41if (isset($_ENV['TMPDIR']) && is_dir($_ENV['TMPDIR'])) {
42    $tmpdir = $_ENV['TMPDIR'];
43}
44
45$tmpfile = $tmpdir . '/moodle-mailout.log';
46$fh = fopen($tmpfile, 'a+', false)
47    or mdie("Error openning $tmpfile on append\n");
48fwrite($fh, "==== ".strftime("%a %b %e %H:%M:%S %Y", time())." ====\n");
49fwrite($fh, "==== Commandline: " . implode(' ',$argv) . "\n");
50
51$stdin = fopen('php://stdin', 'r');
52
53while ($line = fgets($stdin)) {
54    fwrite($fh, $line);
55}
56fwrite($fh, "\n");
57fclose($fh);
58fclose($stdin);
59
60/**
61 * Print an error to STDOUT and exit with a non-zero code. For commandline scripts.
62 * Default errorcode is 1.
63 *
64 * Very useful for perl-like error-handling:
65 *
66 * do_something() or mdie("Something went wrong");
67 *
68 * @param string  $msg       Error message
69 * @param integer $errorcode Error code to emit
70 *
71 */
72function mdie($msg='', $errorcode=1) {
73    trigger_error($msg);
74    exit($errorcode);
75}
76
77
Note: See TracBrowser for help on using the repository browser.