Sends an e-mail message.
@Action(
id = "action_send_email_action",
label = @Translation("Send e-mail"),
type = "system"
)
Expanded class hierarchy of EmailAction
class EmailAction extends ConfigurableActionBase {
/**
* The token service.
*
* @var \Drupal\Core\Utility\Token
*/
protected $token;
/**
* The user storage controller.
*
* @var \Drupal\Core\Entity\EntityStorageControllerInterface
*/
protected $storageController;
/**
* Constructs a EmailAction 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 array $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Utility\Token $token
* The token service.
* @param \Drupal\Core\Entity\EntityManager $entity_manager
* The entity manager.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, Token $token, EntityManager $entity_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->token = $token;
$this->storageController = $entity_manager
->getStorageController('user');
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('token'), $container
->get('plugin.manager.entity'));
}
/**
* {@inheritdoc}
*/
public function execute($entity = NULL) {
if (empty($this->configuration['node'])) {
$this->configuration['node'] = $entity;
}
$recipient = $this->token
->replace($this->configuration['recipient'], $this->configuration);
// If the recipient is a registered user with a language preference, use
// the recipient's preferred language. Otherwise, use the system default
// language.
$recipient_accounts = $this->storageController
->loadByProperties(array(
'mail' => $recipient,
));
$recipient_account = reset($recipient_accounts);
if ($recipient_account) {
$langcode = user_preferred_langcode($recipient_account);
}
else {
$langcode = language_default()->langcode;
}
$params = array(
'context' => $this->configuration,
);
if (drupal_mail('system', 'action_send_email', $recipient, $langcode, $params)) {
watchdog('action', 'Sent email to %recipient', array(
'%recipient' => $recipient,
));
}
else {
watchdog('error', 'Unable to send email to %recipient', array(
'%recipient' => $recipient,
));
}
}
/**
* {@inheritdoc}
*/
protected function getDefaultConfiguration() {
return array(
'recipient' => '',
'subject' => '',
'message' => '',
);
}
/**
* {@inheritdoc}
*/
public function form(array $form, array &$form_state) {
$form['recipient'] = array(
'#type' => 'textfield',
'#title' => t('Recipient'),
'#default_value' => $this->configuration['recipient'],
'#maxlength' => '254',
'#description' => t('The e-mail address to which the message should be sent OR enter [node:author:mail], [comment:author:mail], etc. if you would like to send an e-mail to the author of the original post.'),
);
$form['subject'] = array(
'#type' => 'textfield',
'#title' => t('Subject'),
'#default_value' => $this->configuration['subject'],
'#maxlength' => '254',
'#description' => t('The subject of the message.'),
);
$form['message'] = array(
'#type' => 'textarea',
'#title' => t('Message'),
'#default_value' => $this->configuration['message'],
'#cols' => '80',
'#rows' => '20',
'#description' => t('The message that should be sent. You may include placeholders like [node:title], [user:name], and [comment:body] to represent data that will be different each time message is sent. Not all placeholders will be available in all contexts.'),
);
return $form;
}
/**
* {@inheritdoc}
*/
public function validate(array &$form, array &$form_state) {
if (!valid_email_address($form_state['values']['recipient']) && strpos($form_state['values']['recipient'], ':mail') === FALSE) {
// We want the literal %author placeholder to be emphasized in the error message.
form_set_error('recipient', t('Enter a valid email address or use a token e-mail address such as %author.', array(
'%author' => '[node:author:mail]',
)));
}
}
/**
* {@inheritdoc}
*/
public function submit(array &$form, array &$form_state) {
$this->configuration['recipient'] = $form_state['values']['recipient'];
$this->configuration['subject'] = $form_state['values']['subject'];
$this->configuration['message'] = $form_state['values']['message'];
}
}
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ActionBase:: |
public | function |
Executes the plugin for an array of objects. Overrides ActionInterface:: |
2 |
ConfigurableActionBase:: |
public | function |
Returns this plugin's configuration. Overrides ConfigurableActionInterface:: |
|
EmailAction:: |
protected | property | The user storage controller. | |
EmailAction:: |
protected | property | The token service. | |
EmailAction:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginBase:: |
|
EmailAction:: |
public | function |
Executes the plugin. Overrides ExecutableInterface:: |
|
EmailAction:: |
public | function |
Form constructor. Overrides ConfigurableActionInterface:: |
|
EmailAction:: |
protected | function |
Returns default configuration for this action. Overrides ConfigurableActionBase:: |
|
EmailAction:: |
public | function |
Form submission handler. Overrides ConfigurableActionInterface:: |
|
EmailAction:: |
public | function |
Form validation handler. Overrides ConfigurableActionBase:: |
|
EmailAction:: |
public | function |
Constructs a EmailAction object. Overrides ConfigurableActionBase:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
public | function |
Returns the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function |
Returns the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |