PHP IMAP read unread email from inbox search by subject and date Turn Back
2017-03-13 17:00:25
<?php
ini_set('max_execution_time', 300); //300 seconds = 5 minutes
/* connect to notes */
$hostname = '{mail.yourhost.com/imap/ssl}INBOX';
$username = 'xxx@yourhost.com';
$password = 'xxx';
/* Search Config */
$search = 'word';
$dates = date('j F Y'); //format: 8 March 2017
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to host: ' . imap_last_error());
$emails = imap_search($inbox, 'UNSEEN SUBJECT "'.$search.'" ON "'.$dates.'"', SE_FREE, "UTF-8");
$loop=1;
$files = array();
$temp_path = "temp/";
if($emails) {
rsort($emails);
foreach($emails as $email_number) {
//if($loop>1)break;
$header = imap_fetchmime($inbox,$email_number,1);
$headers = imap_fetchmime($inbox,$email_number);
$overview = imap_fetch_overview($inbox,$email_number,0);
$structure = imap_fetchstructure($inbox, $email_number);
$str = substr($header, (int)strpos($header, "charset"), 30);
$str = explode(",",preg_replace('#s+#',',',trim($str)));
$str = str_replace('"', "", $str[0]);
$str = strtolower(str_replace('charset=', "", $str));
$sender = utf8_decode(imap_utf8($overview[0]->from));
$sender = str_replace("<", "(", $sender);
$sender = str_replace(">", ")", $sender);
$date = utf8_decode(imap_utf8($overview[0]->date));
$subject = '<p class="Head3_ข่าว" data-field="header">'.trim(iconv_mime_decode($overview[0]->subject,0,"UTF-8")).'</p>';
$pdate = date('Y-m-d H:i:s',strtotime($overview[0]->date));
$pdate = str_replace(" ", "_", $pdate);
$pdate = str_replace(":", "", $pdate);
$body ='';
if(count($structure->parts)>0){
for ($i = 0, $j = count($structure->parts); $i < $j; $i++) {
$part = $structure->parts[$i];
if ($part->subtype == 'HTML') {//PLAIN
$body = imap_fetchbody($inbox, $email_number, $i+1);
if($part->encoding == 3) {
$body = imap_base64($body);
} else if($part->encoding == 1) {
$body = imap_8bit($body);
} else {
$body = imap_qprint($body);
}
$body = $str=="utf-8" ? $body : iconv("TIS-620","UTF-8",$body);
}
}
}else{
$body = imap_body($inbox, $email_number);
if($structure->encoding == 3) {
$body = imap_base64($body);
} else if($structure->encoding == 1) {
$body = imap_8bit($body);
} else {
$body = imap_qprint($body);
}
$body = $str=="utf-8" ? $body : iconv("TIS-620","UTF-8",$body);
}
if(trim($body)=="")continue;
if($_GET['debug']==1)echo $loop.". ".$subject." (".$date.")<br/>";
/*Write XML*/
//////////////////////////////////////////////////////////
$xml = new DOMDocument("1.0", 'UTF-8');
$news = $xml->createElement("news");
$xml->appendChild( $news );
$news->appendChild($xml->createElement('newsID',''));
$header = $news->appendChild($xml->createElement("header"));
$header->appendChild($xml->createCDATASection( $subject ));
$news->appendChild($xml->createElement('byline',$sender));
$contentHTML = $news->appendChild($xml->createElement("contentHTML"));
$contentHTML->appendChild($xml->createCDATASection(remTag($body)));
$news->appendChild($xml->createElement('category',''));
$news->appendChild($xml->createElement('keywords',''));
$news->appendChild($xml->createElement('deskId','53bb652f007fe77c60000017'));
$news->appendChild($xml->createElement('publicationDate', date('Y-m-d H:i:s',strtotime($overview[0]->date) )));
$news->appendChild($xml->createElement('attachedResources',''));
$note = $news->appendChild($xml->createElement("note"));
$note->appendChild($xml->createCDATASection(''));
$xml->preserveWhiteSpace = false;
$xml->formatOutput = true;
$file = "news_".$loop."_".$pdate.".xml";
$xml->save($temp_path.$file);
$files[]=$file;
//////////////////////////////////////////////////////////
// Test
$loop++;
#if($loop>2)break;
} // End loop
/* Ftp upload *//////////////////////////////////////////
if(count($files)>0){
$ftp_server = "172.16.1.233";
$ftp_user_name = "ftp_newsxml";
$ftp_user_pass = "FTP#!newsxml";
$path = "NEWS_XML/";
foreach($files as $file){
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
if (ftp_put($conn_id, $path.$file, $temp_path.$file, FTP_ASCII))
{
if($_GET['debug']==1)echo 'put: '.$file.'<br/>';
#unlink($file);
rename($temp_path.$file, $temp_path.'sent_'.$file);
}else{
rename($temp_path.$file, $temp_path.'failed_'.$file);
}
}
ftp_close($conn_id);
}
/////////////////////////////////////////////////////////
}
function remTag($text){
$text = strip_tags($text, '<p>');
$text = preg_replace('#<p class="MsoNormal">(.*?)</p>#', '', $text);
//return trim(preg_replace('/class=".*?"/', '', $text));
$text = trim(preg_replace("/<([a-z][a-z0-9]*)[^>]*?(/?)>/i",'<$1$2>', $text));// strip all attributes
return str_replace('<p>','<p class="BodyText-ข่าว" data-field="story" style="text-indent: 25px;">',$text);
}
function TIS2UTF($tis620){
return iconv("tis-620", "utf-8", $tis620);
}
?>