- Drupal 8 Module Development
- Daniel Sipos
- 479字
- 2021-07-02 15:45:21
The mail plugin
So let's start by creating our Mail plugin class, and if you remember, plugins go inside the Plugin folder of our module namespace. Mail plugins belong inside a Mail folder. So this is what a simple skeleton mail plugin class can look like:
namespace Drupal\hello_world\Plugin\Mail;
use Drupal\Core\Mail\MailFormatHelper;
use Drupal\Core\Mail\MailInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Defines the Hello World mail backend.
*
* @Mail(
* id = "hello_world_mail",
* label = @Translation("Hello World mailer"),
* description = @Translation("Sends an email using an external API specific to our Hello World module.")
* )
*/
class HelloWorldMail implements MailInterface, ContainerFactoryPluginInterface {
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static();
}
/**
* {@inheritdoc}
*/
public function format(array $message) {
// Join the body array into one string.
$message['body'] = implode("\n\n", $message['body']);
// Convert any HTML to plain-text.
$message['body'] = MailFormatHelper::htmlToText($message['body']);
// Wrap the mail body for sending.
$message['body'] = MailFormatHelper::wrapMail($message['body']);
return $message;
}
/**
* {@inheritdoc}
*/
public function mail(array $message) {
// Use the external API to send the email based on the $message array
// constructed via the `hook_mail()` implementation.
}
}
As you can see, we have a relatively easy plugin annotation, no unusual arguments there. Then, you will note that we implemented the mandatory MailInterface, which comes with the two methods implemented in the following paragraph.
I mentioned the format() method earlier and stated that it is responsible for doing certain processing before the message is ready to be sent. The preceding implementation is a copy from the PhpMail plugin to exemplify just what kind of task would go there. However, you can do whatever you want in here, including allowing HTML tags. Imploding the body is something you will probably want to do anyway, as it is kind of expected that the mail body is constructed as an array by hook_mail().
The mail() method, on the other hand, is left empty. This is because it's up to you to use the external API to send the email. For this, you can use the $message array we encountered in the hook_mail() implementation.
Lastly, note that ContainerFactoryPluginInterface is another interface that our class implements. If you remember, that is what plugins need to implement in order for them to become container aware (for the dependencies to be injectable). Since this was only example code, it doesn't have any dependencies, so I did not include a constructor and left the create() method empty. Most likely, you will have to inject something, such as a PHP client library, that works with your external API. So, it doesn't hurt to see this again.
That is pretty much it for our plugin class. Now, let's take a look at how we can use it because for the moment, our hello_world_log emails are still being sent with the default PHP mailer.
- 一步一步學(xué)Spring Boot 2:微服務(wù)項(xiàng)目實(shí)戰(zhàn)
- Getting Started with CreateJS
- 面向STEM的Scratch創(chuàng)新課程
- 數(shù)據(jù)結(jié)構(gòu)習(xí)題精解(C語(yǔ)言實(shí)現(xiàn)+微課視頻)
- Linux網(wǎng)絡(luò)程序設(shè)計(jì):基于龍芯平臺(tái)
- Big Data Analytics
- 大數(shù)據(jù)分析與應(yīng)用實(shí)戰(zhàn):統(tǒng)計(jì)機(jī)器學(xué)習(xí)之?dāng)?shù)據(jù)導(dǎo)向編程
- C語(yǔ)言程序設(shè)計(jì)
- Android系統(tǒng)級(jí)深入開(kāi)發(fā)
- HTML 5與CSS 3權(quán)威指南(第3版·上冊(cè))
- 深入剖析Java虛擬機(jī):源碼剖析與實(shí)例詳解(基礎(chǔ)卷)
- Programming Microsoft Dynamics? NAV 2015
- 軟件測(cè)試分析與實(shí)踐
- HTML5游戲開(kāi)發(fā)實(shí)戰(zhàn)
- Python Social Media Analytics