PHP code example of opcodesio / mail-parser

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

    

opcodesio / mail-parser example snippets


use Opcodes\MailParser\Message;

// Parse a message from a string
$message = Message::fromString('...');
// Or from a file location (accessible with file_get_contents())
$message = Message::fromFile('/path/to/email.eml');

$message->getHeaders();                 // get all headers
$message->getHeader('Content-Type');    // 'multipart/mixed; boundary="----=_Part_1_1234567890"'
$message->getFrom();                    // 'Arunas <[email protected]>
$message->getTo();                      // 'John Doe <[email protected]>
$message->getSubject();                 // 'Subject line'
$message->getDate();                    // DateTime object when the email was sent
$message->getSize();                    // Email size in bytes

$message->getParts();       // Returns an array of \Opcodes\MailParser\MessagePart, which can be html parts, text parts, attachments, etc.
$message->getHtmlPart();    // Returns the \Opcodes\MailParser\MessagePart containing the HTML body
$message->getTextPart();    // Returns the \Opcodes\MailParser\MessagePart containing the Text body
$message->getAttachments(); // Returns an array of \Opcodes\MailParser\MessagePart that represent attachments

$messagePart = $message->getParts()[0];

$messagePart->getHeaders();                 // array of all headers for this message part
$messagePart->getHeader('Content-Type');    // value of a particular header
$messagePart->getContentType();             // 'text/html; charset="utf-8"'
$messagePart->getContent();                 // '<html><body>....'
$messagePart->getSize();                    // 312
$messagePart->getFilename();                // name of the file, in case this is an attachment part