function _webform_submission_prepare_mail in Webform 7.4
Prepare a submission email for use by webform_submission_send_mail()
Parameters
$node: The node object containing the current webform.
$submission: The webform submission object to be used in sending e-mails.
$email: The e-mail settings to be used. This will have some of its values adjusted.
Return value
array|null An array of the information about the email needed by drupal_mail().
2 calls to _webform_submission_prepare_mail()
- WebformSubmissionTestCase::testWebformSubmission in tests/
WebformSubmissionTestCase.test - Test sending a submission and check database integrity.
- webform_submission_send_mail in includes/
webform.submissions.inc - Send related e-mails related to a submission.
File
- includes/
webform.submissions.inc, line 287 - Submission handling functions.
Code
function _webform_submission_prepare_mail($node, $submission, &$email) {
global $user;
// Set the HTML property based on availablity of MIME Mail.
$email['html'] = $email['html'] && webform_variable_get('webform_email_html_capable');
// Pass through the theme layer if using the default template.
if ($email['template'] == 'default') {
$email['message'] = theme(array(
'webform_mail_' . $node->nid,
'webform_mail',
'webform_mail_message',
), array(
'node' => $node,
'submission' => $submission,
'email' => $email,
));
}
else {
$email['message'] = $email['template'];
}
// Replace tokens in the message.
$email['message'] = webform_replace_tokens($email['message'], $node, $submission, $email, (bool) $email['html']);
// Build the e-mail headers.
$email['headers'] = theme(array(
'webform_mail_headers_' . $node->nid,
'webform_mail_headers',
), array(
'node' => $node,
'submission' => $submission,
'email' => $email,
));
// Assemble the From string.
if (isset($email['headers']['From'])) {
// If a header From is already set, don't override it.
$email['from'] = $email['headers']['From'];
unset($email['headers']['From']);
}
else {
// Format the From address.
$mapping = isset($email['extra']['from_address_mapping']) ? $email['extra']['from_address_mapping'] : NULL;
$email['from'] = webform_format_email_address($email['from_address'], $email['from_name'], $node, $submission, TRUE, TRUE, NULL, $mapping);
}
// If "Use Reply-To header" is set in Webform settings and the Reply-To
// header is not already set, set Reply-To to the From and set From address
// to webform_default_from_name and webform_default_from_address.
if (webform_variable_get('webform_email_replyto') && empty($email['headers']['Reply-To']) && ($default_from_name = webform_variable_get('webform_default_from_name')) && ($default_from_address = webform_variable_get('webform_default_from_address')) && $email['from'] !== $default_from_name) {
$email['headers']['Reply-To'] = $email['from'];
$email['from'] = $default_from_address;
if (webform_variable_get('webform_email_address_format') == 'long') {
$email_parts = webform_parse_email_address($email['headers']['Reply-To']);
$from_name = t('!name via !site_name', array(
'!name' => strlen($email_parts['name']) ? $email_parts['name'] : $email_parts['address'],
'!site_name' => $default_from_name,
));
$from_name = str_replace('"', "'", $from_name);
$from_name = implode(' ', array_map('mime_header_encode', explode(' ', $from_name)));
$email['from'] = '"' . $from_name . '" <' . $email['from'] . '>';
}
}
// Update the subject if set in the themed headers.
if (isset($email['headers']['Subject'])) {
$email['subject'] = $email['headers']['Subject'];
unset($email['headers']['Subject']);
}
else {
$email['subject'] = webform_format_email_subject($email['subject'], $node, $submission);
}
// Update the to e-mail if set in the themed headers.
if (isset($email['headers']['To'])) {
$email['email'] = $email['headers']['To'];
unset($email['headers']['To']);
$addresses = array_filter(explode(',', $email['email']));
}
else {
// Format the To address(es).
$mapping = isset($email['extra']['email_mapping']) ? $email['extra']['email_mapping'] : NULL;
$addresses = webform_format_email_address($email['email'], NULL, $node, $submission, TRUE, FALSE, NULL, $mapping);
$email['email'] = implode(',', $addresses);
}
// Generate the list of addresses that this e-mail will be sent to.
$addresses_final = array_filter($addresses, 'webform_valid_email_address_filter');
if (!$addresses_final) {
return;
}
// Verify that this submission is not attempting to send any spam hacks.
foreach ($addresses_final as $address) {
if (_webform_submission_spam_check($address, $email['subject'], $email['from'], $email['headers'])) {
watchdog('webform', 'Possible spam attempt from @remote !message', array(
'@remote' => ip_address(),
'!message' => "<br />\n" . nl2br(htmlentities($email['message'])),
));
drupal_set_message(t('Illegal information. Data not submitted.'), 'error');
return;
}
}
// Consolidate addressees into one message if permitted by configuration.
$send_increment = 1;
if (!webform_variable_get('webform_email_address_individual')) {
$send_increment = count($addresses_final);
$addresses_final = array(
implode(', ', $addresses_final),
);
}
// Prepare the variables used by drupal_mail().
$language = $user->uid ? user_preferred_language($user) : language_default();
$mail_params = array(
'message' => $email['message'],
'subject' => $email['subject'],
'headers' => $email['headers'],
'node' => $node,
'submission' => $submission,
'email' => $email,
);
if (webform_variable_get('webform_email_html_capable')) {
// Load attachments for the e-mail.
$attachments = array();
if ($email['attachments']) {
webform_component_include('file');
foreach ($node->webform['components'] as $component) {
if (webform_component_feature($component['type'], 'attachment') && !empty($submission->data[$component['cid']][0])) {
if (webform_component_implements($component['type'], 'attachments')) {
$files = webform_component_invoke($component['type'], 'attachments', $component, $submission->data[$component['cid']]);
if ($files) {
$attachments = array_merge($attachments, $files);
}
}
}
}
}
// Add the attachments to the mail parameters.
$mail_params['attachments'] = $attachments;
// Set all other properties for HTML e-mail handling.
$mail_params['plain'] = !$email['html'];
$mail_params['plaintext'] = $email['html'] ? NULL : $email['message'];
$mail_params['headers'] = $email['headers'];
if ($email['html']) {
// MIME Mail requires this header or it will filter all text.
$mail_params['headers']['Content-Type'] = 'text/html; charset=UTF-8';
}
}
return array(
'addresses_final' => $addresses_final,
'send_increment' => $send_increment,
'language' => $language,
'mail_params' => $mail_params,
);
}