PHP code example of erseco / mime-mail-parser

1. Go to this page and download the library: Download erseco/mime-mail-parser 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/ */

    

erseco / mime-mail-parser example snippets


use Erseco\Message;

// Parse a message from a string
$rawEmail = file_get_contents('/path/to/email.eml');
$message = Message::fromString($rawEmail);

// Or parse from a file directly
$message = Message::fromFile('/path/to/email.eml');

$message->getHeaders();                 // get all headers as array
$message->getHeader('Content-Type');    // get specific header
$message->getContentType();             // 'multipart/mixed; boundary="----=_Part_1_1234567890"'
$message->getFrom();                    // 'Service <[email protected]>'
$message->getTo();                      // 'John Doe <[email protected]>'
$message->getSubject();                 // 'Subject line'
$message->getDate();                    // DateTime object when the email was sent

$message->getParts();       // Returns array of MessagePart objects
$message->getHtmlPart();    // Returns MessagePart with HTML content
$message->getTextPart();    // Returns MessagePart with Text content
$message->getAttachments(); // Returns array of attachment MessageParts

// Working with message parts
$parts = $message->getParts();
$firstPart = $parts[0];

$firstPart->getHeaders();                 // array of all headers for this part
$firstPart->getHeader('Content-Type');    // get specific header
$firstPart->getContentType();             // 'text/html; charset="utf-8"'
$firstPart->getContent();                 // '<html><body>....'
$firstPart->isHtml();                     // true if it's an HTML part
$firstPart->isText();                     // true if it's a text part
$firstPart->isAttachment();               // true if it's an attachment
$firstPart->getFilename();                // name of the file if attachment
$firstPart->getSize();                    // size of content in bytes