Skip to content

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.

  1. Install the SDK

    Terminal window
    composer require vaartix/vaartix-php symfony/mailer
  2. Set the DSN

    .env.local
    MAILER_DSN=vaartix+api://vx_your_api_key@default

    default means https://api.vaartix.com. Add ?timeout=10 to change the request timeout (seconds), for example vaartix+api://vx_your_api_key@default?timeout=10. If your API key contains + or /, URL-encode them in the DSN (%2B and %2F).

  3. Register the transport factory

    config/services.yaml
    services:
    Vaartix\Symfony\VaartixTransportFactory:
    parent: mailer.transport_factory.abstract
    tags: ['mailer.transport_factory']
  4. Send a Symfony email

    use Symfony\Component\Mailer\MailerInterface;
    use Symfony\Component\Mime\Email;
    public function confirm(MailerInterface $mailer): Response
    {
    $mailer->send(
    (new Email())
    ->from('Acme <[email protected]>') // on a domain verified in Vaartix
    ->subject('Order confirmed')
    ->html('<p>Thanks for your order!</p>'),
    );
    // ...
    }

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.