GTD-PHP Import from POP3 Mail
by dwarfsoft on Aug.04, 2009, under Home, Scripting, Tweet
I have been attempting to get a fully working system set up for David Allens Getting Things Done. To this end I have set up an install of GTD-PHP.
Being the kind of person who likes to adapt, change, and implement better solutions, particularly for myself, I created a list of features I would like to see in my GTD-PHP installation. One of the first problems I had with the existing system was that it does not support importing Inbox items from a real mail Inbox.
I did some research and came across an existing mail importer, however this required directly piping mail from SMTP into the php script, which I could not do on my host (or would prefer not to do).
I decided to build my own POP3 Importer of Inbox items and decided to learn PHP and imap_open. I built it as an addon to GTD-PHP that could be called to do an import, however I am still going to extend it so that in the GTD-PHP header additional headers can be called (ones that identify if new items have been added to an Inbox so I can get feedback of when somebody mails into that account).
<?php //Script to insert e-mails into GTD-PHP inbox. //These are loaded from a pop3 account //The account info and other options are configured in the config.php file //$config['addons']['pop3inbox']=array( // "link"=>"addons/pop3inbox/import.php", // 'title'=>"Import POP3 Mail", 'label'=>"Import Inbox", // 'where'=>'item.php?type=i','when'=>'after', // 'options'=>array("user" => 'gtduser', // "pass" => 'gtdpass', // "server" => 'mail.server.com', // "port" => '995', // "type" => '/pop3/ssl/novalidate-cert', // "delete" => true) // ); //Subject becomes title //From & Body becomes description $title='Import POP3 Inbox'; include_once 'header.php'; $msoptions = $addon['options']; mysql_connect($config['host'],$config['user'],$config['pass']); mysql_select_db($config['db']); $imap = imap_open("{".$msoptions['server'].":".$msoptions['port'].$msoptions['type']."}INBOX", $msoptions['user'], $msoptions['pass']); if (!$imap) { print_r(imap_errors()); } else { $num_msg = imap_num_msg($imap); include_once('gtdfuncs.php'); echo "importing ".$num_msg." messages<br>"; echo "<div class='success'>\n"; for ($i = $num_msg; $i > 0; $i--) { // empty vars $from = ""; $subject = ""; $headers = ""; $message = ""; $header = imap_headerinfo($imap, $i, 80, 80); $from = $header->fromaddress; $subject = $header->fetchsubject; $date = date('YmdHis', $header->udate); $message=imap_body($imap, $i); $message="From: ".$from."\n\n".$message; mysql_query("INSERT INTO `".$config['prefix']."items` ( `itemId` , `title` , `description` , `desiredOutcome` )VALUES (NULL , '$subject', '$message', NULL);"); mysql_query("INSERT INTO `".$config['prefix']."itemstatus` (`itemId`, `dateCreated`, `lastModified`, `dateCompleted`) VALUES (NULL, '".date($config["datemask"])."', NOW(), NULL);"); mysql_query("INSERT INTO `".$config['prefix']."itemattributes` ( `itemId` , `type` , `isSomeday` , `categoryId` , `contextId` , `timeframeId` , `deadline` , `repeat` , `suppress` , `suppressUntil` )VALUES (NULL , 'i', 'n', '0', '0', '0', NULL , '0', 'n', NULL);"); echo "Imported '".stripslashes(escapeChars($subject)),"' into Inbox<br />\n"; if ($msoptions['delete']) imap_delete($imap, $i); } echo "</div>"; if ($msoptions['delete']) imap_expunge($imap); imap_close($imap); } include_once 'footer.php'; ?> |
Works pretty well at this point. Though I am having problems getting mail into that POP3 account. Will have to work further on that today 🙂
Cheers, Chris.
You must be logged in to post a comment.