function views_send_queue_mail in Views Send 8
Same name and namespace in other branches
- 7 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 objecy.
1 call to views_send_queue_mail()
- ViewsSend::viewsFormSubmit in src/
Plugin/ views/ field/ ViewsSend.php - Overrides \Drupal\system\Plugin\views\field\BulkForm::viewsFormSubmit().
File
- ./
views_send.module, line 444 - The Views Send module.
Code
function views_send_queue_mail($params, $selected_rows, $view) {
$account = Drupal::currentUser();
if (!$account
->hasPermission('mass mailing with views_send')) {
\Drupal::messenger()
->addError(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::fromRoute('user.admin_permissions', [], [
'fragment' => 'module-views_send',
])
->toString(),
)));
return;
}
$formats = filter_formats($account);
// From: parts.
$from_mail = trim($params['views_send_from_mail']);
$from_name = $params['views_send_from_name'];
$to_mail_key = $params['views_send_tokens'][$params['views_send_to_mail']];
$to_name_key = $params['views_send_tokens'][$params['views_send_to_name']];
foreach ($selected_rows as $row_id) {
$to_mail = _views_send_strip_html($view->style_plugin
->getField($row_id, $to_mail_key));
$to_name = _views_send_strip_html($view->style_plugin
->getField($row_id, $to_name_key));
$subject = $params['views_send_subject'];
$body = $params['views_send_message']['value'];
$params['format'] = $params['views_send_message']['format'];
// This shouldn't happen, but better be 100% sure.
/* FIXME
if (!$formats[$params['format']]->access('use', $account)) {
drupal_set_message(t('No mails sent since an illegale format is selected for the message.'));
return;
}
*/
$body = check_markup($body, $params['format']);
// 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[] = (string) $view->style_plugin
->getField($row_id, $field_name);
}
// Views Send specific token replacements
$subject = str_replace($token_keys, $token_values, $subject);
$body = 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'] = User::load($view->result[$row_id]->nid);
}
$token_service = \Drupal::service('token');
$subject = $token_service
->replace($subject, $data);
$body = $token_service
->replace($body, $data);
if (!VIEWS_SEND_MIMEMAIL || \Drupal::config('mimemail.settings')
->get('format') == 'plain_text') {
$body = MailFormatHelper::htmlToText($body);
}
else {
// This is needed to avoid escaping of HTML in Swiftmailer.
$body = Markup::create($body);
}
if ($params['format'] == 'plain_text') {
$plain_format = TRUE;
}
else {
$plain_format = FALSE;
}
// We transform receipt, priority in headers,
// merging them to the user defined headers.
$headers = _views_send_headers($params['views_send_receipt'], $params['views_send_priority'], $from_mail, $params['views_send_headers']);
$attachments = !empty($params['views_send_attachments']) ? $params['views_send_attachments'] : array();
$message = array(
'uid' => $account
->id(),
'timestamp' => time(),
'from_name' => $from_name,
'from_mail' => $from_mail,
'to_name' => $to_name,
'to_mail' => $to_mail,
'subject' => _views_send_strip_html($subject),
'body' => $body,
'headers' => $headers,
);
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);
// Removing stuff added because Swift Mailer and Mandrill doesn't
// handle attachments in the format function.
if (\Drupal::moduleHandler()
->moduleExists('swiftmailer') || \Drupal::moduleHandler()
->moduleExists('mandrill')) {
unset($message['params']);
}
// Queue the message to the spool table.
\Drupal::database()
->insert('views_send_spool')
->fields($message)
->execute();
$event = new MailAddedEvent($message);
$event_dispatcher = \Drupal::service('event_dispatcher');
$event_dispatcher
->dispatch(MailAddedEvent::EVENT_NAME, $event);
}
}
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,
),
);
}
$operations[] = array(
'views_send_remove_uploaded',
array(
$attachments,
),
);
$batch = array(
'operations' => $operations,
'finished' => 'views_send_batch_deliver_finished',
'progress_message' => t('Sent @current of @total messages.'),
);
batch_set($batch);
\Drupal::messenger()
->addMessage(\Drupal::translation()
->formatPlural(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;
\Drupal::database()
->insert('views_send_spool')
->fields($message)
->execute();
}
views_send_remove_uploaded($attachments);
\Drupal::messenger()
->addMessage(\Drupal::translation()
->formatPlural(count($selected_rows), '1 message added to the spool.', '@count messages added to the spool.'));
$event = new AllMailAddedEvent(count($selected_rows));
$event_dispatcher = \Drupal::service('event_dispatcher');
$event_dispatcher
->dispatch(AllMailAddedEvent::EVENT_NAME, $event);
}
}