Google::IMAP/POP3 – custom your own mail application

IMAP access to google is open for all. I assumed that you already know what is IMAP and Google Settings. So, you can grab my written here. For a generic IMAP connection to google mail, you need to set SSL no-validate option as well.

$inbox = @imap_open("{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX", $email, $password);

Port 993 is for gmail secured IMAP. And 995 is for POP3. We can use POP3 protocol instead of IMAP here –

$inbox = @imap_open("{pop.gmail.com:995/pop3/ssl/novalidate-cert}INBOX", $email, $password);

ssl” will transport the data using “tsl” protocol for mail. “novalidate-cert” will not try to validate the SSL certificate from the user side. It is mandatory to access secured server if we have not .cert file to validate connection. Here “INBOX” is the mailbox which we want to access for mails, we can change it for our needs, such as it might be “Trash”.

    if(!$inbox) die("Mail server connect failed");
    $foo = imap_errors();

After successful login to IMAP server, we want to count the total mail. So –

    $total = imap_num_msg($inbox);
    if($total < 1) { echo "No Message for $email"; }
    else { echo "Our code goes here in the next";}
[/php]

Now we will access the email from the mailbox -
[php]
for ( $i=$total; $i >= 1; $i-- ) {
$headers = @imap_header($inbox, $i);
$fromaddress = $headers->from[0]->mailbox . '@' . $headers->from[0]->host;
echo $message_body =  @imap_body( $inbox, $i);
}

If we want to delete this mail from inbox, then we need to call imap_delete() function. And to move the mail to trash, we need to call imap_mail_move() function with mailbox name ‘[Gmail]/Trash’ parameter.

@imap_delete($inbox, $i);
@imap_mail_move($inbox, "$i:$i", '[Gmail]/Trash');

To close the mailbox, we need to call imap_close() function. And calling of imap_expunge() function is mandatory to delete the mails which are marked from the code above. imap_expunge() should call before imap_close() function.

	@imap_expunge($inbox); 				
	@imap_close($inbox);

Complete code:

    $inbox = @imap_open("{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX", $email, $password);
    if(!$inbox) die("Mail server connect failed");
    $foo = imap_errors();
    $total = imap_num_msg($inbox);
    if($total < 1) { echo "No Message for $email"; }
    else { 
        for ( $i=$total; $i >= 1; $i-- ) {
            $headers = @imap_header($inbox, $i);
            $fromaddress = $headers->from[0]->mailbox . '@' . $headers->from[0]->host;
            echo $message_body =  @imap_body( $inbox, $i);
            if($headers->fromaddress == '[email protected]') {
            @imap_delete($inbox, $i);
            @imap_mail_move($inbox, "$i:$i", '[Gmail]/Trash');
            }
        }
    }
    @imap_expunge($inbox); 				
    @imap_close($inbox);
?>


All comments of post - "Google::IMAP/POP3 – custom your own mail application":

:Haha! I'am the first! Yeh~

Comments are currently closed.

Add a Comment / Trackback url

  1. #10  Winston Simonetti

    Howdy! This is kind of off topic but I need some advice from an established blog. Is it very hard to set up your own blog? I’m not very techincal but I can figure things out pretty fast. I’m thinking about creating my own but I’m not sure where to start. Do you have any ideas or suggestions? With thanks

    11/05/08 17:37
  2. #9  Kimbra Rusten

    Great blog, Just wanted to comment that i can not connect to the rss stream, you might want install the right wordpress plugin for that to workthat.

    11/05/05 07:40
  3. #8  Cristin Aseng

    Strong blog. I acquired variant nice info. I?ve been keeping a watch on this technology for a few time. It?utes fascinating the approach it retains completely different, nonetheless a number of of the primary parts remain identical. have you observed lots change since Search engines created their own latest purchase in the field?

    11/05/05 00:08
  4. #7  Jason Burnett

    Great post. I used to be checking continuously this blog and I’m inspired! Extremely helpful info specifically the closing phase :) I care for such info much. I was seeking this certain info for a very long. Thank you and best of luck.

    11/05/04 16:45
  5. #6  ninja blender

    What’s Happening i’m new to this, I stumbled upon this I have found It absolutely helpful and it has helped me out loads. I hope to contribute & aid other users like its aided me. Great job.

    11/04/05 02:33
  6. #5  Rosalinda

    Could be the GREATEST blog I have read this month…

    11/01/12 06:41
  7. #4  Carson

    The most interesting read that I read this week??

    11/01/11 01:54
  8. #3  Mia Darnell

    Letha, that logic is flawed!?!

    11/01/10 18:14
  9. #2  Ashley Mitchell

    I have to hear just what John can do about this…

    -Sincere regards
    Pearlie

    11/01/09 14:09
  10. #1  Preston Wright

    I wonder what Benito can do about this :)

    Minerva

    11/01/08 10:43

Show all Show 5 so far Close all

Comment begin from here or jump up!