An open API service indexing awesome lists of open source software.

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: 3 months ago
JSON representation

A modern, plugin-based API for sending mails in Drupal 8.

Awesome Lists containing this project

README

        

wmmailable
======================

[![Latest Stable Version](https://poser.pugx.org/wieni/wmmailable/v/stable)](https://packagist.org/packages/wieni/wmmailable)
[![Total Downloads](https://poser.pugx.org/wieni/wmmailable/downloads)](https://packagist.org/packages/wieni/wmmailable)
[![License](https://poser.pugx.org/wieni/wmmailable/license)](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 ', '[email protected]'])
->addBcc('[email protected]')
->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
[[email protected]](mailto:[email protected]) instead of using the issue
tracker.

## License
Distributed under the MIT License. See the [LICENSE](LICENSE) file
for more information.