Send email with Symfony Mailer
Since vaartix/vaartix-php 0.3.0, Symfony Mailer can send through Vaartix with a
MAILER_DSN, so MailerInterface, TemplatedEmail, notifications and Messenger-queued mail all
go through Vaartix. It needs symfony/mailer 6.2 or newer. The source is on GitHub at
vaartix-mail/vaartix-php and the package is on
Packagist.
-
Install the SDK
Terminal window composer require vaartix/vaartix-php symfony/mailer -
Set the DSN
.env.local MAILER_DSN=vaartix+api://vx_your_api_key@defaultdefaultmeanshttps://api.vaartix.com. Add?timeout=10to change the request timeout (seconds), for examplevaartix+api://vx_your_api_key@default?timeout=10. If your API key contains+or/, URL-encode them in the DSN (%2Band%2F). -
Register the transport factory
config/services.yaml services:Vaartix\Symfony\VaartixTransportFactory:parent: mailer.transport_factory.abstracttags: ['mailer.transport_factory'] -
Send a Symfony email
use Symfony\Component\Mailer\MailerInterface;use Symfony\Component\Mime\Email;public function confirm(MailerInterface $mailer): Response{$mailer->send((new Email())->subject('Order confirmed')->html('<p>Thanks for your order!</p>'),);// ...}
Without the framework
Section titled “Without the framework”Build the transport from the same DSN:
use Symfony\Component\Mailer\Mailer;use Symfony\Component\Mailer\Transport;use Vaartix\Symfony\VaartixTransportFactory;
$transport = (new Transport([new VaartixTransportFactory()])) ->fromString('vaartix+api://vx_your_api_key@default');$mailer = new Mailer($transport);Or create the transport directly, reading VAARTIX_API_KEY from the environment:
$mailer = new Mailer(new Vaartix\Laravel\VaartixTransport(new Vaartix\Vaartix()));To verify webhooks, use Vaartix\Webhook::verify() as shown in the
PHP SDK docs.