laminas/laminas-mime
Laminas MIME component for handling MIME messages and parts. Note: this package is abandoned and will receive no further development. For an actively maintained alternative, consider symfony/mime or zbateson/mail-mime-parser.
Installation:
composer require laminas/laminas-mime
(Note: Due to the package being abandoned, prefer alternatives like symfony/mime for new projects.)
Basic Usage:
use Laminas\Mime\Message;
use Laminas\Mime\Part;
// Create a simple text part
$textPart = new Part('Hello, World!');
$textPart->type = 'text/plain';
$textPart->charset = 'UTF-8';
// Create a multipart message
$message = new Message();
$message->addPart($textPart);
// Output the raw MIME message
echo $message->generateMessage();
First Use Case:
$rawEmail = file_get_contents('email.eml');
$message = Message::fromString($rawEmail);
foreach ($message->getParts() as $part) {
if ($part->isAttachment()) {
echo 'Attachment: ' . $part->filename . "\n";
}
}
Part, Message, Header).Laminas\Mime\Message as a container for Part objects.$message = new Message();
$message->setEncoding('7bit'); // Default encoding for the message
// Add a text part
$textPart = new Part('Plain text content');
$textPart->type = 'text/plain';
$textPart->charset = 'UTF-8';
$message->addPart($textPart);
// Add an HTML part
$htmlPart = new Part('<b>HTML content</b>');
$htmlPart->type = 'text/html';
$htmlPart->charset = 'UTF-8';
$message->addPart($htmlPart);
// Generate the final MIME message
$rawMessage = $message->generateMessage();
Part::fromFile() or Part::fromString() for attachments.$attachment = new Part(file_get_contents('file.pdf'));
$attachment->type = 'application/pdf';
$attachment->filename = 'document.pdf';
$attachment->disposition = 'attachment';
$message->addPart($attachment);
Message::fromString() to parse raw MIME data.$rawEmail = file_get_contents('email.eml');
$message = Message::fromString($rawEmail);
foreach ($message->getParts() as $part) {
if ($part->type === 'text/html') {
echo $part->content; // HTML content
}
}
$message->getHeaders()->addHeaderLine('X-Custom-Header', 'value');
$encodedContent = $message->encodeContent($plainText, 'quoted-printable');
With Laravel Mail:
Use laminas/mime to customize MIME parts before passing them to Laravel’s MimeMessage:
use Laminas\Mime\Part;
use Illuminate\Mail\MimeMessage;
$part = new Part('<h1>Hello</h1>', 'text/html');
$mimeMessage = new MimeMessage();
$mimeMessage->withPart($part);
With File Uploads: Dynamically generate MIME parts from uploaded files:
$file = request()->file('attachment');
$part = new Part(file_get_contents($file->path()));
$part->filename = $file->getClientOriginalName();
$part->type = mime_content_type($file->path());
Streaming Large Files: Avoid loading entire files into memory by streaming content:
$part = new Part();
$part->setContentStream(function () {
yield file_get_contents('large-file.pdf');
});
$part->type = 'application/pdf';
Deprecation Warning:
symfony/mime or zbateson/mail-mime-parser.2.12.0) to avoid breaking changes.PHP Version Support:
Header Folding Issues:
Header::fold() explicitly:
$header = new Header\ContentType('text/plain; charset=UTF-8');
$header->fold(76); // Fold at 76 chars (RFC 2822 compliant)
Encoding Quirks:
quoted-printable encoding for large strings can be memory-intensive (fixed in v2.7.3, but still a risk for huge payloads).charset for non-ASCII content to avoid corruption.Boundary Generation:
$message->setBoundary('fixed-boundary-string');
Inspect Raw MIME Output:
Use generateMessage() to debug the final output:
echo htmlspecialchars($message->generateMessage());
Validate Headers: Check for malformed headers with:
$headers = $message->getHeaders();
foreach ($headers as $header) {
echo $header->getFieldName() . ': ' . $header->getFieldValue() . "\n";
}
Handle Encoding Errors: If content appears garbled, explicitly set the encoding:
$part->encoding = 'base64'; // Force base64 for binary data
Custom Part Types:
Extend Laminas\Mime\Part to add domain-specific logic:
class CustomPart extends Part {
public function isValid() {
return $this->type === 'application/vnd.custom';
}
}
Override Encoding: Replace the default encoder by injecting a custom strategy:
$message->setEncodingStrategy(function ($content, $encoding) {
return custom_encode($content, $encoding);
});
Hook into Message Generation:
Use events (if supported in your Laravel version) or wrap generateMessage():
$rawMessage = $message->generateMessage();
$rawMessage = preg_replace('/custom-pattern/', 'replacement', $rawMessage);
laminas/mime has no config file. All settings are runtime (e.g., boundaries, encodings).UTF-8, but explicitly set it for non-English content to avoid issues:
$part->charset = 'ISO-8859-1'; // For legacy systems
$part->setContentStream(function () use ($filePath) {
yield file_get_contents($filePath);
});
$cachedMessage = Message::fromString($rawEmail, ['cache' => true]);
How can I help you explore Laravel packages today?