<?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>westworld: a webmasters best friend &#187; PHP IMAP</title>
	<atom:link href="http://www.westworld.be/tag/php-imap/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.westworld.be</link>
	<description>A webmasters best friend</description>
	<lastBuildDate>Tue, 17 Aug 2010 10:37:26 +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>Cleaning up after an emailing</title>
		<link>http://www.westworld.be/a-note-to-self/cleaning-up-after-an-emailing/</link>
		<comments>http://www.westworld.be/a-note-to-self/cleaning-up-after-an-emailing/#comments</comments>
		<pubDate>Thu, 05 Nov 2009 15:48:23 +0000</pubDate>
		<dc:creator>westworld</dc:creator>
				<category><![CDATA[a note to self]]></category>
		<category><![CDATA[PHP IMAP]]></category>

		<guid isPermaLink="false">http://www.westworld.be/?p=168</guid>
		<description><![CDATA[As a web master, I often send out mass mailings. After the mailing is done, the bounced emails are returned to my Outlook. I go through them manually. The real bouncers are put in a &#8220;Bounced&#8221; folder and,  Out of office messages in the &#8220;Trash&#8221;. The rest of the emails are addresses I need to [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>As a web master, I often send out mass mailings. After the mailing is done, the bounced emails are returned to my Outlook. I go through them manually. The real bouncers are put in a &#8220;Bounced&#8221; folder and,  Out of office messages in the &#8220;Trash&#8221;. The rest of the emails are addresses I need to update in my database or people that wish to unsubscribe. Since sorting through 2000 bouncers can take up a lot of time, I did some research on how to automate this.</p>
<p>PHP offers a set of functions to connect to a mail server (IMAP/POP3).  The plan was to let my web server connect to the mail server and sort out the bouncers for me. Below is a little script to give you some ideas of what I came up with.</p>
<textarea cols="40" rows="10" name="code" class="Php"><?php
// fill in your SERVER, LOG and PAS!
$mbox = imap_open("{SERVER:143}", "LOG", "PAS");

echo "<h1>Mailboxes</h1>\n";
$folders = imap_listmailbox($mbox, "{SERVER:143}", "*");
if($folders == false) {echo "Call failed<br />\n";}
else{
	foreach ($folders as $val) { echo $val . "<br />\n";  }
}

$list = array(); // array will store all bounced email adresses
$result = imap_search($mbox,'SUBJECT "failure notice"'); // find all with subject 'failure notice'
if(!empty($result)){
foreach($result as $msgno) {
    $body = imap_fetchbody($mbox, $msgno,1); // fetch the body of the message
	$find= "To: "; // look for the 'To' part
	$foundatloc= strpos($body,$find); //remember the location
	if ($foundatloc > 1){ // test if a mail adres was found
		$findend = "\n"; 
		$foundendloc = strpos($body,$findend,$foundatloc); // find  the location of EOL
		$foundendloc = $foundendloc -($foundatloc + 5);
		$adres = substr($body,$foundatloc+5,$foundendloc); 
		$adres = str_replace("<","",$adres); // strip <, > and spaces
		$adres = str_replace(">","",$adres);
		$adres = trim($adres);
		array_push($list,$adres); // put the adres is $list
		imap_mail_move($mbox,$msgno,"INBOX.Trash"); // flag as  "move to trash"
		imap_expunge($mbox); // execute move to trash folder
	}
}
unset ($result);
}
// put function to process bouncers/$list here		 
imap_close($mbox); // close connection
?></textarea>
	<!-- WordPress Code Snippet -->
	<script type="text/javascript" src="http://www.westworld.be/wp-content/plugins/wordpress-code-snippet/js/shCore.js"></script><script type="text/javascript" src="http://www.westworld.be/wp-content/plugins/wordpress-code-snippet/js/shBrushPhp.js"></script>
	<link type="text/css" rel="stylesheet" href="http://www.westworld.be/wp-content/plugins/wordpress-code-snippet/css/SyntaxHighlighter.css"/>
	
	<script language="javascript">
	dp.SyntaxHighlighter.ClipboardSwf = 'http://www.westworld.be/wp-content/plugins/wordpress-code-snippet/js/clipboard.swf';
	dp.SyntaxHighlighter.HighlightAll('code');
	</script>
	<!-- End WordPress Code Snippet -->
	

<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.westworld.be/a-note-to-self/cleaning-up-after-an-emailing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
