cgonser/swiftmailer-database-s3-spool-bundle
Install via Composer:
composer require cgonser/swiftmailer-database-s3-spool-bundle:dev-master
(Note: Use dev-master as the package is outdated; pin to a specific commit if needed.)
Enable the Bundle:
Add to config/bundles.php (Symfony 4+):
return [
// ...
Cgonser\SwiftMailerDatabaseS3SpoolBundle\CgonserSwiftMailerDatabaseS3SpoolBundle::class => ['all' => true],
];
Configure AWS SDK: Install the AWS SDK:
composer require aws/aws-sdk-php
Configure credentials in config/packages/aws.yaml:
aws:
credentials:
key: '%env(AWS_ACCESS_KEY_ID)%'
secret: '%env(AWS_SECRET_ACCESS_KEY)%'
region: '%env(AWS_DEFAULT_REGION)%'
Configure the Bundle:
Add to config/packages/cgonser_swift_mailer_database_s3_spool.yaml:
cgonser_swift_mailer_database_s3_spool:
s3:
bucket: 'your-bucket-name'
region: '%env(AWS_DEFAULT_REGION)%'
folder: 'emails' # Optional
First Use Case: Replace your SwiftMailer transport with the bundle’s spool:
# config/packages/mailer.yaml
framework:
mailer:
transports:
async_s3:
spool: { type: 'database_s3' }
default_transport: 'async_s3'
Spooling Emails:
emails/1234567890_attachment.pdf).Sending Emails:
use Cgonser\SwiftMailerDatabaseS3SpoolBundle\Command\SendEmailsCommand;
// In a custom command or service:
$sender = $this->get('cgonser_swift_mailer_database_s3_spool.sender');
$sender->sendQueuedEmails(10); // Send up to 10 emails
Integration with SwiftMailer:
DatabaseS3Spool transport.Mailer service is configured to use this transport:
$mailer->getTransport()->start();
Attachment Handling:
Email entity if needed):
/**
* @ORM\Entity
*/
class Email {
// ...
private $attachmentPaths = []; // Array of S3 paths (e.g., ['emails/123_attachment.pdf'])
}
Retry Logic:
sent_at and failed_at column).sent_at IS NULL and retry them.Dynamic S3 Buckets/Folders:
S3Client service to dynamically resolve buckets/folders:
# config/services.yaml
services:
Cgonser\SwiftMailerDatabaseS3SpoolBundle\S3\S3Client:
arguments:
$bucket: '%env(S3_BUCKET_DYNAMIC)%'
Custom Email Entity:
Email entity to add custom fields (e.g., template_id, priority):
use Cgonser\SwiftMailerDatabaseS3SpoolBundle\Entity\Email as BaseEmail;
class CustomEmail extends BaseEmail {
private $templateId;
// ...
}
EmailRepository to work with your entity.Batching:
$sender->sendQueuedEmails(50); // Send 50 emails at a time
Logging:
$logger = $this->get('logger');
$logger->info('Sent email', ['email_id' => $email->getId()]);
Testing:
$this->mock(S3Client::class)
->shouldReceive('putObject')
->once();
Outdated Package:
S3 Permissions:
s3:PutObject and s3:GetObject permissions for the bucket/folder.Database Schema:
emails table with attachment_paths). Migrations may fail if the schema drifts.php bin/console doctrine:schema:dump-sql --em=default
Attachment Corruption:
Memory Limits:
memory_limit in php.ini or stream attachments instead of loading them entirely into memory.SwiftMailer Version:
Enable Debugging:
Swift_Events_SendEvent to log all sent emails:
Swift_Events_SendEvent::addListener(function ($event) {
\Log::debug('Email sent', ['subject' => $event->getMessage()->getSubject()]);
});
Check S3 Files:
aws s3 ls s3://your-bucket/emails/ --recursive
Database Queries:
# config/packages/doctrine.yaml
doctrine:
dbal:
logging: true
profiling: true
Symfony Debug Toolbar:
DatabaseS3Spool transport and email entities.Custom Spool Logic:
DatabaseS3Spool class to add pre-send hooks (e.g., validate recipients):
namespace App\SwiftMailer;
use Cgonser\SwiftMailerDatabaseS3SpoolBundle\Spool\DatabaseS3Spool as BaseSpool;
class CustomDatabaseS3Spool extends BaseSpool {
public function send(Swift_Message $message) {
// Custom logic (e.g., validate message)
parent::send($message);
}
}
config/services.yaml:
services:
App\SwiftMailer\CustomDatabaseS3Spool: ~
Cgonser\SwiftMailerDatabaseS3SpoolBundle\Spool\DatabaseS3Spool:
alias: 'App\SwiftMailer\CustomDatabaseS3Spool'
Event Listeners:
mailer.email_sent or mailer.email_failed events to extend functionality:
use Symfony\Component\Mailer\EventListener\SentEvent;
$mailer->addEventListener(SentEvent::class, function (SentEvent $event) {
// Log or process sent email
});
Custom Email Entity:
Email entity entirely by:
How can I help you explore Laravel packages today?