array( 'anonymous_contact' => array( 'user_contact_form_sent' => array( 'runs when' => t('Site visitor submitted a user contact form') ) ) ) ); } /** * Implementation of hook_trigger_name(). */ function anonymous_contact_anonymous_contact($op, $user, $form_values) { if (!in_array($op, array('user_contact_form_sent'))) { return; } $aids = _trigger_get_hook_aids('anonymous_contact', $op); $context = array( 'hook' => 'anonymous_contact', 'op' => $op, 'user' => $user, 'form_values' => $form_values ); actions_do(array_keys($aids), $user, $context); } /** * Implementation of hook_action_info(). */ function anonymous_contact_action_info() { return array( 'anonymous_contact_send_email_action' => array( 'description' => t('Send e-mail about user contact form'), 'type' => 'anonymous_contact', 'configurable' => TRUE, 'hooks' => array( 'anonymous_contact' => array('user_contact_form_sent') ) ) ); } /** * Define recipient for the action. * * @see anonymous_contact_send_email_action_validate() * @see anonymous_contact_send_email_action_submit() * @param $context * Default values (if we are editing an existing action instance). * @return * Form definition. */ function anonymous_contact_send_email_action_form($context) { if (!isset($context['recipient'])) { $context['recipient'] = ''; } if (!isset($context['subject'])) { $context['subject'] = ''; } if (!isset($context['message'])) { $context['message'] = ''; } $form['recipient'] = array( '#type' => 'textfield', '#title' => t('Recipient'), '#default_value' => $context['recipient'], '#maxlength' => '254', '#description' => t('The email address to which the message should be sent.'), ); $form['subject'] = array( '#type' => 'textfield', '#title' => t('Subject'), '#default_value' => $context['subject'], '#maxlength' => '254', '#description' => t('The subject of the message.'), ); $form['message'] = array( '#type' => 'textarea', '#title' => t('Message'), '#default_value' => $context['message'], '#cols' => '80', '#rows' => '20', '#description' => t('The message that should be sent. You may include the following variables: %recipient, %sender_name, %sender_mail, %subject, %body.'), ); return $form; } /** * Validate anonymous_contact_send_email_action form submissions. */ function anonymous_contact_send_email_action_validate($form, $form_state) { $form_values = $form_state['values']; if (!valid_email_address($form_values['recipient'])) { form_set_error('recipient', t('Please enter a valid email address.')); } } /** * Process anonymous_contact_send_email_action form submissions. */ function anonymous_contact_send_email_action_submit($form, $form_state) { $form_values = $form_state['values']; $params = array( 'recipient' => $form_values['recipient'], 'subject' => $form_values['subject'], 'message' => $form_values['message'] ); return $params; } /** * Implementation of Drupal action. */ function anonymous_contact_send_email_action($object, $context) { global $user; $recipient = $context['recipient']; $language = user_preferred_language($account); $params = array('account' => $context['form_values']['recipient'], 'fake_account' => $context['form_values']['user'], 'context' => $context); if (drupal_mail('anonymous_contact', 'action_send_email', $recipient, $language, $params)) { watchdog('action', 'Sent email to %recipient', array('%recipient' => $recipient)); } else { watchdog('error', 'Unable to send email to %recipient', array('%recipient' => $recipient)); } } /** * Addition to hook_mail(). */ function anonymous_contact_mail($key, &$message, $params) { case 'action_send_email': $variables = array( '%site_name' => variable_get('site_name', 'Drupal'), '%recipient' => $params['context']['user']->name, '%sender_name' => $params['context']['form_values']['user']->name, '%sender_mail' => $params['context']['form_values']['user']->mail, '%subject' => $params['context']['form_values']['subject'], '%body' => nl2br($params['context']['form_values']['message']) ); $subject = strtr($params['context']['subject'], $variables); $body = strtr($params['context']['message'], $variables); $message['subject'] = $subject; $message['body'][] = drupal_html_to_text($body); break; }