PHP code example of melloww / laravel-mailfiles

1. Go to this page and download the library: Download melloww/laravel-mailfiles 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/ */

    

melloww / laravel-mailfiles example snippets


use Melloww\MailFiles\MailFile;

$email = MailFile::read('/path/to/message.msg'); // or message.eml — auto-detected

$email->subject();        // "Quarterly report"
$email->from();           // Address { name: "Jane Doe", email: "[email protected]" }
$email->to();             // list<Address>
$email->date();           // DateTimeImmutable
$email->htmlBody();       // "<p>…</p>"
$email->attachments();    // list<Attachment>

use Melloww\MailFiles\MailFile;          // static helper (works anywhere)
use Melloww\MailFiles\Facades\MailFiles; // Laravel facade (container-managed)

$email = MailFile::read($path);                 // detect format from the bytes
$email = MailFile::fromString($contents);       // from an in-memory string
$email = MailFile::eml($path);                   // force the EML parser
$email = MailFile::msg($path);                   // force the MSG parser

$email = MailFiles::read($path);                 // same thing, via the facade
$email = MailFiles::readUploadedFile($request->file('mail')); // Laravel upload

$email->inReplyTo();      // parent Message-ID
$email->references();     // list<string> of the thread's Message-IDs
$email->priority();       // Priority::High | Normal | Low
$email->sensitivity();    // Sensitivity::Normal | Personal | Private | Confidential
$email->isAutoSubmitted();// auto-reply / bulk / list (for out-of-office notices)
$email->returnPath();     // ?Address
$email->deliveredTo();    // ?string
$email->listId();         // ?string
$email->listUnsubscribe();// list<string> (mailto:/https: endpoints)
$email->wantsReadReceipt();
$email->readReceiptTo();  // list<Address>
$email->authenticationResults();
$email->spfResult();      // "pass" | "fail" | "softfail" | ...
$email->dkimResult();
$email->dmarcResult();

use Melloww\MailFiles\Enums\BodyFormat;

$email->htmlBody();                    // raw HTML
$email->body();                        // plain text (text part, or HTML stripped)
$email->markdown();                    // "# Heading\n\nHello **world** …"
$email->bodyAs(BodyFormat::Markdown);  // same, choosing the format dynamically

foreach ($email->attachments() as $attachment) {
    $attachment->filename();        // "invoice.pdf"
    $attachment->contentType();     // "application/pdf" — as declared (may be null)
    $attachment->mimeType();        // declared type, else detected from the bytes.
    $attachment->extension();       // "pdf"
    $attachment->size();            // 48213 (bytes)
    $attachment->humanSize();       // "47.1 KB"
    $attachment->disposition();     // AttachmentDisposition::Attachment
    $attachment->isInline();        // false
    $attachment->isEmbeddedImage(); // false
    $attachment->contentId();       // null, or the cid for inline images
    $attachment->content();         // the raw decoded bytes
    $attachment->saveTo('/tmp/'.$attachment->filename());
}

$email->attachments();       // the PDF the customer actually sent
$email->inlineAttachments(); // the LinkedIn/Twitter icons + logo + tracking pixel

$attachment->disposition();     // AttachmentDisposition::Attachment | ::Inline
$attachment->isEmbeddedImage(); // true for an inline image

foreach ($email->attachments() as $attachment) {
    if ($attachment->isEmbeddedMessage()) {
        $inner = $attachment->embeddedMessage();   // a full Email instance
        $inner->subject();
        $inner->attachments();                      // …with its own attachments
    }
}

$email->embeddedMessages();         // list<Email> — just the nested messages
$email->allAttachmentsRecursive();  // every file across every nesting level

$email->header('Message-ID');     // first value, case-insensitive
$email->headers()->all('Received'); // every value of a repeated header
$email->headers()->toArray();       // name => first value

$from = $email->from();
$from->name;          // "Jane Doe"
$from->email;         // "[email protected]"
$from->displayName(); // name, or email if there is no name
(string) $from;       // '"Jane Doe" <[email protected]>'

$email = MailFile::read('fwd.eml');

$email->isForwarded();        // true
$email->isReply();            // false

// The original author + who it was originally addressed to:
$email->originalSender();     // Address { "Alice Original", [email protected] }
$email->originalRecipients(); // [Address [email protected]]

// Who it was forwarded TO is simply the current recipient:
$email->to();                 // [Address [email protected]]

// The full chain, newest-first, with per-message contacts:
foreach ($email->thread()->messages() as $msg) {
    $msg->type();     // ThreadEntryType::Message | Forwarded | Reply | Attached
    $msg->from();     // Address|null
    $msg->to();       // list<Address>
    $msg->cc();
    $msg->subject();
    $msg->date();
}

$email->isSigned();       // multipart/signed (S/MIME or PGP)
$email->isEncrypted();    // multipart/encrypted or S/MIME enveloped
$email->securityProtocol();// "smime" | "pgp" | null

if ($email->hasCalendar()) {
    $event = $email->calendarEvents()[0];
    $event->method();     // REQUEST | CANCEL | REPLY | ...
    $event->summary();
    $event->organizer();  // ?Address
    $event->attendees();  // list<Address>
    $event->start(); $event->end();
    $event->isCancellation();
}

$email->isDeliveryStatusNotification(); // a bounce/DSN
$email->isReadReceipt();                // an MDN
$email->hasTnef();                      // carries winmail.dat
$email->tnefAttachment();               // the raw TNEF bytes, if any

foreach (MailFile::mbox('/var/mail/archive.mbox') as $email) {
    echo $email->subject().PHP_EOL;
}

foreach (MailFile::directory('/inbox', recursive: true) as $path => $email) {
    // ...
}

  $email = MailFile::headers('/inbox/msg-42.eml'); // or ::headers() on a .msg
  $email->isHeadersOnly();  // true
  $email->subject(); $email->from(); $email->date(); $email->inReplyTo();
  $email->body();           // null — reparse with MailFile::read() for the body
  

use Melloww\MailFiles\MailFileReader;

class InboxController
{
    public function show(MailFileReader $reader, Request $request)
    {
        $email = $reader->readUploadedFile($request->file('mail'));

        return response()->json([
            'subject' => $email->subject(),
            'from'    => $email->from()?->toArray(),
            'to'      => array_map(fn ($a) => $a->toArray(), $email->to()),
            'body'    => $email->cleanHtml() ?? $email->body(),
        ]);
    }
}

'eml' => [
    'driver' => env('MAILFILES_EML_DRIVER', 'native'), // native | mailparse | auto
],

'attachments' => [
    'extract_nested' => env('MAILFILES_EXTRACT_NESTED', true), // parse embedded .eml/.msg
    'max_depth'      => env('MAILFILES_MAX_DEPTH', 5),          // how deep to recurse
],

use Melloww\MailFiles\MailFileReader;
use Melloww\MailFiles\ParseOptions;

$reader = new MailFileReader('native', new ParseOptions(
    extractNestedMessages: true,
    maxDepth: 3,
));
bash
php artisan vendor:publish --tag="mailfiles-config"