function views_send_queue_mail in Views Send 7
Same name and namespace in other branches
- 8 views_send.module \views_send_queue_mail()
Assembles the email and queues it for sending.
Also email sent directly using the Batch API is handled here.
Parameters
$params: Data entered in the "config" step of the form.
$selected_rows: An array with the indexes of the selected views rows.
$view: The actual view object.
1 call to views_send_queue_mail()
- views_send_form_submit in ./
views_send.module - Submit handler for all steps of the Views Send multistep form.
File
- ./
views_send.module, line 678 - The Views Send module.
Code
function views_send_queue_mail($params, $selected_rows, $view) {
global $user;
global $language;
if (!user_access('mass mailing with views_send')) {
drupal_set_message(t('No mails sent since you aren\'t allowed to send mass mail with Views. (<a href="@permurl">Edit the permission.</a>)', array(
'@permurl' => url('admin/people/permissions', array(
'fragment' => 'module-views_send',
)),
)), 'error');
return;
}
// From: parts.
$from_mail = trim($params['views_send_from_mail']);
$from_name = $params['views_send_from_name'];
// To: parts. (Mail is mandatory, name is optional.)
$to_mail_key = $params['views_send_tokens'][$params['views_send_to_mail']];
if (!empty($params['views_send_to_name'])) {
$to_name_key = $params['views_send_tokens'][$params['views_send_to_name']];
}
else {
$to_name_key = false;
$to_name = '';
}
$subject = $params['views_send_subject'];
$body = $params['views_send_message']['value'];
$headers = _views_send_headers($params['views_send_receipt'], $params['views_send_priority'], $from_mail, $params['views_send_headers']);
$format = $params['views_send_message']['format'];
$attachments = $params['views_send_attachments'];
$formats = filter_formats();
if (!filter_access($formats[$format])) {
drupal_set_message(t('No mails sent since an illegale format is selected for the message.'));
return;
}
else {
$body = check_markup($body, $format);
}
if ($format == 'plain_text') {
$plain_format = TRUE;
}
else {
$plain_format = FALSE;
}
$message_base = array(
'uid' => $user->uid,
'from_name' => trim($from_name),
'from_mail' => trim($from_mail),
'headers' => $headers,
);
foreach ($selected_rows as $selected_rows_key => $row_id) {
// To: parts.
$to_mail = implode(',', _views_send_get_field_value_from_views_row($view, $row_id, $to_mail_key, 'mail'));
if ($to_name_key) {
$to_name = _views_send_get_field_value_from_views_row($view, $row_id, $to_name_key, 'plain_text');
}
// Populate row/context tokens.
$token_keys = $token_values = array();
foreach ($params['views_send_tokens'] as $field_key => $field_name) {
$token_keys[] = VIEWS_SEND_TOKEN_PREFIX . sprintf(VIEWS_SEND_TOKEN_PATTERN, $field_name) . VIEWS_SEND_TOKEN_POSTFIX;
$token_values[] = _views_send_get_field_value_from_views_row($view, $row_id, $field_name);
}
// Views Send specific token replacements
$subject_expanded = str_replace($token_keys, $token_values, $subject);
$body_expanded = str_replace($token_keys, $token_values, $body);
// Global token replacement, and node/user token replacements
// if a nid/uid is found in the views result row.
$data = array();
if (property_exists($view->result[$row_id], 'uid')) {
$data['user'] = user_load($view->result[$row_id]->uid);
}
if (property_exists($view->result[$row_id], 'nid')) {
$data['node'] = node_load($view->result[$row_id]->nid);
}
$subject_expanded = token_replace($subject_expanded, $data);
$body_expanded = token_replace($body_expanded, $data, array(
'language' => $language,
'callback' => 'user_mail_tokens',
'sanitize' => FALSE,
'clear' => TRUE,
));
if (!VIEWS_SEND_MIMEMAIL || variable_get('mimemail_format', 'plain_text') == 'plain_text') {
$body_expanded = drupal_html_to_text($body_expanded);
}
$message = $message_base + array(
'timestamp' => time(),
'to_name' => trim($to_name),
'to_mail' => trim($to_mail),
'subject' => strip_tags($subject_expanded),
'body' => $body_expanded,
);
// Enable other modules to alter the actual message before queueing it
// by providing the hook 'views_send_mail_alter'.
$views_send_token_data = array(
'keys' => $token_keys,
'values' => $token_values,
);
drupal_alter('views_send_mail', $message, $params, $data, $views_send_token_data);
if ($params['views_send_direct']) {
$operations[] = array(
'views_send_batch_deliver',
array(
$message,
$plain_format,
$attachments,
),
);
}
else {
_views_send_prepare_mail($message, $plain_format, $attachments);
// Only queue the message if it hasn't been cancelled by another module.
if ($message['send']) {
unset($message['send']);
// Removing stuff added because Swift Mailer and Mandrill doesn't
// handle attachments in the format function.
if (module_exists('swiftmailer') || module_exists('mandrill')) {
unset($message['params']);
}
db_insert('views_send_spool')
->fields($message)
->execute();
if (module_exists('rules')) {
rules_invoke_event('views_send_email_added_to_spool', $message);
}
// Enabled other modules to act just after a message is queued
// by providing the hook 'views_send_mail_queued'.
module_invoke_all('views_send_mail_queued', $message, $view, $row_id);
}
else {
unset($selected_rows[$selected_rows_key]);
}
}
}
if ($params['views_send_direct']) {
if ($params['views_send_carbon_copy']) {
$message['to_name'] = $from_name;
$message['to_mail'] = $from_mail;
$operations[] = array(
'views_send_batch_deliver',
array(
$message,
$plain_format,
$attachments,
),
);
}
$batch = array(
'operations' => $operations,
'finished' => 'views_send_batch_deliver_finished',
'progress_message' => t('Sent @current of @total messages.'),
);
batch_set($batch);
drupal_set_message(format_plural(count($selected_rows), '1 message processed.', '@count messages processed.'));
}
else {
if ($params['views_send_carbon_copy']) {
$message['to_name'] = $from_name;
$message['to_mail'] = $from_mail;
db_insert('views_send_spool')
->fields($message)
->execute();
}
drupal_set_message(format_plural(count($selected_rows), '1 message added to the spool.', '@count messages added to the spool.'));
if (module_exists('rules')) {
rules_invoke_event('views_send_all_email_added_to_spool', count($selected_rows));
}
}
}