accord/postmark-swiftmailer-bundle
Symfony2 bundle adding a Postmark transport for SwiftMailer. Configure your Postmark API key (and optional SSL) and set Swiftmailer transport to accord_postmark to send email through Postmark.
Install via Composer
composer require accord/postmark-swiftmailer-bundle:dev-master
(Note: Use a stable version if available; dev-master is unstable.)
Register the Bundle
Add to app/AppKernel.php:
new Accord\PostmarkSwiftMailerBundle\AccordPostmarkSwiftMailerBundle(),
Configure API Key
Add to app/config/config.yml:
accord_postmark_swift_mailer:
api_key: "%env(POSTMARK_API_KEY)%" # Use env vars for security
use_ssl: true
Set SwiftMailer Transport
swiftmailer:
transport: accord_postmark
First Test Send a test email in a controller:
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class MailController extends Controller
{
public function sendTestEmail()
{
$message = (new \Swift_Message('Test Email'))
->setFrom('sender@example.com')
->setTo('recipient@example.com')
->setBody('Hello from Postmark!');
$this->get('mailer')->send($message);
return new Response('Email sent!');
}
}
Configuration
config.yml or .env (recommended).use_ssl if needed (e.g., for testing with false).Email Sending
Mailer service (autowired via DI):
public function __construct(\Swift_Mailer $mailer) { ... }
$message = (new \Swift_Message('Subject'))
->setFrom('no-reply@domain.com')
->setTo(['user@example.com', 'admin@example.com'])
->setBody($htmlContent, 'text/html')
->addPart($plainText, 'text/plain');
$mailer->send($message);
Templates & Attachments
$message->attach(\Swift_Attachment::fromPath('/path/to/file.pdf'));
SwiftMailer:
{# templates/emails/welcome.html.twig #}
<p>Hello {{ user.name }}!</p>
$message->setBody(
$this->renderView('emails/welcome.html.twig', ['user' => $user]),
'text/html'
);
Batch Sending
SwiftMailer's batch() for bulk emails:
$mailer->batch(function (\Swift_Message $message, $failedRecipients) {
$message->setTo($recipient->email);
$message->setBody($this->renderTemplate($recipient));
}, $recipients);
Async Processing
$this->get('messenger')->dispatch(
new SendEmailMessage($message)
);
API Key Exposure
config.yml violates security best practices.%env(POSTMARK_API_KEY)%) and .env files.
# config/packages/accord_postmark.yaml
accord_postmark_swift_mailer:
api_key: "%env(POSTMARK_API_KEY)%"
SSL Warnings
use_ssl: false may trigger Postmark’s API warnings.use_ssl: true in production; use a local tunnel (e.g., ngrok) for testing.Rate Limits
$this->get('messenger')->dispatch(
new SendEmailMessage($message),
['delay' => 1000] // 1-second delay
);
Debugging Failures
config/packages/monolog.yaml:
handlers:
swiftmailer:
type: stream
path: "%kernel.logs_dir%/swiftmailer.log"
level: debug
Bundle Compatibility
Testing
accord_postmark_swift_mailer:
api_key: "YOUR_SANDBOX_KEY"
use_ssl: false
Swift_Mailer service in PHPUnit:
$this->mailer = $this->createMock(\Swift_Mailer::class);
$this->mailer->expects($this->once())
->method('send')
->with($this->isInstanceOf(\Swift_Message::class));
Performance
Extensions
$mailer->getEventDispatcher()->addListener(
'sent',
function (\Swift_Events_SendEvent $event) {
// Log sent emails
}
);
$mailer = new RetryMailerDecorator($this->mailer, 3);
Monitoring
// Configure webhook URL in Postmark dashboard
$message->getHeaders()->addTextHeader('X-Postmark-Webhook', 'https://your-app.com/webhook');
How can I help you explore Laravel packages today?