免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1907 | 回复: 1

用PHP写了个校园里卖二手书和二手物品的小网站 [复制链接]

论坛徽章:
0
发表于 2012-03-06 10:50 |显示全部楼层
用PHP写了个校园里卖二手书和二手物品的小网站





我在做CS1010(Programming Methodology)的Tutor,其中一个任务是给学生作业判分,然后把判分的结果发给学生。



学生作业在我电脑里面folder的结构是:题号->学生学号->题目;其中题目为C code,有时候有其它的Tutor用Word判分之后会把结果发给我。



为了保护学生隐私,我需要分别给每个学生发送他的成绩,附上他的批改过的作业作为附件。于是,我可以用电脑来做这个重复而且麻烦的事情。



这里用的是PHP,以及它的Pear组件中的Pear_mail (用来发送email),和pear_mime(用来给email加入附件)。学生的email就是学生学号@学校网址。



code见下(这里居然不能用PHP后缀作为附件!应该是因为怕上传上去自动执行PHP吧~)。我觉得这是一次很好的Programming Practice——把一些编程技巧拿来解决实际问题。





Php代码
  1. <?php   
  2. // basic email settings   
  3. require_once "Mail.php";   
  4. require_once "Mail/mime.php";   
  5. $params = array(   
  6.     'host' => "ssl://smtp.gmail.com",   
  7.     'port' => "465",   
  8.     'auth' => true,   
  9.     'username'   => "something@gmail.com", // CONFIG your username comes here   
  10.     'password'   => "Your Password", // CONFIG and your password   
  11. );   
  12. $from = "something@gmail.com";     // CONFIG your email address   
  13. $cc = "something@gmail.com";    // CONFIG the cc address    -- put your address so that you can confirm that the email has been sent successfully   
  14. $subject = "[CS1010] PE Mark";   
  15. $body =                         // CONFIG: the message you want to pass to your student, it's in the HTML format   
  16.     "<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>";   
  17.   
  18. // Generate the location array   
  19. $markLoc = "../PE_DG4";     // CONFIG: the file location of the folder storing ex1, ex2, ...   
  20.   
  21. $regEx = "/^ex\\d$/";   
  22. $regMatrix = "/^[au]\\d+$/";   
  23. $regWord = "/\\.docx$/";   
  24. $regCProg = "/\\.c$/";   
  25.   
  26. // location str:   
  27. $loc = array();   
  28.   
  29. $fileNameArr = array($markLoc);   
  30. foreach(scandir($markLoc) as $exName){   
  31.     if(!preg_match($regEx,$exName)) continue;   
  32.     array_push($fileNameArr,$exName);   
  33.     foreach(scandir(implode("/",$fileNameArr)) as $matrixNum){   
  34.         if(!preg_match($regMatrix,$matrixNum))  continue;   
  35.         if(!array_key_exists($matrixNum,$loc))  $loc[$matrixNum] = array();   
  36.         array_push($fileNameArr,$matrixNum);   
  37.         foreach(scandir(implode("/",$fileNameArr)) as $file){   
  38.             if(preg_match($regWord,$file)){   
  39.                 array_push($fileNameArr,$file);   
  40.                 $loc[$matrixNum][] = implode("/",$fileNameArr);   
  41.                 array_pop($fileNameArr);   
  42.             } else if(preg_match($regCProg,$file)){   
  43.                 array_push($fileNameArr,$file);   
  44.                 $loc[$matrixNum][] = implode("/",$fileNameArr);   
  45.                 array_pop($fileNameArr);   
  46.             }   
  47.         }   
  48.         array_pop($fileNameArr);   
  49.     }   
  50.     array_pop($fileNameArr);   
  51. }   
  52.   
  53. // then send out emails to each specific student   
  54.   
  55. foreach($loc as $matrix => $files){   
  56.     $message = new Mail_mime();   
  57.     $message->setHTMLBody($body);   
  58.   
  59.     // add in attachment here   
  60.     foreach($files as $file){   
  61.         $message->addAttachment($file);   
  62.     }   
  63.     $to = $matrix."@nus.edu.sg";   
  64.       
  65.     $headers = array(   
  66.         'From'  => $from,   
  67.         'Reply-to' => $from,   
  68.         'To'    => $to,   
  69.         'Cc'    => $cc,   
  70.         'Subject'   => $subject,   
  71.     );   
  72.   
  73.     $msgContent = $message->get();   
  74.     $msgHeader = $message->headers($headers);   
  75.   
  76.     $smtp = Mail::factory('smtp',$params);   
  77.     $mail = $smtp->send($to.",".$cc,$msgHeader,$msgContent);   
  78.   
  79.     if(PEAR::isError($mail)){   
  80.         echo "Error: ".$mail->getMessage()."\n";   
  81.     } else{   
  82.         echo "Message Sent Successfully to: $to\n";   
  83.     }   
  84. }   
  85.   
  86. ?>  
复制代码

论坛徽章:
0
发表于 2012-03-11 22:24 |显示全部楼层
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP