tips:imap2wiki
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
wiki:tips:imap2wiki [2006-11-13 14:48] – old revision restored andi | tips:imap2wiki [2010-01-26 19:10] – 129.74.96.230 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== IMAP2wiki ====== | ||
+ | |||
+ | Below you find a script that connects to an IMAP server, and posts the emails to the namespace that is in the subject line. | ||
+ | |||
+ | By this method it is possible to have people comment on things that appear in electronic newsletters, | ||
+ | |||
+ | Regard this as experimental, | ||
+ | |||
+ | Please commment below if you use or adapt this script, I'd be glad to hear from you. | ||
+ | |||
+ | ====== How to use ====== | ||
+ | |||
+ | - adapt the configuration data below to match your IMAP account, replace the imapserver IP address with your own | ||
+ | - put the script somewhere on your server | ||
+ | - you may run it manually or have it run as a cronjob | ||
+ | ===== Code ===== | ||
+ | |||
+ | |||
+ | <file php imap2wiki.php> | ||
+ | <? | ||
+ | |||
+ | /** | ||
+ | * IMAP2WIKI | ||
+ | * | ||
+ | * Usage: | ||
+ | * | ||
+ | * Gets email from imap account and adds it to a DokuWiki page | ||
+ | * Typically used to give offline/ | ||
+ | * Subject of email indicates namespace and pagename that will be used to add comments to | ||
+ | | ||
+ | * Tested with UTF-8 encoded subjects/ | ||
+ | * | ||
+ | * Todo : | ||
+ | * Todo : error checking for nasty situations: | ||
+ | - subject is not valid wiki namespace | ||
+ | - IMAP server does not respond | ||
+ | - posting does not succeed | ||
+ | |||
+ | * @license | ||
+ | * @author | ||
+ | * @thanks Andreas Gohr, Harry Fuecks, Christopher Arndt, Christopher Smith and DokuWiki community | ||
+ | */ | ||
+ | |||
+ | // SETUP | ||
+ | |||
+ | $host =" | ||
+ | $dokuwikipath ="/ | ||
+ | $discussionprefix = " | ||
+ | $imapuser =" | ||
+ | $imappass =" | ||
+ | |||
+ | |||
+ | // FUNCTIONS | ||
+ | |||
+ | function _decodeHeader($input) | ||
+ | // Original from RFC2047 routine from http:// | ||
+ | // Remove white space between encoded-words | ||
+ | $input = preg_replace('/ | ||
+ | // For each encoded-word... | ||
+ | while (preg_match('/ | ||
+ | |||
+ | $encoded | ||
+ | $charset | ||
+ | $encoding = $matches[3]; | ||
+ | $text = $matches[4]; | ||
+ | |||
+ | switch (strtolower($encoding)) { | ||
+ | case ' | ||
+ | $text = base64_decode($text); | ||
+ | break; | ||
+ | |||
+ | case ' | ||
+ | $text = str_replace(' | ||
+ | preg_match_all('/ | ||
+ | foreach($matches[1] as $value) | ||
+ | $text = str_replace(' | ||
+ | break; | ||
+ | } | ||
+ | |||
+ | $input = str_replace($encoded, | ||
+ | } | ||
+ | |||
+ | return $input; | ||
+ | } | ||
+ | |||
+ | |||
+ | function PostToHost($host, | ||
+ | $fp = fsockopen($host, | ||
+ | printf(" | ||
+ | fputs($fp, "POST $path HTTP/ | ||
+ | fputs($fp, "Host: $host\r\n" | ||
+ | fputs($fp, " | ||
+ | fputs($fp, " | ||
+ | fputs($fp, " | ||
+ | fputs($fp, " | ||
+ | fputs($fp, $data_to_send); | ||
+ | printf(" | ||
+ | while(!feof($fp)) { | ||
+ | $res .= fgets($fp, 128); | ||
+ | } | ||
+ | printf(" | ||
+ | fclose($fp); | ||
+ | return $res; | ||
+ | } | ||
+ | |||
+ | |||
+ | function GetRaw($host, | ||
+ | // gets raw content (not authenticated) | ||
+ | $filetoget=" | ||
+ | return implode('', | ||
+ | } | ||
+ | |||
+ | |||
+ | ///////////////////////////////////////////////////////////////////// | ||
+ | |||
+ | // connect to IMAP server 82.62.3.23 | ||
+ | |||
+ | $mailbox = imap_open(" | ||
+ | |||
+ | // check mailbox | ||
+ | $check = imap_check($mailbox); | ||
+ | |||
+ | // check nr of messages | ||
+ | $noofmsg=$check-> | ||
+ | |||
+ | if ($noofmsg==0){ | ||
+ | echo "No messages to process"; | ||
+ | } | ||
+ | |||
+ | // for every message in inbox | ||
+ | for ($index=1; $index< | ||
+ | $header = imap_header($mailbox, | ||
+ | $mddate=$header-> | ||
+ | $mdbody = imap_body($mailbox, | ||
+ | $mdid = base64_encode($id); | ||
+ | $mdfrom = htmlspecialchars($header-> | ||
+ | $from=$header-> | ||
+ | $frommailhost=htmlspecialchars($from[0]-> | ||
+ | $frommailbox=htmlspecialchars($from[0]-> | ||
+ | $mdfromemail=$frommailbox." | ||
+ | |||
+ | echo "< | ||
+ | // $mdsubject = _decodeHeader($header-> | ||
+ | // Andi's snippet to decode base64 works fine as well... | ||
+ | |||
+ | $mdsubjectb64= preg_replace('/ | ||
+ | $mdsubjectURL=urlencode($mdsubjectb64); | ||
+ | |||
+ | // Post to wiki, by adding timestamp and email body to existing wikitext | ||
+ | $timestamp = date(" | ||
+ | $title=" | ||
+ | $wikiid=$discussionprefix.$mdsubjectURL; | ||
+ | |||
+ | // get existing wikitext | ||
+ | $oldwikitext=(GetRaw($host, | ||
+ | |||
+ | // compose new text to post to wiki | ||
+ | echo $mdbody; | ||
+ | $wikitext=$oldwikitext." | ||
+ | $targeturl=" | ||
+ | $data = "& | ||
+ | |||
+ | echo (" | ||
+ | |||
+ | $x = PostToHost($host, | ||
+ | |||
+ | // if posted successfully (no check yet!): | ||
+ | |||
+ | imap_delete($mailbox, | ||
+ | |||
+ | } | ||
+ | imap_expunge ($mailbox); | ||
+ | imap_close($mailbox); | ||
+ | |||
+ | ?> | ||
+ | </ | ||
+ | ====== Discussion ====== | ||
+ | |||
+ | I can't get this to work. I get the following message: | ||
+ | Wiki found... Submitted to localhost | ||
+ | Posted to localhost | ||
+ | |||
+ | But the wiki is not updated with the new info. | ||
+ | Also i had to add /notls to the imap_server config to disable SSL connection. | ||
+ | |||
+ | -- | ||
+ | Check your serverlog if the connection has been made. Did you change the URL to match your domain? Standard replies are posted to the // | ||
+ | |||
+ | -- | ||
+ | * Is there a way to connect to Gmail using this method? Thanks. | ||
tips/imap2wiki.txt · Last modified: 2010-01-26 20:26 by 74.93.99.97