PHP Requires Chilkat v11.0.0+
PHP
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 Downloads
<?php
use Chilkat2\Email;
use Chilkat2\HtmlToText;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
$email = new Email();
try {
$email->LoadEml('with-attachments.eml');
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
echo 'From: ', $email->From, "\n";
echo 'Subject: ', $email->Subject, "\n";
echo 'Date: ', $email->EmailDateStr, "\n";
$i = 0;
while ($i < $email->NumTo) {
echo 'To: ', $email->GetToName($i), ' ', $email->GetToAddr($i), "\n";
$i = $i + 1;
}
$referenceId = $email->GetHeaderField('X-Reference-ID');
if ($referenceId !== null) {
echo 'Reference: ', $referenceId, "\n";
}
if ($email->HasPlainTextBody()) {
echo $email->GetPlainTextBody(), "\n";
} else {
if ($email->HasHtmlBody()) {
$reader = new HtmlToText();
echo $reader->ToText($email->GetHtmlBody()), "\n";
} else {
echo "No text or HTML body.\n";
}
}