Index: simplenews.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/simplenews/simplenews.module,v retrieving revision 1.76.2.94 diff -u -r1.76.2.94 simplenews.module --- simplenews.module 11 Dec 2008 09:53:47 -0000 1.76.2.94 +++ simplenews.module 16 Dec 2008 11:33:18 -0000 @@ -1008,7 +1008,7 @@ db_query("INSERT INTO {simplenews_snid_tid} (snid, tid) VALUES (%d, %d)", $subscription->snid, $tid); // Execute simplenews subscribe trigger. - simplenews_call_actions('subscribe', $subscription); + simplenews_call_actions('subscribe', $subscription, $tid); } return TRUE; } @@ -1057,7 +1057,7 @@ } // Execute simplenews unsubscribe trigger - simplenews_call_actions('unsubscribe', $subscription); + simplenews_call_actions('unsubscribe', $subscription, $tid); } return TRUE; @@ -1663,6 +1663,36 @@ $message['body'] = _simplenews_subscription_confirmation_text('unsubscribe_unsubscribed', $context['account']->language, $variables); } break; + case 'action_send_email': + $account = $context['account']; + $newsletter = $params['newsletter']; + // Use formatted from address "name" + $message['headers']['From'] = $params['from']['formatted']; + + $user = user_load( $account->uid ); + + //Anonymous users need a name + if (!$user->uid) { + $user->name = t('Anonymous User'); + } + + //Build the message body + //For some reason, if a user has an account on the site, this if statement is reversed (thus the || $user->uid) + if (simplenews_user_is_subscribed($account->mail, $newsletter->tid) || $user->uid) { + $body = t('@username (@email) just subscribed to @newsletter', array('@username' => $user->name, '@email' => $account->mail, '@newsletter' => $newsletter->name)); + $message['subject'] = t('[@newsletter] subscription', array('@newsletter' => $newsletter->name)); + } + else { + $body = t('@username (@email) just unsubscribed from @newsletter', array('@username' => $user->name, '@email' => $account->mail, '@newsletter' => $newsletter->name)); + $message['subject'] = t('[@newsletter] unsubscription', array('@newsletter' => $newsletter->name)); + } + + if ($user->uid) { + $body .= "\n\n" . t("Access this user's account page at the following URL: !url", array('!url' => url('user/' . $user->uid, array('absolute' => TRUE)))); + } + + $message['body'] = $body; + break; } // Debug message to check for outgoing emails messages. @@ -1929,7 +1959,7 @@ /** * Call simplenews actions. */ -function simplenews_call_actions($op, $subscription) { +function simplenews_call_actions($op, $subscription, $tid) { // Only call actions when the simplenews_action module is enabled. if (!module_exists('simplenews_action')) { return; @@ -1939,6 +1969,7 @@ 'hook' => 'simplenews', 'op' => $op, 'account' => $subscription, + 'tid' => $tid, ); foreach ($aids as $aid => $action_info) { actions_do($aid, $subscription, $context); Index: simplenews_action/simplenews_action.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/simplenews/simplenews_action/simplenews_action.module,v retrieving revision 1.1.2.5 diff -u -r1.1.2.5 simplenews_action.module --- simplenews_action/simplenews_action.module 4 Aug 2008 06:11:51 -0000 1.1.2.5 +++ simplenews_action/simplenews_action.module 16 Dec 2008 11:34:52 -0000 @@ -47,6 +47,14 @@ 'user' => array('update', 'delete'), ), ), + 'simplenews_send_email_action' => array( + 'description' => t('Send email upon subscription/unsubscription'), + 'type' => 'simplenews', + 'configurable' => TRUE, + 'hooks' => array( + 'simplenews' => array('subscribe', 'unsubscribe'), + ), + ), ); } @@ -296,6 +304,87 @@ } /** + * A configurable Drupal action. Send an email when someone subscribes or unsubscribes from a newsletter. + * + * Available context: + * $context['recipient'] email recipient + * + * @see simplenews_send_email_action_form() + * @see simplenews_send_email_action_submit() + */ +function simplenews_send_email_action($object, $context) { + if (in_array($context['tid'], $context['newsletter'])) { + $params['from'] = _simplenews_set_from(); + $params['newsletter'] = taxonomy_get_term( $context['tid'] ); + $params['context']['account'] = $object; + + drupal_mail('simplenews', 'action_send_email', $context['recipient'], $object->language, $params, $params['from']['address']); + } +} + +/** + * Implementation of a configurable Drupal action. + */ +function simplenews_send_email_action_form($context) { + // Set default values for form. + if (!isset($context['recipient'])) { + $context['recipient'] = ''; + } + + $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.'), + ); + + if (!isset($context['newsletter'])) { + $context['newsletter'] = array(); + } + + $tree = taxonomy_get_tree(variable_get('simplenews_vid', '')); + $terms = array(); + foreach ($tree as $newsletter) { + $terms[$newsletter->tid] = check_plain($newsletter->name); + } + $form['newsletter'] = array( + '#title' => t('Newsletter'), + '#type' => 'select', + '#multiple' => 'true', + '#options' => $terms, + '#description' => t('The newsletter series (select as many as you like) of which the recipient will be alerted of subscriptions or unsubscriptions.'), + ); + + return $form; +} + +/** + * Process simplenews_send_email_action form submissions. + */ +function simplenews_send_email_action_submit($form, $form_state) { + $form_values = $form_state['values']; + + $params = array( + 'recipient' => $form_values['recipient'], + 'newsletter' => $form_values['newsletter'], + ); + return $params; +} + +/** + * Validate simplenews_send_email_action form submissions. + */ +function simplenews_send_email_action_validate($form, $form_state) { + $form_values = $form_state['values']; + // Validate the configuration form. + if (!valid_email_address($form_values['recipient'])) { + form_set_error('recipient', t('Please enter a valid email address.')); + } +} + + +/** * Implementation of hook_hook_info(). */ function simplenews_hook_info() {