Installation Add the bundle to your Laravel project via Composer:
composer require black/email-bundle
Register the bundle in config/app.php under providers:
Black\EmailBundle\BlackEmailBundle::class,
Configuration Publish the default config:
php artisan vendor:publish --provider="Black\EmailBundle\BlackEmailBundle" --tag="config"
Edit config/black_email.php to define your email transport (e.g., SMTP, Mailgun, SendGrid).
First Use Case
Inject the Black\Email\EmailManager service into a controller or service:
use Black\Email\EmailManager;
public function sendWelcomeEmail(EmailManager $emailManager) {
$email = $emailManager->createEmail()
->setTo('user@example.com')
->setSubject('Welcome!')
->setBody('Hello, welcome!');
$emailManager->send($email);
}
Email Creation
Use the EmailManager to create emails with fluent methods:
$email = $emailManager->createEmail()
->setFrom('noreply@example.com')
->setTo(['user1@example.com', 'user2@example.com'])
->setCc('cc@example.com')
->setBcc('bcc@example.com')
->setSubject('Your Order Confirmation')
->setBody($this->renderView('emails.order_confirmation', ['order' => $order]));
Attachments Attach files dynamically:
$email->attachFromPath(storage_path('app/order.pdf'), 'order.pdf', 'application/pdf');
$email->attachFromString('Base64-encoded-content', 'inline.png', 'image/png');
Templates Use Laravel’s Blade for templating:
$email->setBody($this->renderView('emails.template', ['data' => $data]));
Queueing Emails Dispatch emails as jobs for async processing:
use Black\Email\Jobs\SendEmail;
SendEmail::dispatch($email)->onQueue('emails');
Black\Email\Email to create reusable email classes:
class WelcomeEmail extends Email {
public function __construct($user) {
$this->setTo($user->email)
->setSubject('Welcome, ' . $user->name);
}
}
$emailManager->send($email, function () {
// Post-send logic (e.g., analytics, logging)
});
EmailManager mock in PHPUnit:
$emailManager = $this->createMock(EmailManager::class);
$emailManager->expects($this->once())->method('send');
$this->app->instance(EmailManager::class, $emailManager);
Version Instability
Avoid @stable; pin to a specific version (e.g., 1.0.0) to prevent breaking changes. Check the releases for updates.
Missing Dependencies
Ensure black/email (the underlying component) is installed:
composer require black/email
Queue Configuration
If using queues, ensure the emails queue exists in .env:
QUEUE_CONNECTION=database
QUEUE_DEFAULT=emails
Attachment Limits
Large attachments may hit PHP’s post_max_size or upload_max_filesize. Adjust in php.ini or use cloud storage (e.g., S3) for binaries.
Log Emails: Enable logging in config/black_email.php:
'logging' => true,
Logs will appear in storage/logs/laravel.log.
Validate Recipients:
Use setTo() with an array to avoid typos:
$email->setTo(['valid@example.com']); // Fails silently on invalid addresses
Custom Transports
Extend Black\Email\Transport\TransportInterface to add new providers (e.g., AWS SES):
class AWSTransport implements TransportInterface {
public function send(Email $email) { /* ... */ }
}
Register in config/black_email.php:
'transports' => [
'aws' => Black\Email\Transport\AWSTransport::class,
],
Email Events
Listen for email.sent or email.failed events:
Event::listen('email.sent', function ($email) {
// Track sent emails in a database
});
Fallback Transports
Configure a fallback transport in config/black_email.php:
'fallback_transport' => 'smtp',
Ensures emails are sent even if the primary transport fails.
// config/black_email.php
'transport' => env('MAIL_TRANSPORT', 'smtp'),
$emailManager->send($email, function () use ($user) {
$user->increment('emails_sent');
});
log transport for testing:
'transport' => 'log', // Logs emails to storage/logs/black_email.log
How can I help you explore Laravel packages today?