|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\df_tools_message\Plugin\RulesAction; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Core\Language\LanguageInterface; |
|
6
|
|
|
use Drupal\Core\Mail\MailManagerInterface; |
|
7
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
|
8
|
|
|
use Drupal\rules\Core\RulesActionBase; |
|
9
|
|
|
use Psr\Log\LoggerInterface; |
|
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Provides "Send styled email" rules action. |
|
15
|
|
|
* |
|
16
|
|
|
* @RulesAction( |
|
17
|
|
|
* id = "df_tools_message_send_styled_email", |
|
18
|
|
|
* label = @Translation("Send styled email"), |
|
19
|
|
|
* category = @Translation("System"), |
|
20
|
|
|
* context = { |
|
21
|
|
|
* "to" = @ContextDefinition("email", |
|
22
|
|
|
* label = @Translation("Send to"), |
|
23
|
|
|
* description = @Translation("Email address drupal will send an email to."), |
|
24
|
|
|
* ), |
|
25
|
|
|
* "subject" = @ContextDefinition("string", |
|
26
|
|
|
* label = @Translation("Subject"), |
|
27
|
|
|
* description = @Translation("The email's subject."), |
|
28
|
|
|
* ), |
|
29
|
|
|
* "message" = @ContextDefinition("string", |
|
30
|
|
|
* label = @Translation("Message"), |
|
31
|
|
|
* description = @Translation("The email's message body. If you want any non-default styling, use inline styles."), |
|
32
|
|
|
* ), |
|
33
|
|
|
* "format" = @ContextDefinition("string", |
|
34
|
|
|
* label = @Translation("Format"), |
|
35
|
|
|
* description = @Translation("The email's message text format."), |
|
36
|
|
|
* default_value = "rich_text", |
|
37
|
|
|
* ) |
|
38
|
|
|
* } |
|
39
|
|
|
* ) |
|
40
|
|
|
*/ |
|
41
|
|
|
class SendStyledEmail extends RulesActionBase implements ContainerFactoryPluginInterface { |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* The logger channel the action will write log messages to. |
|
45
|
|
|
* |
|
46
|
|
|
* @var \Psr\Log\LoggerInterface |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $logger; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var \Drupal\Core\Mail\MailManagerInterface |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $mailManager; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Constructs a SendEmail object. |
|
57
|
|
|
* |
|
58
|
|
|
* @param array $configuration |
|
59
|
|
|
* A configuration array containing information about the plugin instance. |
|
60
|
|
|
* @param string $plugin_id |
|
61
|
|
|
* The plugin ID for the plugin instance. |
|
62
|
|
|
* @param mixed $plugin_definition |
|
63
|
|
|
* The plugin implementation definition. |
|
64
|
|
|
* @param \Psr\Log\LoggerInterface $logger |
|
65
|
|
|
* The alias storage service. |
|
66
|
|
|
* @param \Drupal\Core\Mail\MailManagerInterface $mail_manager |
|
67
|
|
|
* The mail manager service. |
|
68
|
|
|
*/ |
|
69
|
|
|
public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerInterface $logger, MailManagerInterface $mail_manager) { |
|
70
|
|
|
parent::__construct($configuration, $plugin_id, $plugin_definition); |
|
71
|
|
|
$this->logger = $logger; |
|
72
|
|
|
$this->mailManager = $mail_manager; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* {@inheritdoc} |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
|
79
|
|
|
return new static( |
|
80
|
|
|
$configuration, |
|
81
|
|
|
$plugin_id, |
|
82
|
|
|
$plugin_definition, |
|
83
|
|
|
$container->get('logger.factory')->get('rules'), |
|
84
|
|
|
$container->get('plugin.manager.mail') |
|
|
|
|
|
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Send a styled email. |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $to |
|
92
|
|
|
* Email address of the recipient. |
|
93
|
|
|
* @param string $subject |
|
94
|
|
|
* Subject of the email. |
|
95
|
|
|
* @param string $message |
|
96
|
|
|
* Email message text. |
|
97
|
|
|
* @param string $format |
|
98
|
|
|
* Email message text format. |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function doExecute($to, $subject, $message, $format) { |
|
101
|
|
|
$params = [ |
|
102
|
|
|
'subject' => $subject, |
|
103
|
|
|
'message' => $message, |
|
104
|
|
|
'format' => $format, |
|
105
|
|
|
]; |
|
106
|
|
|
// Set a unique key for this mail. |
|
107
|
|
|
$key = 'rules_action_mail_' . $this->getPluginId(); |
|
108
|
|
|
$message = $this->mailManager->mail('df_tools_message', $key, $to, LanguageInterface::LANGCODE_NOT_SPECIFIED, $params); |
|
109
|
|
|
if ($message['result']) { |
|
110
|
|
|
$this->logger->notice('Successfully sent styled email to %recipient', ['%recipient' => $to]); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|