Jun 12 2007

Email Reading Library (Source Code)

php email library

With the support of component ezc , You can read your email via pop3 protocol.

The email and password used here are only for temporary purpose. Try this code:

Click more for source code:

require_once("ezc/Base/base.php");

function __autoload( $className )
{
ezcBase::autoload( $className );

}

$pop3 = new ezcMailPop3Transport( "torque.co.in" );
$pop3->authenticate( temp@temp.com, "password" );
$set = $pop3->fetchAll(); // if you put "true" inside this function, all the mail will be deleted after retrieving. eg: fetchAll(true)
$parser = new ezcMailParser();
$mails = $parser->parseMail( $set );
$mail = end($mails);

echo formatMail($mail);

function formatMail( $mail )
{

$t = '';

$t .= "From: ". formatAddress( $mail->from ). "\n";
$t .= "To: ". formatAddresses( $mail->to ). "\n";
$t .= "Cc: ". formatAddresses( $mail->cc ). "\n";
$t .= "Bcc: ". formatAddresses( $mail->bcc ). "\n";
$t .= 'Date: '. date( DATE_RFC822, $mail->timestamp ). "\n";
$t .= 'Subject: '. $mail->subject . "\n";
$t .= "MessageId: ". $mail->messageId . "\n";
$t .= "\n";

$t .= formatMailPart( $mail->body );
return $t;
}

function formatMailPart( $part )
{
if ( $part instanceof ezcMail )
return formatMail( $part );

if ( $part instanceof ezcMailText )
return formatMailText( $part );

if ( $part instanceof ezcMailFile )
return formatMailFile( $part );

if ( $part instanceof ezcMailRfc822Digest )
return formatMailRfc822Digest( $part );

if ( $part instanceof ezcMailMultiPart )
return formatMailMultipart( $part );

die( "No clue about the ". get_class( $part ) . "\n" );
}

function formatMailMultipart( $part )
{
if ( $part instanceof ezcMailMultiPartAlternative )
return formatMailMultipartAlternative( $part );

if ( $part instanceof ezcMailMultiPartDigest )
return formatMailMultipartDigest( $part );

if ( $part instanceof ezcMailMultiPartRelated )
return formatMailMultipartRelated( $part );

if ( $part instanceof ezcMailMultiPartMixed )
return formatMailMultipartMixed( $part );

die( "No clue about the ". get_class( $part ) . "\n" );
}

function formatMailMultipartMixed( $part )
{
$t = '';
foreach ( $part->getParts() as $key => $alternativePart )
{
$t .= "-MIXED-$key------------------------------------------------------------------\n";
$t .= formatMailPart( $alternativePart );
}
$t .= "-MIXED END----------------------------------------------------------\n";
return $t;
}

function formatMailMultipartRelated( $part )
{
$t = '';
$t .= "-RELATED MAIN PART-----------------------------------------------------------\n";
$t .= formatMailPart( $part->getMainPart() );
foreach ( $part->getRelatedParts() as $key => $alternativePart )
{
$t .= "-RELATED PART $key-----------------------------------------------------\n";
$t .= formatMailPart( $alternativePart );
}
$t .= "-RELATED END--------------------------------------------------------\n";
return $t;
}

function formatMailMultipartDigest( $part )
{
$t = '';
foreach ( $part->getParts() as $key => $alternativePart )
{
$t .= "-DIGEST-$key-----------------------------------------------------------------\n";
$t .= formatMailPart( $alternativePart );
}
$t .= "-DIGEST END---------------------------------------------------------\n";
return $t;
}

function formatMailRfc822Digest( $part )
{
$t = '';
$t .= "-DIGEST-ITEM-$key------------------------------------------------------------\n";
$t .= "Item:\n\n";
$t .= formatMailpart( $part->mail );
$t .= "-DIGEST ITEM END----------------------------------------------------\n";
return $t;
}

function formatMailMultipartAlternative( $part )
{
$t = '';
foreach ( $part->getParts() as $key => $alternativePart )
{
$t .= "-ALTERNATIVE ITEM $key-------------------------------------------------------\n";
$t .= formatMailPart( $alternativePart );
}
$t .= "-ALTERNATIVE END----------------------------------------------------\n";
return $t;
}

function formatMailText( $part )
{
$t = '';
$t .= "Original Charset: {$part->originalCharset}\n";
$t .= "Charset: {$part->charset}\n";
$t .= "Encoding: {$part->encoding}\n";
$t .= "Type: {$part->subType}\n";
$t .= "\n{$part->text}\n";
return $t;
}

function formatMailFile( $part )
{
$t = '';
$t .= "Disposition Type: {$part->dispositionType}\n";
$t .= "Content Type: {$part->contentType}\n";
$t .= "Mime Type: {$part->mimeType}\n";
$t .= "Content ID: {$part->contentId}\n";
$t .= "Filename: {$part->fileName}\n";
$t .= "\n";
return $t;
}

function formatAddresses( $addresses )
{
$fa = array();
foreach ( $addresses as $address )
{
$fa[] = formatAddress( $address );
}
return implode( ', ', $fa );
}

function formatAddress( $address )
{
$name = '';
if ( !empty( $address->name ) )
{
$name = "{$address->name} ";
}
return $name . "<{$address->email}>";

}

TAGS:

4 Comments on this post

Trackbacks

  1. salihpk said:

    Sir,
    I am from India/Kerala and I like all your codes and interested to study something from you !

    Plz mail me

    June 24th, 2007 at 1:36 pm
  2. ghulamfareed said:

    AA
    hi i am from paistan and i wanted ot get the source code of php and interest to learn some thing

    August 9th, 2007 at 11:11 am
  3. shobn said:

    Good one mate.. your site have some real good stuff!!! Keep up the good work

    March 4th, 2008 at 12:56 pm
  4. Thomas Alexander said:

    how can a word file can be viewed as html using php?

    October 15th, 2008 at 5:07 pm

LEAVE A COMMENT

Subscribe Form

Subscribe to Blog

Sponsors

    Advt on sajithmr.com
    Advt on sajithmr.com
    Itslife Online
    Advt on sajithmr.com

Recent Comments

  • Binny V A: Thanks for the post - love the picture ;-)
  • TheAnand: A lot of people are seeing errors with google video chat….is there any other software which has to be...
  • Alex: Wow, interresting analysis you have done! I’m trying to run GoogleVoiceAndVideoSetup on linux, using...
  • Mella Fitriansyah: Nice plugin, I will try to add this plugin in my blog…
  • Mella Fitriansyah: Nice Info, Sir i will reading your another post success for you

Recent Readers

JOIN MY COMMUNITY!

Recent Posts