PHP code example of vaibhavpandeyvpz / phemail

1. Go to this page and download the library: Download vaibhavpandeyvpz/phemail library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

vaibhavpandeyvpz / phemail example snippets




use Phemail\MessageParser;

$parser = new MessageParser();
$message = $parser->parse('path/to/email.eml');

// Get header values
echo $message->getHeaderValue('subject');        // "Testing simple email"
echo $message->getHeaderValue('from');           // "[email protected]"
echo $message->getHeaderValue('date');            // "Sat, 22 Nov 2008 15:04:59 +1100"

// Get header attributes
echo $message->getHeaderAttribute('content-type', 'charset');  // "US-ASCII"

// Get message content
echo $message->getContents();

// From file path
$message = $parser->parse('/path/to/email.eml');

// From array of lines
$lines = file('email.eml', FILE_IGNORE_NEW_LINES);
$message = $parser->parse($lines);

// From iterator
$iterator = new \ArrayIterator($lines);
$message = $parser->parse($iterator);

$message = $parser->parse('multipart.eml');

// Check if message is multipart
if ($message->isMultiPart()) {
    echo "Content-Type: " . $message->getContentType();  // "multipart/mixed"

    // Get all parts
    $parts = $message->getParts();
    foreach ($parts as $part) {
        echo "Part: " . $part->getContentType() . "\n";
        echo "Content: " . $part->getContents() . "\n";
    }
}

$message = $parser->parse('email-with-attachments.eml');

// Get all attachments (non-recursive)
$attachments = $message->getAttachments();
foreach ($attachments as $attachment) {
    echo "Filename: " . $attachment->getHeaderAttribute('content-disposition', 'filename') . "\n";
    echo "Content-Type: " . $attachment->getContentType() . "\n";
    echo "Size: " . strlen($attachment->getContents()) . " bytes\n";
}

// Get all attachments recursively (including nested)
$allAttachments = $message->getAttachments(true);

$message = $parser->parse('nested-message.eml');

// Check if a part is a nested message
$parts = $message->getParts();
foreach ($parts as $part) {
    if ($part->isMessage()) {
        // This is a message/rfc822 part
        $nestedMessage = $part->getParts()[0];
        echo "Nested Subject: " . $nestedMessage->getHeaderValue('subject') . "\n";
    }
}

$message = $parser->parse('email.eml');

// Get a header object
$contentType = $message->getHeader('content-type');

// Get header value
echo $contentType->getValue();  // "text/plain; charset=UTF-8"

// Get all attributes
$attributes = $contentType->getAttributes();
// ['charset' => 'UTF-8', 'format' => 'flowed']

// Get specific attribute
echo $contentType->getAttribute('charset');  // "UTF-8"

$message = $parser->parse('complex-nested.eml');

// Get all parts recursively (including nested parts)
$allParts = $message->getParts(true);

// Get all attachments recursively (including from nested messages)
$allAttachments = $message->getAttachments(true);

$parser = new MessageParser();
$message = $parser->parse('simple-email.eml');

echo "Subject: " . $message->getHeaderValue('subject') . "\n";
echo "From: " . $message->getHeaderValue('from') . "\n";
echo "Body: " . $message->getContents() . "\n";

$parser = new MessageParser();
$message = $parser->parse('email-with-attachments.eml');

// Get text parts
$parts = $message->getParts();
foreach ($parts as $part) {
    if ($part->isText()) {
        echo "Text part: " . $part->getContents() . "\n";
    }
}

// Get attachments
$attachments = $message->getAttachments();
foreach ($attachments as $attachment) {
    $filename = $attachment->getHeaderAttribute('content-disposition', 'filename');
    file_put_contents($filename, $attachment->getContents());
}

$parser = new MessageParser();
$message = $parser->parse('complex-nested.eml');

// Traverse all parts recursively
function processPart($part, $level = 0) {
    $indent = str_repeat('  ', $level);
    echo $indent . "Type: " . $part->getContentType() . "\n";

    if ($part->isMultiPart()) {
        foreach ($part->getParts() as $subPart) {
            processPart($subPart, $level + 1);
        }
    }
}

processPart($message);