Subscribe For Free Updates!

We'll not spam mate! We promise.

May 21, 2013

Get body message content from mail using PHP

Get body message content from mail using PHP :

 Here i will explain how to get mail contents(body of mail) by using php. Here i used two functions like imap_num_msg() and imap_body() which return the contents of a specific message respectively and the number of messages in the mail box.

Syntax of these function:

int imap_num_msg ( resource imap_stream)
string imap_body ( resource imap_stream, int msg_number, int options)

1. To get number of mail in inbox :


 For get this we need to pass IMAP stream into imap_num_msg() function and it return the result.


<?php

    $imap = imap_open("{mail.yourserver.com:143}INBOX", "username", "password");

    $message_count = imap_num_msg($imap);

    print $message_count;

    imap_close($imap); 

?>


2. To get message content :

 <?php

    $imap = imap_open("{mail.yourserver.com:143}INBOX", "username", "password");

    $message_count = imap_num_msg($imap);

    for ($i = 1; $i <= $message_count; ++$i) {

        $header = imap_header($imap, $i);

        $body = trim(substr(imap_body($imap, $i), 0, 100)); // 100 specifies no.of character u need.

        $prettydate = date("jS F Y", $header->udate);

        if (isset($header->from[0]->personal)) {

            $personal = $header->from[0]->personal;

        } else {

            $personal = $header->from[0]->mailbox;

        }

        $email = "$personal <{$header->from[0]->mailbox}@{$header->from[0]->host}>";

        echo "On $prettydate, $email said \"$body\".\n";

    }

    imap_close($imap); 

?>





TechniqZone Socializer Widget
SOCIALIZE IT →
FOLLOW US →
SHARE IT →

0 comments:

Post a Comment