https://github.com/wieni/wmmailable
A modern, plugin-based API for sending mails in Drupal 8.
https://github.com/wieni/wmmailable
drupal-8 drupal-module drupal8-module
Last synced: 18 days ago
JSON representation
A modern, plugin-based API for sending mails in Drupal 8.
- Host: GitHub
- URL: https://github.com/wieni/wmmailable
- Owner: wieni
- License: mit
- Created: 2018-11-10T14:29:53.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2025-06-03T12:11:59.000Z (10 months ago)
- Last Synced: 2025-10-24T02:18:19.784Z (5 months ago)
- Topics: drupal-8, drupal-module, drupal8-module
- Language: PHP
- Homepage:
- Size: 145 KB
- Stars: 0
- Watchers: 4
- Forks: 1
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
wmmailable
======================
[](https://packagist.org/packages/wieni/wmmailable)
[](https://packagist.org/packages/wieni/wmmailable)
[](https://packagist.org/packages/wieni/wmmailable)
> A modern, plugin-based API for sending mails in Drupal 8. Inspired by [Laravel](https://laravel.com/docs/5.7/mail)
## Why?
- No 'modern' way to handle mails in Drupal 8: messing with `hook_mail` and `hook_theme` does not really fit in the Wieni Drupal flow with [wmmodel](https://github.com/wieni/wmmodel), [wmcontroller](https://github.com/wieni/wmcontroller), etc.
- No clean, object-oriented API, e.g. to add CC / BCC-adresses you have to manually add headers
- Not intuitive, logic is scattered across multiple files, tends to get messy
## Installation
This package requires PHP 7.1 and Drupal 8 or higher. It can be
installed using Composer:
```bash
composer require wieni/wmmailable
```
## How does it work?
### Building mails
- Mails are annotated plugins
- Each class represents one mail
- Dependency injection is possible by implementing the `ContainerFactoryPluginInterface` ([tutorial](https://chromatichq.com/blog/dependency-injection-drupal-8-plugins))
```php
mailer = $mailer;
}
public function submitForm(array &$form, FormStateInterface $formState)
{
$mail = $this->mailer->create('contact_form_submission')
->setRecepients(['Wieni ', 'dieter@wieni.be'])
->addBcc('sophie@wieni.be')
->setParameters(
compact('domain', 'firstName', 'lastName', 'email', 'question')
);
$this->mailer->send($mail);
}
public static function create(ContainerInterface $container)
{
return new static(
$container->get('mailable.mailer')
);
}
}
```
### Logging sent mails
- When enabled, this wil log all outgoing mails - also those that aren't composed through this module.
- To enable, configure the [mailsystem](https://www.drupal.org/project/mailsystem) module to use _Mailable - Logger_ as
the mail sender.
- To view logged mails, go to `/admin/reports/sent-mails` or follow the menu link at _Reports_ > _Sent mails_.
### Hooks and events
- Two hooks are provided, `hook_mailable_alter` and `hook_mailable_{module}_{key}_alter`. These hooks are called after the `send` method is called on the mailable, but before the mail is sent.
```php
setHeader('X-SES-SOURCE-ARN', '<...>');
}
```
- Two events are provided, equivalent to the hooks:
```php
getMailable();
$mailable->setSubject("Here's a better subject.");
}
}
```
## Changelog
All notable changes to this project will be documented in the
[CHANGELOG](CHANGELOG.md) file.
## Security
If you discover any security-related issues, please email
[security@wieni.be](mailto:security@wieni.be) instead of using the issue
tracker.
## License
Distributed under the MIT License. See the [LICENSE](LICENSE) file
for more information.