Sample code for 30+ languages & platforms
Perl 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 Perl Downloads

Perl
use strict;
use warnings;
use Chilkat2;

# Assumes Chilkat is unlocked. See Global Unlock Sample.

my $email = Chilkat2::Email->new;
my $success = $email->LoadEml('with-attachments.eml');
if (!$success) {
    print $email->LastErrorText, "\n";
    exit;
}

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

my $referenceId = $email->GetHeaderField('X-Reference-ID');
if ($email->LastMethodSuccess) {
    print 'Reference: ', $referenceId, "\n";
}

if ($email->HasPlainTextBody()) {
    print $email->GetPlainTextBody(), "\n";
} else {
    if ($email->HasHtmlBody()) {
        my $reader = Chilkat2::HtmlToText->new;
        print $reader->ToText($email->GetHtmlBody()), "\n";
    } else {
        print "No text or HTML body.\n";
    }
}