Archive

Posts Tagged ‘ez components’

Mail parsing via Ez Components

April 18, 2010 3 comments

Get started with EZ Components:

After spending ma 24hrs I landed on a blog where the guys have an excellent tutorial series to get started with Ez components in detail so skim it first here you go [1] Hello World![2]  Send an Email[3] RSS feed creation.

sudo apt-get install apache2

sudo apt-get install php5

sudo apt-get install libapache2-mod-php5

sudo /etc/init.d/apache2 restart

Pear Package Manager installation on Linux:

lynx -source http://pear.php.net/go-pear | php

sudo apt-get install lynx-cur

sudo apt-get install php5-cli

Ez Component using Pear Installed

pear channel-discover components.ez.no
pear install -a ezc/eZComponents

Setup include_path

Navigate to the following files and append the root PEAR directory.

/etc/php5/apache2/php.ini
/etc/php5/cli/php.ini

include_path=”.:/var/www/path/to/root/PEAR”

At the top of the code you wrote using ez libs

require_once(“ezc/Base/base.php”);  //depend of the installation method either pear or svn
function __autoload( $className )
{
ezcBase::autoload( $className );
}

Pre-requisites:

  1. Xampp
  2. Ez Components

Task 1: Receiving and Parsing emails:

In the code below we are going to receive emails from a remote server and parse it’s parts. Moreover we’ll tear down the attached files and move them to a separate directory for later processing.

<?php
$dir = dirname( __FILE__ );
require_once “$dir/../../ezcomponents-2009/Mail/docs/tutorial/tutorial_autoload.php”;

$options = new ezcMailPop3TransportOptions();
$options->ssl = true;
$pop3 = new ezcMailPop3Transport( “pop.gmail.com”, 995, $options  );
$pop3->authenticate( “sendmarks@gmail.com”, “cubesatlover” );
$pop3->status( $num, $size );
$messages = $pop3->listMessages();
$set = $pop3->fetchAll();
$parser = new ezcMailParser();
$mail = $parser->parseMail( $set );

//echo print_r($mail);

for ( $i = 0; $i < count( $mail ); $i++ )
{
echo formatMail( $mail[$i] );
}

function formatMail( $mail )
{
$t = ”;
$t .= “From:      “. formatAddress( $mail->from ). “<br />”;
$t .= “To:        “. formatAddresses( $mail->to ). “<br />”;
$t .= “Cc:        “. formatAddresses( $mail->cc ). “<br />”;
$t .= “Bcc:       “. formatAddresses( $mail->bcc ). “<br />”;
$t .= “Date:      “. date( DATE_RFC822, $mail->timestamp ).
“<br />”;
$t .= “Subject:   “. $mail->subject . “<br />”;
$t .= “MessageId: “. $mail->messageId . “<br />”;
$t .= “<br />”;
$t .= formatMailPart( $mail->body ) . “<br />”;
return $t . “<br /><br />”;
}

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 ) . “<br />” );
}

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 ) . “<br />” );
}

function formatMailMultipartMixed( $part )
{
$t = ”;
foreach ( $part->getParts() as $key => $alternativePart )
{
$t .= “-MIXED-
$key——————————————————————
<br />”;
$t .= formatMailPart( $alternativePart );
}
$t .= “-MIXED
END———————————————————-<br />”;
return $t;
}

function formatMailMultipartRelated( $part )
{
$t = ”;
$t .= “-RELATED MAIN
PART———————————————————–<br />”;
$t .= formatMailPart( $part->getMainPart() );
foreach ( $part->getRelatedParts() as $key => $alternativePart )
{
$t .= “-RELATED PART
$key—————————————————–<br />”;
$t .= formatMailPart( $alternativePart );
}
$t .= “-RELATED
END——————————————————–<br />”;
return $t;
}

function formatMailMultipartDigest( $part )
{
$t = ”;
foreach ( $part->getParts() as $key => $alternativePart )
{
$t .= “-DIGEST-
$key—————————————————————–
<br />”;
$t .= formatMailPart( $alternativePart );
}
$t .= “-DIGEST
END———————————————————<br />”;
return $t;
}

function formatMailRfc822Digest( $part )
{
$t = ”;
$t .= “-DIGEST-ITEM-
$key————————————————————<br />”;
$t .= “Item:<br /><br />”;
$t .= formatMailpart( $part->mail );
$t .= “-DIGEST ITEM
END—————————————————-<br />”;
return $t;
}

function formatMailMultipartAlternative( $part )
{
$t = ”;
foreach ( $part->getParts() as $key => $alternativePart )
{
$t .= “-ALTERNATIVE ITEM
$key——————————————————-<br />”;
$t .= formatMailPart( $alternativePart );
}
$t .= “-ALTERNATIVE
END—————————————————-<br />”;
return $t;
}

function formatMailText( $part )
{
$t = ”;
$t .= “Original Charset: {$part->originalCharset}<br />”;
$t .= “Charset:          {$part->charset}<br />”;
$t .= “Encoding:         {$part->encoding}<br />”;
$t .= “Type:             {$part->subType}<br />”;
$t .= “<br />{$part->text}<br />”;
return $t;
}

function formatMailFile( $part )
{
$t = ”;
$t .= “Disposition Type: {$part->dispositionType}<br />”;
$t .= “Content Type:     {$part->contentType}<br />”;
$t .= “Mime Type:        {$part->mimeType}<br />”;
$t .= “Content ID:       {$part->contentId}<br />”;
$t .= “Filename:         {$part->fileName}<br />”;
$t .= “<br />”;
move_uploaded_file ( $part->fileName , “/var/www/vhosts/domain.com/
httpdocs/image.png”);
$t .= “<img src=’/image.png’>”;
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}>”;
}

?>

%d bloggers like this: