Merge branch '41997-25' of git://github.com/samhemelryk/moodle into MOODLE_25_STABLE
[moodle.git] / admin / process_email.php
blob20ad4c1ec4daf4b4aaf2e2170ed792bb79680b34
1 #!/usr/bin/php -f
2 <?php
4 //error_reporting(0);
5 //ini_set('display_errors',0);
6 require_once(dirname(dirname(__FILE__)).'/config.php');
7 $tmp = explode('@',$_ENV['RECIPIENT']);
8 $address = $tmp[0];
10 // BOUNCE EMAILS TO NOREPLY
11 if ($_ENV['RECIPIENT'] == $CFG->noreplyaddress) {
12 $user = new stdClass();
13 $user->email = $_ENV['SENDER'];
15 if (!validate_email($user->email)) {
16 die();
19 $site = get_site();
20 $subject = get_string('noreplybouncesubject','moodle',format_string($site->fullname));
21 $body = get_string('noreplybouncemessage','moodle',format_string($site->fullname))."\n\n";
23 $fd = fopen('php://stdin','r');
24 if ($fd) {
25 while(!feof($fd)) {
26 $body .= fgets($fd);
28 fclose($fd);
31 $user->id = 0; // to prevent anything annoying happening
33 $from->firstname = null;
34 $from->lastname = null;
35 $from->email = '<>';
36 $from->maildisplay = true;
38 email_to_user($user,$from,$subject,$body);
39 die ();
41 /// ALL OTHER PROCESSING
42 // we need to split up the address
43 $prefix = substr($address,0,4);
44 $mod = substr($address,4,2);
45 $modargs = substr($address,6,-16);
46 $hash = substr($address,-16);
48 if (substr(md5($prefix.$mod.$modargs.$CFG->siteidentifier),0,16) != $hash) {
49 die("HASH DIDN'T MATCH!\n");
51 list(,$modid) = unpack('C',base64_decode($mod.'=='));
53 if ($modid == '0') { // special
54 $modname = 'moodle';
56 else {
57 $modname = $DB->get_field("modules", "name", array("id"=>$modid));
58 include_once('mod/'.$modname.'/lib.php');
60 $function = $modname.'_process_email';
62 if (!function_exists($function)) {
63 die();
65 $fd = fopen('php://stdin','r');
66 if (!$fd) {
67 exit();
70 while(!feof($fd)) {
71 $body .= fgets($fd);
74 $function($modargs,$body);
76 fclose($fd);