Skip to content

Latest commit

 

History

History

Symfony

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Mailtrap bridge for Symfony framework [API]

Provides mailtrap.io integration for Symfony Mailer.

Installation

If you just want to get started quickly, you should run the following command:

composer require railsware/mailtrap-php symfony/http-client nyholm/psr7

Usage

Add MailtrapTransport into your config/services.yaml file

...
    # add more service definitions when explicit configuration is needed
    # please note that last definitions always *replace* previous ones

    Mailtrap\Bridge\Transport\MailtrapTransportFactory:
        tags:
            - { name: 'mailer.transport_factory' }

Sending

Add or change MAILER_DSN variable inside your .env file. Also, you need to change the YOUR_API_KEY_HERE placeholder.

MAILER_DSN=mailtrap+api://YOUR_API_KEY_HERE@default
# or
MAILER_DSN=mailtrap+api://YOUR_API_KEY_HERE@send.api.mailtrap.io

Bulk Sending

Add or change MAILER_DSN variable inside your .env file. Also, you need to change the YOUR_API_KEY_HERE placeholder.

More info about bulk sending -> https://help.mailtrap.io/article/113-sending-streams

MAILER_DSN=mailtrap+api://YOUR_API_KEY_HERE@bulk.api.mailtrap.io

Sandbox

Add or change MAILER_DSN variable inside your .env file. Also, you need to change the YOUR_API_KEY_HERE placeholder and put correct inboxId.

More info sandbox -> https://help.mailtrap.io/article/109-getting-started-with-mailtrap-email-testing

MAILER_DSN=mailtrap+api://YOUR_API_KEY_HERE@sandbox.api.mailtrap.io?inboxId=1000001

Send you first email

CLI command (the mailer:test command was introduced only in Symfony 6.2)

php bin/console mailer:test to@example.com

Controller (base example)

<?php

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Mailer\Transport\TransportInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Mime\Email;
use Symfony\Component\Routing\Annotation\Route;


final class SomeController extends AbstractController
{
    private TransportInterface $transport;

    public function __construct(TransportInterface $transport)
    {
        $this->transport = $transport;
    }

    /**
     * @Route(name="send-test-email", path="/test", methods={"GET"})
     *
     * @return JsonResponse
     */
    public function sendTestEmail(): JsonResponse
    {
        $message = (new Email())
            ->to('to@xample.com')
            ->from('from@xample.com')
            ->subject('Test email')
            ->text('text')
        ;

        $response = $this->transport->send($message);

        return JsonResponse::create(['messageId' => $response->getMessageId()]);
    }
}

Resources