/apps/request/bin/downloadEmails.php

http://zoop.googlecode.com/ · PHP · 104 lines · 55 code · 16 blank · 33 comment · 7 complexity · 846e7b3cf9d397f944c7ec45270319a5 MD5 · raw file

  1. <?php
  2. include('config.php');
  3. include('includes.php');
  4. Zoop::loadLib('zend');
  5. $mail = new Zend_Mail_Storage_Pop3(array('host' => 'pop.gmail.com',
  6. 'user' => 'request_test@rickgigger.com',
  7. 'password' => 'requestx0ring',
  8. 'ssl' => 'SSL'));
  9. // $mail = new Zend_Mail_Storage_Imap(array('host' => 'imap.gmail.com',
  10. // 'user' => 'request_test@rickgigger.com',
  11. // 'password' => 'requestx0ring',
  12. // 'ssl' => 'SSL'));
  13. var_dump($mail);
  14. var_dump($count = $mail->countMessages());
  15. // for($i = 1; $i <= $count; $i++)
  16. foreach($mail as $message)
  17. {
  18. // $message = $mail->getMessage($i);
  19. // print_r($message);
  20. // echo "$i {$message->from} {$message->to} {$message->subject}\n";
  21. echo "{$message->from} {$message->to} {$message->subject}\n";
  22. $res = preg_match('/([\w ]+)<(\w+)@([\w.]+)>/' , $message->from, $matches);
  23. $name = trim($matches[1]);
  24. $parts = explode(' ', $name);
  25. $firstname = array_shift($parts);
  26. $lastname = array_pop($parts);
  27. $user = trim($matches[2]);
  28. $domain = trim($matches[3]);
  29. $username = $email = "$user@$domain";
  30. $sender = DbObject::_getOne('Person', array('username' => $username), array(
  31. 'firstname' => $firstname, 'lastname' => $lastname, 'email' => $email
  32. ));
  33. // print_r($sender);
  34. preg_match('/<([^>]+)>/' , trim($message->messageId), $matches);
  35. $messageId = $matches[1];
  36. if(SqlFetchCell("SELECT count(*) from request where message_id = :messageId", array('messageId' => $messageId)))
  37. continue;
  38. $request = new Request();
  39. $request->owner_id = $sender->id;
  40. $request->name = trim($message->subject);
  41. $request->message_id = $messageId;
  42. // deal with the headers
  43. // foreach ($message->getHeaders() as $name => $value)
  44. // {
  45. // if(is_string($value))
  46. // {
  47. // echo "$name: $value\n";
  48. // continue;
  49. // }
  50. // else
  51. // {
  52. // echo "$name: <complex value>\n";
  53. // }
  54. // // foreach ($value as $entry) {
  55. // // echo "$name: $entry\n";
  56. // // }
  57. // }
  58. if($message->isMultipart())
  59. {
  60. foreach($message as $part)
  61. {
  62. print_r($part->getContent());
  63. var_dump($part->contentType);
  64. $start = strpos($part->contentType, '/');
  65. var_dump($start);
  66. $end = strpos($part->contentType, ';');
  67. var_dump($end);
  68. var_dump($end - $start);
  69. $type = substr($part->contentType, $start + 1, $end - $start - 1);
  70. var_dump($type);
  71. if($type == 'text')
  72. $request->text_desc = $part->getContent();
  73. else if($type == 'html')
  74. $request->html_desc = $part->getContent();
  75. }
  76. // die();
  77. }
  78. else
  79. $request->text_desc = $message->getContent();
  80. $request->save();
  81. // break;
  82. }
  83. // echo_r(Config::get('app.allowedDomains'));
  84. // get the owner id
  85. // if the user doesn't exist create them with the email as the username
  86. // take the name from the subject line
  87. // take the description from the body
  88. // split it into text_desc and html_desc - display the html desc if it is there