Code

Test PHP Connection to IMAP Mailbox

Simply enter your server location, account name, and password into this code and it will connect to your email account via IMAP (or display the reason why it couldn't connect).

For more information on PHP IMAP functions, see the official documentation.

<?php

$mail_server = "imap.example.com" ;
$mail_port = 143 ;
$mail_username = "test@example.com" ;
$mail_password = "pw" ;

echo "<h1>".$mail_username." on ".$mail_server."</h1>\n\n" ;
$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>\n\n";
} else {
    echo "<h2>".$mail_folder."</h2>\n" ;
    echo "<ol>\n" ;
    $msgno = 0;
    foreach ($mailboxheaders as $val) {
        $msgno++;
        echo "  <li>".$val ."</li>\n";
    }
    echo "</ol>\n\n" ;
}
imap_close($mbox);

?>

0 Comments

Be the first to leave a comment.

Leave a Comment

Name
Email
Website
Comment
Name and email are required. Your email will not be published.

This post was published on Friday, February 20th, 2009 by Robert James Reese in the following categories: IMAP, PHP. Before using any of the code or other content in this post, you must read and agree to our Terms & Conditions.

Copyright © 2012, Ink Plant. All rights reserved.