Last active
December 18, 2015 14:49
-
-
Save bangpound/5799683 to your computer and use it in GitHub Desktop.
use secure token in action emal.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Implements hook_action_info(). | |
*/ | |
function MODULE_action_info() { | |
return array( | |
'MODULE_send_email_secure_tokens_action' => array( | |
'type' => 'system', | |
'label' => t('Send e-mail with user secure tokens'), | |
'configurable' => TRUE, | |
'triggers' => array('any'), | |
), | |
); | |
} | |
/** | |
* Return a form definition so the Send email action can be configured. | |
* | |
* @param $context | |
* Default values (if we are editing an existing action instance). | |
* | |
* @return | |
* Form definition. | |
* | |
* @see MODULE_send_email_secure_tokens_action_validate() | |
* @see MODULE_send_email_secure_tokens_action_submit() | |
*/ | |
function MODULE_send_email_secure_tokens_action_form($context) { | |
return system_send_email_action_form($context); | |
} | |
/** | |
* Validate MODULE_send_email_secure_tokens_action form submissions. | |
*/ | |
function MODULE_send_email_secure_tokens_action_validate($form, $form_state) { | |
system_send_email_action_validate($form, $form_state); | |
} | |
/** | |
* Process MODULE_send_email_secure_tokens_action form submissions. | |
*/ | |
function MODULE_send_email_secure_tokens_action_submit($form, $form_state) { | |
return system_send_email_action_form_submit($form, $form_state); | |
} | |
/** | |
* Sends an e-mail message. | |
* | |
* @param object $entity | |
* An optional node object, which will be added as $context['node'] if | |
* provided. | |
* @param array $context | |
* Array with the following elements: | |
* - 'recipient': E-mail message recipient. This will be passed through | |
* token_replace(). | |
* - 'subject': The subject of the message. This will be passed through | |
* token_replace(). | |
* - 'message': The message to send. This will be passed through | |
* token_replace(). | |
* - Other elements will be used as the data for token replacement. | |
* | |
* @ingroup actions | |
*/ | |
function MODULE_send_email_secure_tokens_action($entity, $context) { | |
if (empty($context['node'])) { | |
$context['node'] = $entity; | |
} | |
$recipient = token_replace($context['recipient'], $context); | |
// If the recipient is a registered user with a language preference, use | |
// the recipient's preferred language. Otherwise, use the system default | |
// language. | |
$recipient_account = user_load_by_mail($recipient); | |
if ($recipient_account) { | |
$language = user_preferred_language($recipient_account); | |
} | |
else { | |
$language = language_default(); | |
} | |
$params = array('context' => $context); | |
if (drupal_mail('MODULE', 'action_send_email_secure_tokens', $recipient, $language, $params)) { | |
watchdog('action', 'Sent email to %recipient', array('%recipient' => $recipient)); | |
} | |
else { | |
watchdog('error', 'Unable to send email to %recipient', array('%recipient' => $recipient)); | |
} | |
} | |
/** | |
* Implement hook_mail() | |
*/ | |
function MODULE_mail($key, &$message, $params) { | |
$context = $params['context']; | |
$subject = token_replace($context['subject'], $context, array('callback' => 'user_mail_tokens')); | |
$body = token_replace($context['message'], $context, array('callback' => 'user_mail_tokens')); | |
$message['subject'] .= str_replace(array("\r", "\n"), '', $subject); | |
$message['body'][] = $body; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment