Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

   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.
  32  if (!file_exists(__DIR__.'/mailout-debugger.enable')) {
  33      mdie("Disabled.");
  34  }
  35  $tmpdir=sys_get_temp_dir(); // default
  36  
  37  if (isset($_SERVER['REMOTE_ADDR'])) {
  38      mdie("should not be called from web server!");
  39  }
  40  
  41  if (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");
  48  fwrite($fh, "==== ".strftime("%a %b %e %H:%M:%S %Y", time())." ====\n");
  49  fwrite($fh, "==== Commandline: " . implode(' ',$argv) . "\n");
  50  
  51  $stdin = fopen('php://stdin', 'r');
  52  
  53  while ($line = fgets($stdin)) {
  54      fwrite($fh, $line);
  55  }
  56  fwrite($fh, "\n");
  57  fclose($fh);
  58  fclose($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   */
  72  function mdie($msg='', $errorcode=1) {
  73      trigger_error($msg);
  74      exit($errorcode);
  75  }
  76  
  77