codeconsortium/ccdn-message-bundle
Installation Add the bundle via Composer:
composer require codeconsortium/ccdn-message-bundle
Register the bundle in app/AppKernel.php:
new CodeConsortium\CCDNMessageBundle\CCDNMessageBundle(),
First Use Case Inject the message service into a controller or service:
use CodeConsortium\CCDNMessageBundle\Service\MessageService;
class MyController extends Controller
{
public function __construct(MessageService $messageService)
{
$this->messageService = $messageService;
}
public function sendMessageAction()
{
$this->messageService->send('recipient@example.com', 'Hello', 'Test message');
return new Response('Message sent!');
}
}
Configuration
Check app/config/config.yml for default settings (if any) or override in app/config/packages/ccdn_message.yml:
ccdn_message:
default_from: 'noreply@example.com'
transport: 'smtp' # or 'mail' for PHP's mail()
Sending Messages
Use the MessageService to send messages via different transports:
// SMTP
$this->messageService->send('user@example.com', 'Subject', 'Body');
// With attachments
$this->messageService->sendWithAttachment(
'user@example.com',
'Subject',
'Body',
['/path/to/file.pdf']
);
Templates & Dynamic Content Use Twig templates for dynamic messages:
$this->messageService->sendTemplate(
'user@example.com',
'emails/welcome.twig',
['name' => 'John']
);
Queueing Messages For async processing, integrate with Symfony’s Messenger component:
# config/packages/messenger.yaml
framework:
messenger:
transports:
ccdn_message: '%env(MESSENGER_TRANSPORT_DSN)%'
routing:
'CodeConsortium\CCDNMessageBundle\Message\SendMessage': ccdn_message
Event Listeners Extend functionality via events (e.g., logging, analytics):
// src/EventListener/MessageListener.php
class MessageListener
{
public function onMessageSent(SendMessageEvent $event)
{
// Custom logic (e.g., analytics)
}
}
Register in services.yaml:
services:
App\EventListener\MessageListener:
tags:
- { name: kernel.event_listener, event: ccdn.message.sent, method: onMessageSent }
Doctrine Integration Attach messages to entities via lifecycle callbacks:
// src/Entity/User.php
public function prePersist()
{
$this->messageService->sendWelcomeEmail($this->email);
}
Validation Validate recipient emails before sending:
use Symfony\Component\Validator\Constraints as Assert;
$email = 'user@example.com';
$validator = $this->validator;
$errors = $validator->validate($email, [
new Assert\Email(),
]);
Testing
Mock the MessageService in PHPUnit:
$mockService = $this->createMock(MessageService::class);
$mockService->expects($this->once())
->method('send')
->with('user@example.com', 'Subject', 'Body');
$controller = new MyController($mockService);
Deprecation Warnings
Missing Documentation
sendWithAttachment) may lack examples. Check the Service/MessageService.php source for undocumented features.Transport Configuration
config.yml:
ccdn_message:
transport: smtp
smtp_host: smtp.example.com
smtp_port: 587
smtp_user: user@example.com
smtp_password: '%env(SMTP_PASSWORD)%'
Event System Quirks
ccdn.message.sent may not be documented. Verify available events in Event/SendMessageEvent.php.Enable Debugging
Add logging to config.yml:
ccdn_message:
debug: true
Check logs in var/log/dev.log for transport errors.
Common Errors
AppKernel.php.smtp_port may be blocked).Extension Points
MessageService to add custom logic:
// src/Service/CustomMessageService.php
class CustomMessageService extends \CodeConsortium\CCDNMessageBundle\Service\MessageService
{
public function send($to, $subject, $body)
{
// Pre-process $body or $subject
parent::send($to, $subject, $body);
}
}
Register the override in services.yaml:
services:
CodeConsortium\CCDNMessageBundle\Service\MessageService: '@App\Service\CustomMessageService'
Bulk Sending Use batch processing to avoid rate limits:
$recipients = ['a@example.com', 'b@example.com'];
foreach ($recipients as $email) {
$this->messageService->send($email, 'Batch Subject', 'Batch Body');
}
Local Testing Use a local mail catcher (e.g., MailHog) for development:
ccdn_message:
transport: mail
mailer_path: '/usr/sbin/sendmail -t -i' # MailHog's sendmail path
Security
{{ message.body|e('html') }}
How can I help you explore Laravel packages today?