function simplenews_mail in Simplenews 6.2
Same name and namespace in other branches
- 8.2 simplenews.module \simplenews_mail()
- 8 simplenews.module \simplenews_mail()
- 6 simplenews.module \simplenews_mail()
- 7.2 simplenews.module \simplenews_mail()
- 7 simplenews.module \simplenews_mail()
- 3.x simplenews.module \simplenews_mail()
Implementation of hook_mail().
Send simplenews mails using drupal mail API
Parameters
$key: node | test | subscribe | unsubscribe:
array $message message array: [from] [headers][From] [language] : preferred message language
array $params parameter array: [context][node] : node object of message to be sent [context][snid] : used for $key = subscribe or unsubscribe [context][from_name] : name of mail sender or site name (optional) [context][account] : account details of recipient [from] : array('address' => 'noreply@example.org', 'formatted' => 'site name <noreply@example.org>') [newsletter] : newsletter object (tid, name) [tokens] : tokens for variable replacement. Defaults to: user_mail_tokens()
$reset: Reset the static cache, only used internally for tests.
See also
1 call to simplenews_mail()
- SimplenewsSendTestCase::setUp in tests/
simplenews.test - Generates a random database prefix, runs the install scripts on the prefixed database and enable the specified modules. After installation many caches are flushed and the internal browser is setup so that the page requests will run on the new prefix.…
File
- ./
simplenews.module, line 1758 - Simplenews node handling, sent email, newsletter block and general hooks
Code
function simplenews_mail($key, &$message, $params, $reset = FALSE) {
// Message header, body and mail headers are buffered to increase
// performance 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 retrieved from the
// static $messages variable.
// $message buffer is node and language specific.
static $messages = array();
if ($reset) {
$messages = array();
return;
}
module_load_include('inc', 'simplenews', 'includes/simplenews.mail');
$context = $params['context'];
$langcode = $message['language']->language;
switch ($key) {
case 'node':
case 'test':
// 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;
if (module_exists('translation')) {
// If the node has translations and a translation is required
// the equivalent of the node in the required language 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.
if ($nid != $context['node']->nid && !isset($messages[$nid][$langcode])) {
$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.
if (!isset($messages[$nid][$langcode])) {
$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'];
$message['headers'] = $messages[$nid][$langcode]['headers'] = $headers;
// Build email subject
$subject = '[[simplenews-newsletters-name]] [title-raw]';
if ($tid = $node->simplenews['tid']) {
$term = taxonomy_get_term($tid);
// Translate the newsletter term name if simplenews vocabulary uses Localized terms.
$name = _simplenews_tt_newsletter_name($term, $langcode);
$subject = variable_get('simplenews_email_subject_' . $term->tid, $subject);
}
else {
$name = t('Unassigned newsletter');
}
//$subject = theme('simplenews_newsletter_subject', $name, $node->title, $message['language']);
$subject = token_replace($subject, 'simplenews', $context);
$subject = token_replace($subject, 'node', $context['node']);
$subject = str_replace(array(
"\r",
"\n",
), '', $subject);
$message['subject'] = $messages[$nid][$langcode]['subject'] = $subject;
// Set the active language to the node's language.
// This needs to be done as otherwise the language used to send the mail
// is the language of the user logged in.
if (module_exists('i18n')) {
i18n_selection_mode('node', $node->language);
}
// 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
$messages[$nid][$langcode]['body'] = $body;
if (variable_get('simplenews_opt_inout_' . $tid, 'double') != 'hidden') {
// TODO: Unsubscribe links are broken if key=='test' && $context['snid']==0
// We might pass snid / context to theme functions.
// Build and buffer message footer
$footer = theme(array(
'simplenews_newsletter_footer__' . $context['node']->simplenews['tid'],
'simplenews_newsletter_footer',
), $context, $key, $message['language']);
$messages[$nid][$langcode]['footer'] = $footer;
}
// Reset the language to the original settings.
if (module_exists('i18n')) {
i18n_selection_mode('reset');
}
}
else {
// Get message data from buffer
$message['headers'] = $messages[$nid][$langcode]['headers'];
$message['subject'] = $messages[$nid][$langcode]['subject'];
$body = $messages[$nid][$langcode]['body'];
$footer = $messages[$nid][$langcode]['footer'];
}
// Build message body, replace tokens.
$body = token_replace($body, 'simplenews', $context);
// Convert to plain text if required.
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.
$footer = token_replace($footer, 'simplenews', $context);
$message['body']['footer'] = $footer;
// Add user specific header data.
$message['headers']['List-Unsubscribe'] = '<' . token_replace('[simplenews-unsubscribe-url]', 'simplenews', $context) . '>';
break;
case 'subscribe':
// Use formatted from address "name" <mail_address>
$message['headers']['From'] = $params['from']['formatted'];
$message['subject'] = _simplenews_subscription_confirmation_text('subscribe_subject', $langcode);
$message['subject'] = token_replace($message['subject'], 'simplenews_subscription', $context);
if (simplenews_user_is_subscribed($context['account']->mail, $context['newsletter']->tid)) {
$message['body'] = _simplenews_subscription_confirmation_text('subscribe_subscribed', $langcode);
$message['body'] = token_replace($message['body'], 'simplenews_subscription', $context);
}
else {
$message['body'] = _simplenews_subscription_confirmation_text('subscribe_unsubscribed', $langcode);
$message['body'] = token_replace($message['body'], 'simplenews_subscription', $context);
}
break;
case 'unsubscribe':
// Use formatted from address "name" <mail_address>
$message['headers']['From'] = $params['from']['formatted'];
$message['subject'] = _simplenews_subscription_confirmation_text('subscribe_subject', $langcode);
$message['subject'] = token_replace($message['subject'], 'simplenews_subscription', $context);
if (simplenews_user_is_subscribed($context['account']->mail, $context['newsletter']->tid)) {
$message['body'] = _simplenews_subscription_confirmation_text('unsubscribe_subscribed', $langcode);
$message['body'] = token_replace($message['body'], 'simplenews_subscription', $context);
}
else {
$message['body'] = _simplenews_subscription_confirmation_text('unsubscribe_unsubscribed', $langcode);
$message['body'] = token_replace($message['body'], 'simplenews_subscription', $context);
}
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<br />Subject: %subject<br />Recipient: %to', array(
'%type' => $key,
'%to' => $message['to'],
'%subject' => $message['subject'],
), WATCHDOG_DEBUG);
}
}