PHP IMAP mail fetching including attachments..

November 8th, 2006 by ltz

Took me a while to figure out how to search and fetch a single attachment from a POP mailbox, without ending up with 300+ lines of code.. Maybe this one could help someone else..

// Open pop account.
$mailbox = imap_open( $popaddr, $popuser, $poppasswd);

// Search for the message we are looking for..
$result = imap_search($mailbox, $searchsubject, SE_UID);

if ($result) {

storePDFAttachment(imap_fetchbody($mailbox,$result[0],2),$savefilename,$savefilepath);

} else {

exit(“message not found in mailbox yet..”);

}

// Close the connection.
imap_close($mailbox);

function storePDFAttachment( $content , $filename , $localfilepath ) {

$file = fopen($localfilepath.$filename, ‘w’);
fwrite($file, imap_base64($content));

fclose($file);
}


Posted in Programming | No Comments »

Pierre Norraeus
Joomla! 1.5 Beta 2 - Red Barchetta