Sample code for 30+ languages & platforms
PHP ActiveX Requires Chilkat v11.0.0+

Read email headers and display the body as text

See more Email Object Examples

Read the sender, recipients, subject, date, and an optional custom header from an EML file. Display the plain-text body when available, or convert HTML to text. Handle messages that contain neither body format.

Chilkat PHP ActiveX Downloads

PHP ActiveX
<?php

$success = 0;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

$email = new COM("Chilkat.Email");
$success = $email->LoadEml('with-attachments.eml');
if ($success != 1) {
    print $email->LastErrorText . "\n";
    exit;
}

print 'From: ' . $email->From . "\n";
print 'Subject: ' . $email->Subject . "\n";
print 'Date: ' . $email->EmailDateStr . "\n";
$i = 0;
while (($i < $email->NumTo)) {
    print 'To: ' . $email->getToName($i) . '  ' . $email->getToAddr($i) . "\n";
    $i = $i + 1;
}

$referenceId = $email->getHeaderField('X-Reference-ID');
if ($email->LastMethodSuccess == 1) {
    print 'Reference: ' . $referenceId . "\n";
}

if ($email->HasPlainTextBody() == 1) {
    print $email->getPlainTextBody() . "\n";
}
else {
    if ($email->HasHtmlBody() == 1) {
        $reader = new COM("Chilkat.HtmlToText");
        print $reader->toText($email->getHtmlBody()) . "\n";
    }
    else {
        print 'No text or HTML body.' . "\n";
    }

}


?>