dmt-software/mail-service

Mail sender service

Maintainers

Package info

github.com/dmt-software/mail-service

pkg:composer/dmt-software/mail-service

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

0.2.3 2026-04-08 21:23 UTC

This package is auto-updated.

Last update: 2026-04-09 07:45:22 UTC


README

Installation

composer require dmt-software/mail-service

Choose a mailer package

Currently, the only adapter available uses symfony/mailer.
This should be added to the composer dependencies.

composer require symfony/mailer:^8.0

Usage

use DMT\MailService\Exceptions\InvalidMessageException;
use DMT\MailService\Exceptions\SendMessageException;
use DMT\MailService\MailService;
use DMT\MailService\Model\EmailAddress;
use DMT\MailService\Model\EmailMessage;

try {
    /** @var MailService $service */
    $service->send(
        new EmailMessage(
            subject: 'subject',
            html: '<p>html content</p>',
            text: 'text content',
            to: new EmailAddress('user@example.com'),
            from: new EmailAddress('site@example.com') 
        )
    );
} catch (InvalidMessageException|SendMessageException) {
    // error sending email
}

Events

Auto create text part

To create a text part of the email, the HtmlToTextEventSubscriber can be used. For any HTML mailing without a text part, this event subscriber will generate a simple text variant for that message.

use DMT\MailService\Adapters\MailAdapterInterface;
use DMT\MailService\Event\MailServiceEventDispatcher;
use DMT\MailService\Event\Subscribers\HtmlToTextEventSubscriber;
use DMT\MailService\MailService;
use DMT\MailService\Model\EmailMessage;

/** @var MailAdapterInterface $adapter */
$service = new MailService(
    $adapter, 
    new MailServiceEventDispatcher(
        new HtmlToTextEventSubscriber()
    )
);

/** @var EmailMessage $message */
$message = new EmailMessage(
    subject: 'subject',
    html: '<p>content</p>',
    text: null,
    to: new EmailAddress('user@example.com'),
    from: new EmailAddress('site@example.com') 
);

$service->send($message); // message will contain a text part: "content"

Using mail templates

The message content can also be rendered from a twig template. This can be done by using the RenderMailTemplateEventSubscriber subscriber.

use DMT\MailService\Adapters\MailAdapterInterface;
use DMT\MailService\Event\MailServiceEventDispatcher;
use DMT\MailService\Event\Subscribers\RenderMailTemplateEventSubscriber;
use DMT\MailService\MailService;
use DMT\MailService\Model\TemplatedMessage;
use Twig\Environment;

/** @var MailAdapterInterface $adapter */
/** @var Environment $twig */
$service = new MailService(
    $adapter, 
    new MailServiceEventDispatcher(
        new RenderMailTemplateEventSubscriber($twig)
    )
);

$message = new TemplatedMessage(
    subject: 'subject',
    template: 'mail/template.twig',
    to: new EmailAddress('user@example.com'),
    from: new EmailAddress('site@example.com'),
    context: ['name' => 'Jane Doe']
);

$service->send($message); // template will be rendered

If a template contains a block called html_part, that block will be rendered for the HTML part of the message. A block called text_part, will be rendered as text part of the message. When none of the blocks above are specified the whole template is rendered as HTML part.