Skip to content

A fully tested email parser for PHP 8.0+ (mailparse extension wrapper).

Notifications You must be signed in to change notification settings

ProtonMail/php-mime-mail-parser

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

php-mime-mail-parser

Fully Tested Mailparse Extension Wrapper for PHP 5.4+

Latest Version Total Downloads Software License

Is it reliable ?

Yes, it is.
All the issues are reproduced, fixed and tested.

Build Status Coverage Quality Score

How to install ?

Easy way with Composer ;)

To install this library, run the command below and you will get the latest version

composer require php-mime-mail-parser/php-mime-mail-parser

Requirements

The following versions of PHP are supported by this version.

  • PHP 5.4
  • PHP 5.5
  • PHP 5.6
  • PHP 7
  • HHVM

Make sure you have the mailparse extension (http://php.net/manual/en/book.mailparse.php) properly installed :

pecl install mailparse			#PHP Version = 7
pecl install mailparse-2.1.6		#PHP Version < 7

Take a look at this tutorial if you find it's difficult to install mailparse on Ubuntu.

Also note that you may need to create the 'mailparse.ini' file with 'extension=mailparse.so' inside under '/etc/php5/mods-available/' and then 'sudo php5enmod mailparse' to enable it.

How to use it ?

<?php
//We need to add the library first !
require_once __DIR__.'/vendor/autoload.php';

$path = 'path/to/mail.txt';
$Parser = new PhpMimeMailParser\Parser();

//There are three input methods of the mime mail to be parsed
//specify a file path to the mime mail :
$Parser->setPath($path); 

// Or specify a php file resource (stream) to the mime mail :
$Parser->setStream(fopen($path, "r"));

// Or specify the raw mime mail text :
$Parser->setText(file_get_contents($path));

// We can get all the necessary data
$to = $Parser->getHeader('to'); // "test" <[email protected]>, "test2" <[email protected]>
$addressesTo = $Parser->getAddresses('to'); //Return an array : [[test, [email protected], false],[test2, [email protected], false]]
$from = $Parser->getHeader('from'); // "test" <[email protected]>
$addressesFrom = $Parser->getAddresses('from'); //Return an array : test, [email protected], false
$subject = $Parser->getHeader('subject');

$text = $Parser->getMessageBody('text');
$html = $Parser->getMessageBody('html');
$htmlEmbedded = $Parser->getMessageBody('htmlEmbedded'); //HTML Body included data

// and the attachments also
$attach_dir = '/path/to/save/attachments/';
$Parser->saveAttachments($attach_dir);

// loop the attachments
$attachments = $Parser->getAttachments();
if (count($attachments) > 0) {
	foreach ($attachments as $attachment) {
		echo 'Filename : '.$attachment->getFilename().'<br />'; // logo.jpg
		echo 'Filesize : '.filesize($attach_dir.$attachment->getFilename()).'<br />'; // 1000
		echo 'Filetype : '.$attachment->getContentType().'<br />'; // image/jpeg
	}
}

?>

Contributing ?

Feel free to contribute.
To add issue, please provide the raw email with it.

License

The php-mime-mail-parser/php-mime-mail-parser is open-sourced software licensed under the MIT license

About

A fully tested email parser for PHP 8.0+ (mailparse extension wrapper).

Resources

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 99.8%
  • Shell 0.2%