-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathSystemSendEmail.php
More file actions
130 lines (119 loc) · 4.23 KB
/
SystemSendEmail.php
File metadata and controls
130 lines (119 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
namespace Drupal\rules\Plugin\RulesAction;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Mail\MailManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\rules\Core\RulesActionBase;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides "Send email" rules action.
*
* @RulesAction(
* id = "rules_send_email",
* label = @Translation("Send email"),
* category = @Translation("System"),
* context = {
* "to" = @ContextDefinition("email",
* label = @Translation("Send to"),
* description = @Translation("Email address(es) drupal will send an email to."),
* multiple = TRUE,
* ),
* "subject" = @ContextDefinition("string",
* label = @Translation("Subject"),
* description = @Translation("The email's subject."),
* ),
* "message" = @ContextDefinition("string",
* label = @Translation("Message"),
* description = @Translation("The email's message body."),
* form_element = "textarea",
* ),
* "reply" = @ContextDefinition("email",
* label = @Translation("Reply to"),
* description = @Translation("The mail's reply-to address. Leave it empty to use the site-wide configured address."),
* default_value = NULL,
* required = FALSE,
* ),
* "language" = @ContextDefinition("language",
* label = @Translation("Language"),
* description = @Translation("If specified, the language used for getting the mail message and subject."),
* default_value = NULL,
* required = FALSE,
* ),
* }
* )
*
* @todo: Define that message Context should be textarea comparing with textfield Subject
* @todo: Add access callback information from Drupal 7.
*/
class SystemSendEmail extends RulesActionBase implements ContainerFactoryPluginInterface {
/**
* The logger channel the action will write log messages to.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* @var \Drupal\Core\Mail\MailManagerInterface
*/
protected $mailManager;
/**
* Constructs a SendEmail object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin ID for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Psr\Log\LoggerInterface $logger
* The alias storage service.
* @param \Drupal\Core\Mail\MailManagerInterface $mail_manager
* The mail manager service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerInterface $logger, MailManagerInterface $mail_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->logger = $logger;
$this->mailManager = $mail_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('logger.factory')->get('rules'),
$container->get('plugin.manager.mail')
);
}
/**
* Send a system email.
*
* @param string[] $to
* Email addresses of the recipients.
* @param string $subject
* Subject of the email.
* @param string $message
* Email message text.
* @param string|null $reply
* (optional) Reply to email address.
* @param \Drupal\Core\Language\LanguageInterface|null $language
* (optional) Language code.
*/
protected function doExecute($to, $subject, $message, $reply = NULL, LanguageInterface $language = NULL) {
$langcode = isset($language) ? $language->getId() : LanguageInterface::LANGCODE_SITE_DEFAULT;
$params = [
'subject' => $subject,
'message' => $message,
];
// Set a unique key for this mail.
$key = 'rules_action_mail_' . $this->getPluginId();
$recipients = implode(', ', $to);
$message = $this->mailManager->mail('rules', $key, $recipients, $langcode, $params, $reply);
if ($message['result']) {
$this->logger->notice('Successfully sent email to %recipient', ['%recipient' => $recipients]);
}
}
}