<?php
// basic email settings
require_once "Mail.php";
require_once "Mail/mime.php";
$params = array(
'host' => "ssl://smtp.gmail.com",
'port' => "465",
'auth' => true,
'username' => "something@gmail.com", // CONFIG your username comes here
'password' => "Your Password", // CONFIG and your password
);
$from = "something@gmail.com"; // CONFIG your email address
$cc = "something@gmail.com"; // CONFIG the cc address -- put your address so that you can confirm that the email has been sent successfully
$subject = "[CS1010] PE Mark";
$body = // CONFIG: the message you want to pass to your student, it's in the HTML format
"<p>Please Check the attachment(the word file) for your PE1 Mark. I didn't mark your PE but the graders are following the same scheme. If you have any problem with it, please email me.</p></br><p>Regards, </p> <p>Your Name</p>";
// Generate the location array
$markLoc = "../PE_DG4"; // CONFIG: the file location of the folder storing ex1, ex2, ...
$regEx = "/^ex\\d$/";
$regMatrix = "/^[au]\\d+$/";
$regWord = "/\\.docx$/";
$regCProg = "/\\.c$/";
// location str:
$loc = array();
$fileNameArr = array($markLoc);
foreach(scandir($markLoc) as $exName){
if(!preg_match($regEx,$exName)) continue;
array_push($fileNameArr,$exName);
foreach(scandir(implode("/",$fileNameArr)) as $matrixNum){
if(!preg_match($regMatrix,$matrixNum)) continue;
if(!array_key_exists($matrixNum,$loc)) $loc[$matrixNum] = array();
array_push($fileNameArr,$matrixNum);
foreach(scandir(implode("/",$fileNameArr)) as $file){
if(preg_match($regWord,$file)){
array_push($fileNameArr,$file);
$loc[$matrixNum][] = implode("/",$fileNameArr);
array_pop($fileNameArr);
} else if(preg_match($regCProg,$file)){
array_push($fileNameArr,$file);
$loc[$matrixNum][] = implode("/",$fileNameArr);
array_pop($fileNameArr);
}
}
array_pop($fileNameArr);
}
array_pop($fileNameArr);
}
// then send out emails to each specific student
foreach($loc as $matrix => $files){
$message = new Mail_mime();
$message->setHTMLBody($body);
// add in attachment here
foreach($files as $file){
$message->addAttachment($file);
}
$to = $matrix."@nus.edu.sg";
$headers = array(
'From' => $from,
'Reply-to' => $from,
'To' => $to,
'Cc' => $cc,
'Subject' => $subject,
);
$msgContent = $message->get();
$msgHeader = $message->headers($headers);
$smtp = Mail::factory('smtp',$params);
$mail = $smtp->send($to.",".$cc,$msgHeader,$msgContent);
if(PEAR::isError($mail)){
echo "Error: ".$mail->getMessage()."\n";
} else{
echo "Message Sent Successfully to: $to\n";
}
}
?>