PHP handles postfix mail content

Source: Internet
Author: User
Tags date empty explode header mail trim
 
 
  1. <?php
  2. 02
  3. #从输入读取到所有的邮件内容
  4. $email = "";
  5. $FD = fopen ("Php://stdin", "R");
  6. !feof ($FD)) {
  7. $email. = Fread ($FD, 1024);
  8. 08}
  9. Fclose ($FD);
  10. 10
  11. One #记录所有的内容, test
  12. File_put_contents ("/tmp/mail/". Time (), $email);
  13. 13
  14. #处理邮件
  15. $lines = explode ("\ n", $email);
  16. 16
  17. //Empty VARs
  18. $from = "";
  19. $date = "";
  20. $subject = "";
  21. $message = "";
  22. $splittingheaders = true;
  23. 23
  24. for ($i =0; $i <count ($lines); $i + +) {
  25. if ($splittingheaders) {
  26. 26
  27. /Look out for special headers
  28. if (Preg_match ("/^subject: (. *)/", $lines [$i], $matches)) {
  29. $subject = $matches [1];
  30. 30}
  31. if (Preg_match ("/^from: (. *)/", $lines [$i], $matches)) {
  32. if (Strpos ($lines [$i], "<")) {
  33. //the name exist too in from header
  34. $data = Explode (' < ', $lines [$i]);
  35. $from = substr (Trim ($data [1]), 0,-1);
  36. }else{
  37. Notoginseng//only The Mail
  38. $from = $matches [1];
  39. 39}
  40. 40}
  41. if (Preg_match ("/^date: (. *)/", $lines [$i], $matches)) {
  42. $date = $matches [1];
  43. 43}
  44. {} else {
  45. A header, but message
  46. $message. = $lines [$i]. " \ n ";
  47. 47}
  48. 48
  49. if (Trim ($lines [$i]) = = "") {
  50. Empty line, header section has ended
  51. Wuyi $splittingheaders = false;
  52. 52}
  53. 53}
  54. 54
  55. $when = Date ("y-m-d g:i:s");
  56. $data = explode (' @ ', $from);
  57. $username = $data [0];
  58. 58
  59. #记录到数据库
  60. $sql = "INSERT INTO mails" (' username ', ' from ', ' subject ', ' Date ', ' message ') VALUES (' $username ', ' $from ', ' $subject ') , ' $when ', ' $message ');
  61. 61
  62. #测试
  63. File_put_contents ("/tmp/mail2.log", $sql);
  64. ?>




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.