Monday, July 9, 2012

connect to mailbox with IMAP - php

install
  • php-imap
  • php-cli
modules in-order to work IMAP with php.

search for the  following line in your php.ini file

extension = imap.so

then search for the imap.ini file in /etc/php5/conf.d directory , if not exist create it with following line in it.

extension = imap.so


then restart apache server.

use following php sample code with your server name and user credentials to check connectivity.

<?php

$mail_server = "mail.udara.com" ;
$mail_port = "143/novalidate-cert" ;
$mail_username = "udara@udara.com" ;
$mail_password = "password" ;
$mail_folder = "INBOX";

echo "<h1>".$mail_username." on ".$mail_server."</h1><br/>" ;

$mbox = imap_open("{".$mail_server.":".$mail_port."}".$mail_folder, $mail_username, $mail_password) or die("Error opening mailbox: ".imap_last_error());

$mailboxheaders = imap_headers($mbox);

if ($mailboxheaders == false) {
   echo "<p>".$mail_folder." is empty.</p>";
} else {
   echo "<h2>".$mail_folder."</h2>" ;
   echo "<ol>" ;
   $msgno = 0;
   foreach ($mailboxheaders as $val) {
$msgno++;
echo "  <li>".$val ."</li>";
   }
    echo "</ol>" ;
}


imap_close($mbox);

?>



No comments:

Post a Comment