andrei-mireichyk/sendpulse-mailer
composer require andrei-mireichyk/sendpulse-mailer
.env:
Choose one of the supported schemes and add to your .env:
MAILER_DSN=sendpulse://USERNAME:PASSWORD@default
# OR
MAILER_DSN=sendpulse+smtp-api://USER_ID:SECRET@default
# OR (for Automation360 events)
MAILER_DSN=sendpulse+events://USER_ID:SECRET@default
Email class:
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
public function sendEmail(MailerInterface $mailer)
{
$email = (new Email())
->from('from@example.com')
->to('recipient@example.com')
->subject('Test Email')
->text('Hello, this is a test email!');
$mailer->send($email);
}
Inject MailerInterface via dependency injection (Laravel’s Mail facade or Symfony’s DI container).Basic Email Sending:
Use Symfony’s Email class with SendPulse’s DSN. The package seamlessly integrates with Laravel’s Mail facade or Symfony’s MailerInterface.
Mail::send(new Email()
->to('user@example.com')
->subject('Welcome')
->html('<h1>Welcome!</h1>')
);
Custom Headers for SendPulse Features: Leverage SendPulse-specific headers for advanced use cases:
$email->getHeaders()->addTextHeader('X-SendPulse-Event', 'user_signup');
$email->getHeaders()->add(new \Creonit\SendPulseMailer\Header\SendPulseVariableHeader('user_name', 'John'));
Async Sending (Queue): Laravel users can queue emails for async processing:
Mail::to('user@example.com')->send(new WelcomeMail());
Ensure your MailerInterface is bound to the SendPulse transport in Laravel’s config/mail.php.
Templates:
Use SendPulse’s template system by referencing template IDs or names in the Email object (if supported by the package’s future updates).
Laravel-Specific:
config/mail.php:
'sendpulse' => [
'transport' => 'sendpulse',
'username' => env('SENDPULSE_USERNAME'),
'password' => env('SENDPULSE_PASSWORD'),
],
Mail facade or inject MailerInterface into controllers/services.Symfony-Specific:
Configure the mailer in config/packages/mailer.yaml:
framework:
mailer:
dsn: '%env(MAILER_DSN)%'
Testing:
Mock MailerInterface in tests to avoid real API calls:
$mailer = $this->createMock(MailerInterface::class);
$mailer->expects($this->once())->method('send');
Logging: Enable Symfony’s mailer logging for debugging:
$mailer->send($email, ['logger' => $logger]);
DSN Configuration:
sendpulse, sendpulse+smtp-api, or sendpulse+events). Mixing schemes (e.g., sendpulse+smtp instead of sendpulse+smtp-api) will fail silently or throw errors.USERNAME:PASSWORD or USER_ID:SECRET are correct. SendPulse’s SMTP API uses USER_ID (not email) and SECRET (not password).Headers Not Applied:
X-SendPulse-Event or SendPulseVariableHeader must be added before sending. Late additions may be ignored.X-SendPulse-Event must match SendPulse’s expected format exactly.Async Delays:
Template Limitations:
Error Handling:
try {
$mailer->send($email);
} catch (\Symfony\Component\Mailer\Exception\TransportException $e) {
Log::error('SendPulse error: ' . $e->getMessage());
}
Enable Verbose Logging: Configure Symfony’s mailer to log raw messages:
$mailer->send($email, ['logger' => new \Monolog\Logger('mailer', [$this->logger])]);
Check Raw Headers:
Inspect the Email object’s headers before sending:
dump($email->getHeaders()->all());
SendPulse API Status: Verify SendPulse’s API status at SendPulse Status Page or use their API docs.
Test with SMTP: For debugging, temporarily switch to a local SMTP server (e.g., MailHog) by changing the DSN to:
MAILER_DSN=smtp://mailhog:1025
Custom Headers: Extend the package by creating custom header classes (e.g., for SendPulse’s webhook events):
class SendPulseWebhookHeader extends AbstractHeader
{
public function __construct(string $webhookUrl)
{
$this->header = 'X-SendPulse-Webhook';
$this->value = $webhookUrl;
}
}
Event Dispatching:
Listen for mailer.message.sent events (Symfony) to log or process SendPulse-specific data:
$mailer->send($email, ['event_dispatcher' => $dispatcher]);
Transport Decorator: Decorate the SendPulse transport to add pre-send logic (e.g., analytics):
$transport = new SendPulseTransport($client, $options);
$transport = new AnalyticsDecorator($transport);
Queue Observers: In Laravel, observe mail queue jobs to enrich SendPulse metadata:
Mail::addObserver(function ($event) {
if ($event->mailable instanceof WelcomeMail) {
$event->message->getHeaders()->addTextHeader('X-SendPulse-Campaign', 'welcome_series');
}
});
How can I help you explore Laravel packages today?