<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Yet another blog about PHP, HTML and CSS &#187; IMAP</title>
	<atom:link href="http://blog.pepa.info/php-html-css/category/imap/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.pepa.info</link>
	<description>Petr 'PePa' Pavel</description>
	<lastBuildDate>Thu, 19 Aug 2010 18:17:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>processing IMAP folder on a regular basis</title>
		<link>http://blog.pepa.info/php-html-css/imap/processing-imap-folder-on-a-regular-basis/</link>
		<comments>http://blog.pepa.info/php-html-css/imap/processing-imap-folder-on-a-regular-basis/#comments</comments>
		<pubDate>Sun, 13 Apr 2008 08:36:41 +0000</pubDate>
		<dc:creator>Petr 'PePa' Pavel</dc:creator>
				<category><![CDATA[IMAP]]></category>

		<guid isPermaLink="false">http://blog.pepa.info/php-html-css/uncategorized/processing-imap-folder-on-a-regular-basis/</guid>
		<description><![CDATA[When you need to check a mailbox on a regular basis and process messages that has arrived since your last check you may find that some messages were somehow skipped. It can be caused by (among other things) one of these two reasons: you ignore time zones you use message&#8217;s sent date instead of delivery [...]]]></description>
			<content:encoded><![CDATA[<p>When you need to check a mailbox on a regular basis and process messages that has arrived since your last check you may find that some messages were somehow skipped.</p>
<p><span id="more-17"></span></p>
<p>It can be caused by (among other things) one of these two reasons:</p>
<ul>
<li>you ignore time zones</li>
<li>you use message&#8217;s sent date instead of delivery date</li>
</ul>
<p>Here&#8217;s a piece of code that could put you on a right track:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="re0">$mailBox</span> = imap_open<span class="br0">&#40;</span><span class="st0">&quot;{server.com:143}INBOX&quot;</span>, <span class="re0">$userName</span>, <span class="re0">$password</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// SSL alternative:</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// $mailBox = imap_open(&quot;{server.com:993/imap/ssl/novalidate-cert}INBOX&quot;, $userName, $password);</span></div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$lastCheckDate</span> = <a href="http://www.php.net/date"><span class="kw3">date</span></a><span class="br0">&#40;</span><span class="st0">&quot;d-M-Y&quot;</span>, <span class="br0">&#40;</span><span class="re0">$lastCheckTimestamp</span> &#8211; <span class="nu0">60</span>*<span class="nu0">60</span>*<span class="nu0">24</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="re0">$messageUIDsArray</span> = imap_search<span class="br0">&#40;</span><span class="re0">$mailBox</span>, <span class="st0">&#8216;SINCE &quot;$lastCheckDate&quot;&#8217;</span>, SE_UID<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">foreach</span> <span class="br0">&#40;</span><span class="re0">$messageUIDsArray</span> <span class="kw1">as</span> <span class="re0">$messageUID</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$message</span> = imap_headerinfo<span class="br0">&#40;</span><span class="re0">$mailBox</span>, imap_msgno<span class="br0">&#40;</span><span class="re0">$mailBox</span>, <span class="re0">$messageUID</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// imap_headerinfo() knows no UIDs so you have to convert UID to message sequence number</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&quot;This is delivery date: &quot;</span>.<span class="re0">$message</span>-&gt;<span class="me1">MailDate</span>.<span class="st0">&quot; and this is sent date: &quot;</span>.<span class="re0">$message</span>-&gt;<span class="me1">date</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$messageTimestamp</span> = <a href="http://www.php.net/strtotime"><span class="kw3">strtotime</span></a><span class="br0">&#40;</span><span class="re0">$message</span>-&gt;<span class="me1">MailDate</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$lastCheckTimestamp</span> &lt;= <span class="re0">$messageTimestamp</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">doYourMagic<span class="br0">&#40;</span><span class="re0">$message</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>I could deal with time zones better (finding out what time zone the IMAP server is in and build $lastCheckDate from $lastCheckTimestamp in that time zone). I&#8217;m lazy though so I simply deduct 24 hours. That deals with messages delivered around midnight if you&#8217;re not in GMT.</p>
<p>Using a sent timestamp instead of a delivery timestamp is a common pitfall. As far as I know, all PHP functions that return message date return sent date (i.e. information from header Date:) and no delivery date. A message can travel for a long time before getting to you. If you check the mailbox once in an hour it can easily happen that you skip a message that was held in a queue somewhere on its way for more than an hour. Also sender&#8217;s computer time can be set wrongly. Remember that the information entered into Date: can be anything. It&#8217;s the sender&#8217;s e-mail client who decides about it.</p>
<h3>How to debug</h3>
<p>The last issue I had to deal with was what date format the IMAP server expects in your SEARCH SINCE command. 11-Apr-2008 worked for me (both 01-Apr-2008 and 1-Apr-2008 are fine) but if you&#8217;re getting no results you might want to take a peek under the hood. Here&#8217;s how to telnet to the IMAP server and ask yourself:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">telnet server.com 143</div>
</li>
</ol>
</div>
<p>Enter this into the telnet window to talk to the IMAP server:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">10 LOGIN yourUserName yourPassword
</div>
</li>
<li class="li1">
<div class="de1">40 SELECT INBOX
</div>
</li>
<li class="li1">
<div class="de1">50 UID SEARCH SINCE &quot;11-Apr-2008&quot;</div>
</li>
</ol>
</div>
<p>These are just commands that you say, I don&#8217;t include responses. Please note that you have to start each of your commands with an increasing number. It doesn&#8217;t have to be a regular row of numbers, the next number just has to be bigger than the previous one.The search command returns a list of message UIDs that meet your criteria. To see header of each message try this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">60 UID FETCH messageUIDnumber ALL</div>
</li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.pepa.info/php-html-css/imap/processing-imap-folder-on-a-regular-basis/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
