PageRenderTime 28ms CodeModel.GetById 17ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

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