nette/mail
Nette Mail is a PHP library for composing and sending emails. Build MIME messages with text/HTML bodies, attachments, and embedded images, set headers and recipients, and send via SMTP or native mail() with a simple, reliable API.
Installation
composer require nette/mail
Ensure PHP ≥8.2 (or use nette/mail:^3.1 for PHP 8.0+).
First Use Case: Send a Text Email
use Nette\Mail\Message;
use Nette\Mail\SmtpMailer;
$mailer = new SmtpMailer(
host: 'smtp.example.com',
port: 587,
username: 'user@example.com',
password: 'password',
encryption: 'tls'
);
$message = (new Message())
->setFrom('from@example.com')
->addTo('to@example.com')
->setSubject('Test Email')
->setBody('Hello, this is a test email.');
$mailer->send($message);
Key Files to Reference
Message: Core email composition.SmtpMailer: SMTP transport.CssInliner: CSS-to-inline conversion.Extend Laravel’s Mail facade to use Nette’s Message:
// app/Providers/AppServiceProvider.php
use Illuminate\Support\Facades\Mail;
use Nette\Mail\Message as NetteMessage;
Mail::macro('nette', function ($callback) {
$message = new NetteMessage();
$callback($message);
return $message;
});
Usage:
Mail::nette(function ($message) {
$message->setFrom('from@example.com')
->addTo('to@example.com')
->setSubject('Hello')
->setHtmlBody('<h1>Hi!</h1>');
});
Render Blade templates directly in setHtmlBody():
$message->setHtmlBody(view('emails.welcome', [
'user' => $user,
'buttonUrl' => route('dashboard'),
]));
$message->setHtmlBody('<h1>HTML Content</h1>')
->setTextBody('Plaintext alternative')
->setPriority(Message::PRIORITY_HIGH);
$message->createAttachment(
file: storage_path('file.pdf'),
mimeType: 'application/pdf',
fileName: 'document.pdf',
disposition: 'attachment'
);
$message->addInlinePart(
file: storage_path('logo.png'),
cid: 'logo'
);
$message->setHtmlBody('<img src="cid:logo" alt="Logo">');
use Nette\Mail\CssInliner;
$inliner = new CssInliner(['outlook' => true]);
$message->setHtmlBody($inliner->inline($htmlContent));
Configure in config/mail.php:
'dkim' => [
'domain' => env('MAIL_DKIM_DOMAIN'),
'selector' => env('MAIL_DKIM_SELECTOR'),
'privateKey' => env('MAIL_DKIM_PRIVATE_KEY_PATH'),
],
Apply to mailer:
$mailer = new SmtpMailer(/* ... */);
$mailer->setDkimSigner(
domain: config('mail.dkim.domain'),
selector: config('mail.dkim.selector'),
privateKey: file_get_contents(config('mail.dkim.privateKey'))
);
Combine multiple transports:
use Nette\Mail\FallbackMailer;
$fallbackMailer = new FallbackMailer(
primary: new SmtpMailer(/* ... */),
fallback: new SendmailMailer('/usr/sbin/sendmail')
);
PHP Version Mismatch
nette/mail:^4.0 requires PHP ≥8.2. Use ^3.1 for PHP 8.0+.SMTP Timeouts
setTimeout():
$mailer->setTimeout(30); // 30 seconds
CSS Inlining Quirks
<style> tags. Use CssInliner with outlook: true:
$inliner = new CssInliner(['outlook' => true]);
DKIM Key Permissions
chmod 640 private.key
chown www-data:www-data private.key
Attachment Encoding
encodeHeader():
$message->createAttachment(
file: storage_path('résumé.pdf'),
fileName: 'resume.pdf' // Manually encode if needed
);
Sendmail Path Issues
/usr/sbin/sendmail exists. Use absolute paths:
new SendmailMailer('/usr/sbin/sendmail -t -i');
Log Raw Emails Intercept messages before sending:
$mailer->onSend[] = function ($message) {
file_put_contents(
storage_path('debug/email.eml'),
$message->toString()
);
};
SMTP Debugging Enable verbose output:
$mailer = new SmtpMailer(/* ... */);
$mailer->setDebugMode(true);
Validate MIME Structure
Use Message::toString() to inspect raw output:
echo $message->toString();
Custom Mailers
Extend Nette\Mail\IMailer for unique transports (e.g., AWS SES):
class SesMailer implements IMailer {
public function send(Message $message): void {
// AWS SES logic
}
}
Pre-Send Hooks Modify messages dynamically:
$mailer->onSend[] = function ($message) {
$message->addHeader('X-Custom', 'Value');
};
Override CssInliner
Customize CSS processing:
class CustomInliner extends CssInliner {
protected function processRule(string $rule): string {
// Custom logic
}
}
Mail::queue(function ($message) {
$message->setFrom('from@example.com')
->addTo('to@example.com')
->setSubject('Batch Email');
});
CssInliner for performance-critical emails.How can I help you explore Laravel packages today?