symfony/postmark-mailer
Symfony Mailer transport for Postmark. Send transactional email through Postmark using Symfony’s mailer API, with straightforward configuration and support for Postmark-specific options and headers. Ideal for Symfony apps needing reliable delivery and tracking.
Installation
composer require symfony/mailer symfony/postmark-mailer
Add to config/mail.php:
'transport' => [
'postmark' => [
'server' => env('POSTMARK_SERVER_TOKEN'),
],
],
First Use Case
Inject Symfony\Component\Mailer\MailerInterface into a controller/service:
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mailer\Envelope;
public function sendWelcomeEmail(MailerInterface $mailer)
{
$email = (new Email())
->from('noreply@example.com')
->to('user@example.com')
->subject('Welcome!')
->html('<h1>Hello!</h1>');
$mailer->send($email);
}
Environment Setup
Set POSTMARK_SERVER_TOKEN in .env:
POSTMARK_SERVER_TOKEN=your_token_here
Transactional Emails Use for password resets, notifications, and alerts:
$mailer->send((new Email())
->to($user->email)
->subject('Reset Password')
->text('Click here to reset: ' . route('password.reset', ['token' => $token]))
);
Templates & Variables Leverage Postmark’s templates with dynamic content:
$email = (new Email())
->to('user@example.com')
->subject('Your Order Confirmation')
->html('<p>Order #{{orderId}} confirmed!</p>', [
'orderId' => $order->id,
]);
Attachments & Inline Images
$email = (new Email())
->attachFromPath('/path/to/file.pdf')
->embed($mailer->embedFromPath('/path/to/image.png'))
->html('<img src="cid:embedded-image">');
Bulk Emails
Use MailerInterface::send() in batches with foreach loops:
foreach ($users as $user) {
$mailer->send((new Email())
->to($user->email)
->subject('Weekly Digest')
->html($digestHtml)
);
}
Async Processing Queue emails with Laravel’s queue system:
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
class SendWelcomeEmail implements ShouldQueue
{
use Queueable;
public function handle(MailerInterface $mailer)
{
$mailer->send((new Email())->to('user@example.com')->subject('Welcome!'));
}
}
Token Security
POSTMARK_SERVER_TOKEN in code. Always use .env.send, templates).Template Mismatches
{{variable}} syntax. Laravel’s Email class uses PHP arrays for replacements. Ensure consistency:
// Correct: Matches Postmark template syntax
$email->html('<p>{{name}}</p>', ['name' => 'John']);
Rate Limits
Debugging
config/mail.php:
'debug' => env('MAIL_DEBUG', false),
tail -f storage/logs/laravel.log | grep "PostmarkTransport"
Character Encoding
Webhook Integration Use Postmark’s delivery notifications to track opens/clicks:
// In a Laravel controller
public function handlePostmarkWebhook(Request $request)
{
$event = $request->input('Event');
if ($event === 'Delivery') {
// Log or process delivery data
}
}
Testing
Use Postmark’s sandbox mode (via token) or Laravel’s MailFake for testing:
// config/mail.php (for testing)
'transport' => [
'postmark' => [
'server' => env('POSTMARK_SANDBOX_TOKEN'),
],
],
Performance
$mailer->send($email1);
$mailer->send($email2);
// ... up to 100 emails per batch (Postmark's recommended limit)
Custom Headers Add headers for tracking or custom logic:
$email = (new Email())
->to('user@example.com')
->getHeaders()->addTextHeader('X-Custom-ID', $user->id);
Fallback Transport
Configure a fallback transport in config/mail.php for high availability:
'transport' => [
'postmark' => [
'server' => env('POSTMARK_TOKEN'),
],
'fallback' => [
'dsn' => env('MAILER_DSN_FALLBACK'),
],
],
How can I help you explore Laravel packages today?