Installation:
composer require cnerta/mailing-bundle:dev-master
Register the bundle in config/bundles.php (Symfony 4+):
return [
// ...
Cnerta\MailingBundle\CnertaMailingBundle::class => ['all' => true],
];
Configuration (config/packages/cnerta_mailing.yaml):
cnerta_mailing:
default_bundle: "AppBundle" # Replace with your bundle name
active_log: true
from_email:
address: "noreply@example.com"
name: "Your App"
First Email:
templates/emails/your_email.html.twig.use Cnerta\MailingBundle\Service\MailingService;
class YourController extends AbstractController {
public function sendEmail(MailingService $mailingService) {
$mailingService->sendEmail(
'your_email', // Template name (without extension)
['to' => 'user@example.com'],
['name' => 'John'] // Template variables
);
}
}
Template Organization:
templates/emails/ (e.g., welcome.html.twig)..twig) in sendEmail().Dynamic Recipients:
$recipients = ['user1@example.com', 'user2@example.com'];
$mailingService->sendEmail('newsletter', ['to' => $recipients], ['content' => $data]);
Attachments:
$mailingService->sendEmail(
'invoice',
['to' => 'client@example.com'],
['amount' => 100],
['attachments' => ['/path/to/invoice.pdf']]
);
Async Sending (if supported):
$mailingService->sendEmailAsync('reminder', ['to' => 'user@example.com'], []);
SwiftMailer Integration:
Configure swiftmailer in config/packages/mailer.yaml to ensure compatibility:
framework:
mailer:
dsn: '%env(MAILER_DSN)%'
Event Listeners:
Extend functionality by subscribing to cnerta.mailing.send events (if documented).
Testing:
Use Symfony’s Mailer component to mock emails in tests:
$this->mailer->assertSentCount(1);
Deprecated Symfony2:
AbstractController or Mailer may not work directly. Use SwiftMailer directly if needed:
$this->get('mailer')->send($message);
Template Paths:
templates/emails/ under the configured default_bundle. Misplaced templates will throw errors.No Laravel Port:
spatie/laravel-mailables (for templates).laravel-notification-channels/mail (for notifications).Logging:
active_log: true logs emails to var/log/cnerta_mailing.log. Disable in production if sensitive data is sent.tail -f var/log/cnerta_mailing.log
$mailingService->renderTemplate('your_email', ['name' => 'Test']);
Custom Services:
Extend Cnerta\MailingBundle\Service\MailingService to add features (e.g., rate limiting).
Override Templates: Use Symfony’s template inheritance to override default emails.
SwiftMailer Plugins: Integrate custom SwiftMailer plugins for advanced features (e.g., HTML parsing).
If migrating to Laravel, consider:
php artisan make:mail OrderShippedphp artisan make:notification OrderShippedspatie/laravel-mailables, laravel-notification-channels/mail.How can I help you explore Laravel packages today?