Installation
Run composer require c975l/email-bundle and enable the bundle in config/bundles.php (Laravel 5.5+) or AppKernel.php (older versions):
c975L\EmailBundle\c975LEmailBundle::class => ['all' => true],
Publish Configuration Publish the default config with:
php artisan vendor:publish --provider="c975L\EmailBundle\c975LEmailBundle" --tag="config"
This creates config/email_bundle.php. Configure:
store_emails).Run Migrations
php artisan migrate
This creates the emails table for storage.
First Email
Use the EmailService in a controller:
use c975L\EmailBundle\Service\EmailService;
public function sendWelcomeEmail()
{
$email = $this->emailService->create()
->setSubject('Welcome!')
->setTo('user@example.com')
->setTemplate('emails/welcome'); // Must exist in resources/views/
$this->emailService->send($email);
}
Sending Emails
$email = $this->emailService->create()
->setSubject('Hello')
->setFrom('no-reply@example.com')
->setTo(['user1@example.com', 'user2@example.com'])
->setTemplate('emails/generic')
->addAttachment('/path/to/file.pdf');
$this->emailService->send($email);
setTemplateData():
$email->setTemplateData(['name' => 'John']);
Storing Emails
store_emails: true in config. Emails auto-save to emails table.$sentEmails = $this->emailRepository->findBy(['status' => 'sent']);
Admin Dashboard
/email route (protected by ROLE_EMAIL_ADMIN).resources/views/email_bundle/index.html.twig) to customize the UI.Testing
EmailService in tests:
$emailService = $this->createMock(EmailService::class);
$emailService->expects($this->once())->method('send');
$this->app->instance(EmailService::class, $emailService);
send() in a job for async processing:
use c975L\EmailBundle\Job\SendEmailJob;
dispatch(new SendEmailJob($email));
resources/views/email_bundle/ (e.g., email.html.twig).email.sent/email.failed events:
Event::listen('email.sent', function ($email) {
Log::info("Email sent to {$email->getTo()}");
});
Template Paths:
resources/views/ (e.g., emails/welcome.html.twig).php artisan config:clear if paths aren’t detected.Role Configuration:
ROLE_EMAIL_ADMIN, ROLE_EMAIL_USER) may conflict with existing roles. Override in config/email_bundle.php:
'roles' => [
'admin' => 'ROLE_CUSTOM_ADMIN',
'user' => 'ROLE_CUSTOM_USER',
],
Database Storage:
store_emails permanently deletes the emails table. Backup first if unsure.Attachments:
storage_path():
$email->addAttachment(storage_path('app/files/report.pdf'));
APP_DEBUG=true) to see Symfony Mailer logs in storage/logs/.TemplateNotFoundException: Verify template paths and run composer dump-autoload.TransportException: Check Symfony Mailer config (e.g., SMTP credentials in .env).Custom Email Model:
Extend the Email entity (e.g., add metadata column):
// src/Entity/ExtendedEmail.php
use c975L\EmailBundle\Entity\Email as BaseEmail;
class ExtendedEmail extends BaseEmail {
/**
* @ORM\Column(type="json")
*/
private $metadata;
}
Update the bundle’s EmailType to include the new field.
Override Services:
Bind a custom EmailService in config/services.php:
c975L\EmailBundle\Service\EmailService::class => \App\Service\CustomEmailService::class,
API Endpoints:
Use the bundle’s EmailController as a base to add REST endpoints:
// routes/api.php
Route::post('/api/emails', [EmailController::class, 'sendApiEmail']);
How can I help you explore Laravel packages today?