pear/mail_mime
PEAR Mail_mime builds MIME-encoded email messages in PHP. Create multipart/alternative and mixed emails with plain text and HTML bodies, add attachments and inline images, and generate headers and bodies ready to send via Mail or other transports.
Install via Composer: composer require pear/mail_mime. This is a legacy PEAR package, so verify your environment supports it (PHP 5.x–7.4; not compatible with PHP 8+ in most cases). Begin by instantiating Mail_Mime, then set text/HTML bodies and add attachments:
use Mail_Mime;
$message = new Mail_Mime("\n");
$message->setTXTBody('Plain text version');
$message->setHTMLBody('<p>HTML <strong>version</strong></p>');
$message->addAttachment('report.pdf', 'application/pdf', 'Monthly Report');
$body = $message->get();
$headers = $message->getHeaders([
'From' => 'sender@example.com',
'To' => 'recipient@example.com',
'Subject' => 'Your Report',
]);
Send using PEAR::Mail (Mail::factory('smtp')), or wrap $body + $headers into Symfony Mailer or PHPMailer if needed. Start with the examples/ directory in the package source — it’s more reliable than sparse online docs.
setTXTBody() and setHTMLBody() for accessibility and spam score — Mail_Mime auto-generates multipart/alternative boundaries correctly.addHTMLImage($path, $mimetype, $cid) and reference <img src="cid:$cid"> in HTML body. Critical: CID must match exactly — no extra quotes or casing differences.addFile($filepath, $mimetype, $filename) over addAttachment($content, ...) to avoid memory exhaustion on big attachments.Reply-To, List-Unsubscribe) as key-value to getHeaders(). Important: Manually encode non-ASCII header values using mb_encode_mimeheader() first.Mail_Mime only for composition, then inject $body and $headers into Symfony’s RawMessage or PHPMailer for sending — avoids tight coupling to deprecated transports.\n is safe for mail() and most PHP mail functions, but SMTP requires \r\n — use Mail_Mime::CRLF only when sending via Net_SMTP or configured PEAR::Mail SMTP transport.Subject, From, To headers must be manually encoded (e.g., mb_encode_mimeheader($subject, 'UTF-8', 'B')) — unencoded non-ASCII chars break delivery.$body to debug — it’s opaque and fragile.Mail, Net_SMTP, PEAR core — ensure no conflicts with Composer-installed packages (e.g., php-pear/pear-core-minimal may be needed).Mail_Mime::setDebug(true) or override Mail_Mime::setDebugCallback() to capture verbose logging for troubleshooting misrendered emails.$HTTP_RAW_POST_DATA), and PEAR autoloader incompatibility — avoid in new environments.How can I help you explore Laravel packages today?