PHP and Mail

PHP has a variety of native functions to send mail out from a script, all based on and around the mail() function. There are various other libraries and scripts that exist to provide more features.

However, it’s not as easy to programmatically deal with emails via a PHP script. This is where the MailParse module from the PECL repository can be used (

This module provides a range of functions to deal with email. Using PHP’s stream interface, it can take an email and process it, correctly identifying each individual element of the mail and converting it into a native PHP array.

A snippet of the available headers is shown below.

[return-path] =>
[envelope-to] =>
[to] =>
[subject] => MailParse
[from] =>


The body of the email can be processed via arrays, with each part of the message having its form detected. This allows the developer to respond correctly to plain text emails, html emails, or base64 attachments.

Once a developer is familiar with the functions of the MailParse module, they will be able to process emails just as easily as they can send them.

This entry was posted in Development. Bookmark the permalink.

Comments are closed.