'noreply@example.org', 'formatted' => 'site name ') * [newsletter] : newsletter object (tid, name) * [tokens] : tokens for variable replacement. Defaults to: user_mail_tokens() */ function simplenews_mail($key, &$message, $params) { $context = $params['context']; $build_mode = 'mail_'.$context['node']->simplenews['s_format']; switch ($key) { case 'node': case 'test': // Message header, body and mail headers are buffered to increase // perfomance when sending multiple mails. Buffered data only contains // general data, no recipient specific content. Tokens are used // for recipient data and will later be replaced. // When mailing multiple newsletters in one page call or cron run, // data is once stored and subsequently retreived from the // static $messages variable. // $message buffer is node and language specific. static $messages = array(); // By default the the node is send which is supplied in the function call. // When translation is used, the availability of translations is checked // and when available the translation of the preferred language is selected. $nid = $context['node']->nid; $langcode = $message['language']->language; if (module_exists('translation')) { // If the node has translations and a translation is required // the equivalent of the node in the required langugage is used // or the base node (nid == tnid) is used. if ($tnid = $context['node']->tnid) { if ($langcode != $context['node']->language) { $translations = translation_node_get_translations($tnid); // A translation is available in the preferred language. if ($translation = $translations[$langcode]) { $nid = $translation->nid; $langcode = $translation->language; } else { // No translation found which matches the preferred language. foreach ($translations as $translation) { if ($translation->nid == $tnid) { $nid = $tnid; $langcode = $translation->language; break; } } } } } // If a translation of the node is used and this node is not available in // the message buffer, then load this node. // OLD VERSION // if ($nid != $context['node']->nid && !isset($messages[$nid][$langcode])) { // MODIFIED VERSION >>> if ($nid != $context['node']->nid && !isset($messages[$nid][$langcode][$build_mode])) { // <<< MODIFIED VERSION END $context['node'] = node_load($nid); } } // Check if this node-language pair has been buffered. // If not, build the message and store it for later use. // OLD VERSION // if (!isset($messages[$nid][$langcode])) { // MODIFIED VERSION >>> if (!isset($messages[$nid][$langcode][$build_mode])) { // <<< MODIFIED VERSION END $node = drupal_clone($context['node']); // Add simplenews specific header data $headers = array_merge($message['headers'], _simplenews_headers($node, $params['from']['address'])); $headers['From'] = $params['from']['formatted']; // OLD VERSION // $message['headers'] = $messages[$nid][$langcode]['headers'] = $headers; // MODIFIED VERSION >>> $message['headers'] = $messages[$nid][$langcode][$build_mode]['headers'] = $headers; // <<< MODIFIED VERSION END // Build email subject if ($tid = $node->simplenews['tid']) { $term = taxonomy_get_term($tid); // Translate the newsletter term name if simplenews vocabulary uses Localized terms. if (module_exists('i18ntaxonomy') && i18ntaxonomy_vocabulary(variable_get('simplenews_vid', '')) == I18N_TAXONOMY_LOCALIZE) { $name = tt('taxonomy:term:'. $tid .':name', $term->name, $langcode); } else { $name = $term->name; } } else { $name = t('Unassigned newsletter'); } $subject = theme('simplenews_newsletter_subject', $name, $node->title, $message['language']); $subject = str_replace(array("\r", "\n"), '', $subject); // OLD VERSION // $message['subject'] = $messages[$nid][$langcode]['subject'] = $subject; // MODIFIED VERSION >>> $message['subject'] = $messages[$nid][$langcode][$build_mode]['subject'] = $subject; // <<< MODIFIED VERSION END // Build message body // Processing node body mimics node_view() with full node view $node->build_mode = 'email_'. $node->simplenews['s_format']; $node = node_build_content($node, FALSE, TRUE); $content = drupal_render($node->content); $node->body = $content; unset($node->teaser); // Set a flag to prevent token replacement during node alter. $node->simplenews_mail = TRUE; node_invoke_nodeapi($node, 'alter', FALSE, TRUE); unset($node->simplenews_mail); $body = theme(array('simplenews_newsletter_body__'. $context['node']->simplenews['tid'], 'simplenews_newsletter_body'), $node, $message['language']); // Buffer body text node and language specific // OLD VERSION // $messages[$nid][$langcode]['body'] = $body; // MODIFIED VERSION >>> $messages[$nid][$langcode][$build_mode]['body'] = $body; // <<< MODIFIED VERSION END // Build and buffer message footer $footer = theme(array('simplenews_newsletter_footer__'. $context['node']->simplenews['tid'], 'simplenews_newsletter_footer'), $context['node'], $key, $message['language']); // OLD VERSION // $messages[$nid][$langcode]['footer'] = $footer; // MODIFIED VERSION >>> $messages[$nid][$langcode][$build_mode]['footer'] = $footer; // <<< MODIFIED VERSION END } else { // Get message data from buffer // OLD VERSION // $message['headers'] = $messages[$nid][$langcode]['headers']; // $message['subject'] = $messages[$nid][$langcode]['subject']; // $body = $messages[$nid][$langcode]['body']; // $footer = $messages[$nid][$langcode]['footer']; // MODIFIED VERSION >>> $message['headers'] = $messages[$nid][$langcode][$build_mode]['headers']; $message['subject'] = $messages[$nid][$langcode][$build_mode]['subject']; $body = $messages[$nid][$langcode][$build_mode]['body']; $footer = $messages[$nid][$langcode][$build_mode]['footer']; // <<< MODIFIED VERSION END } // Build message body. // Replace tokens with user specific data and // Convert to plain text if required. $variables = simplenews_mail_tokens($context['account'], $context, is_object($context['account']->language) ? $context['account']->language : language_default()); $body = strtr($body, $variables); if ($context['node']->simplenews['s_format'] == 'plain') { $body = simplenews_html_to_text($body, variable_get('simplenews_hyperlinks_'. $context['node']->simplenews['tid'], 1)); } $message['body']['body'] = $body; // Build message footer. // Replace tokens with user specific data $message['body']['footer'] = strtr($footer, $variables); // Add user specific header data. $message['headers']['List-Unsubscribe'] = strtr('', $variables); break; case 'subscribe': // Use formatted from address "name" $message['headers']['From'] = $params['from']['formatted']; $variables = simplenews_mail_tokens($context['account'], $context, is_object($context['account']->language) ? $context['account']->language : language_default()); $message['subject'] = _simplenews_subscription_confirmation_text('subscribe_subject', $context['account']->language, $variables); if (simplenews_user_is_subscribed($context['account']->mail, $context['newsletter']->tid)) { $message['body'] = _simplenews_subscription_confirmation_text('subscribe_subscribed', $context['account']->language, $variables); } else { $message['body'] = _simplenews_subscription_confirmation_text('subscribe_unsubscribed', $context['account']->language, $variables); } break; case 'unsubscribe': // Use formatted from address "name" $message['headers']['From'] = $params['from']['formatted']; $variables = simplenews_mail_tokens($context['account'], $context, is_object($context['account']->language) ? $context['account']->language : language_default()); $message['subject'] = _simplenews_subscription_confirmation_text('subscribe_subject', $context['account']->language, $variables); if (simplenews_user_is_subscribed($context['account']->mail, $context['newsletter']->tid)) { $message['body'] = _simplenews_subscription_confirmation_text('unsubscribe_subscribed', $context['account']->language, $variables); } else { $message['body'] = _simplenews_subscription_confirmation_text('unsubscribe_unsubscribed', $context['account']->language, $variables); } break; } // Debug message to check for outgoing emails messages. // Debug message of node and test emails is set in simplenews_mail_mail(). if (variable_get('simplenews_debug', FALSE) && $key != 'node' && $key != 'test') { watchdog('simplenews', 'Outgoing email. Message type: %type
Subject: %subject
Recipient: %to', array('%type' => $key, '%to' => $message['to'], '%subject' => $message['subject']), WATCHDOG_DEBUG); } } /** * Send a node to an email address. * * @param $nid node id of newsletter node * @param $vid revision id of newsletter node * @param $mail target email address * @param $key email key [node|test] * * @return TRUE if email is succesfully delivered by php mail() */ function simplenews_mail_mail($nid, $vid, $mail, $key = 'node') { // Get subscription data for recipient and language $account = new stdClass(); $account->mail = $mail; $subscription = simplenews_get_subscription($account); $params['context']['account'] = $subscription; // Get node data for the mail $node = node_load($nid, $vid); if (is_object($node)) { $params['from'] = _simplenews_set_from($node); $params['context']['newsletter'] = taxonomy_get_term($node->simplenews['tid']); $params['context']['node'] = $node; // Send mail if (module_exists('mimemail')) { // If mimemail module is installed ALL emails are send via this module. // drupal_mail() builds the content of the email but does NOT send. $message = drupal_mail('simplenews', $key, $subscription->mail, $subscription->language, $params, $params['from']['formatted'], FALSE); $plain = $message['params']['context']['node']->simplenews['s_format'] == 'plain'; // MODIFICATION STARTS HERE (donquixote) >>> if (!$plain) { // call simplenews_mail again, this time to produce a plaintext version. // $params['simplenews_s_format'] = 'plain'; $s_format = $params['context']['node']->simplenews['s_format']; $params['context']['node']->simplenews['s_format'] = 'plain'; $message_plain = drupal_mail('simplenews', $key, $subscription->mail, $subscription->language, $params, $params['from']['formatted'], FALSE); $params['context']['node']->simplenews['s_format'] = $s_format; // $params['simplenews_s_format'] = null; } // <<< MODIFICATION ENDS HERE (donquixote) $message['result'] = mimemail( $message['from'], $message['to'], $message['subject'], $message['body'], $plain, $message['headers'], // OLD VERSION // $plain ? $message['body'] : simplenews_html_to_text($message['body'], TRUE), // MODIFIED CODE >>> $plain ? $message['body'] : $message_plain['body'], // <<< END MODIFICATION isset($message['params']['context']['node']->files) ? $message['params']['context']['node']->files : array(), '' ); } else { $message = drupal_mail('simplenews', $key, $subscription->mail, $subscription->language, $params, $params['from']['address'], TRUE); } // Log sent message. if (variable_get('simplenews_debug', FALSE)) { if (module_exists('mimemail')) { $via_mimemail = t('Sent via Mime Mail'); } //TODO Add line break before %mimemail. if ($message['result']) { watchdog('simplenews', 'Outgoing email. Message type: %type
Subject: %subject
Recipient: %to %mimemail', array('%type' => $key, '%to' => $message['to'], '%subject' => $message['subject'], '%mimemail' => $via_mimemail), WATCHDOG_DEBUG); } else { watchdog('simplenews', 'Outgoing email failed. Message type: %type
Subject: %subject
Recipient: %to %mimemail', array('%type' => $key, '%to' => $message['to'], '%subject' => $message['subject'], '%mimemail' => $via_mimemail), WATCHDOG_ERROR); } } } else { watchdog('simplenews', 'Newsletter not send: node does not exist (nid = @nid; vid = @vid).', array('@nid' => $message['nid'], '@vid' => $message['vid']), WATCHDOG_ERROR); } return isset($message['result']) ? $message['result'] : FALSE; }