Revision 76bdcd04
Added by Assos Assos almost 6 years ago
drupal7/sites/all/modules/webform/includes/webform.submissions.inc | ||
---|---|---|
233 | 233 |
* Number of mail sent. |
234 | 234 |
*/ |
235 | 235 |
function webform_submission_send_mail($node, $submission, $emails = NULL) { |
236 |
global $user; |
|
237 |
|
|
238 | 236 |
// Get the list of e-mails we'll be sending. |
239 | 237 |
$emails = isset($emails) ? $emails : $node->webform['emails']; |
240 | 238 |
|
241 | 239 |
// Create a themed message for mailing. |
242 | 240 |
$send_count = 0; |
243 | 241 |
foreach ($emails as $eid => $email) { |
244 |
// Continue with next email recipients array if disabled for current.
|
|
245 |
if (!$email['status']) {
|
|
242 |
$mail = _webform_submission_prepare_mail($node, $submission, $email);
|
|
243 |
if (!$mail) {
|
|
246 | 244 |
continue; |
247 | 245 |
} |
248 | 246 |
|
249 |
// Set the HTML property based on availablity of MIME Mail. |
|
250 |
$email['html'] = ($email['html'] && webform_variable_get('webform_email_html_capable')); |
|
247 |
$addresses_final = $mail['addresses_final']; |
|
248 |
$send_increment = $mail['send_increment']; |
|
249 |
$language = $mail['language']; |
|
250 |
$mail_params = $mail['mail_params']; |
|
251 | 251 |
|
252 |
// Pass through the theme layer if using the default template.
|
|
253 |
if ($email['template'] == 'default') {
|
|
254 |
$email['message'] = theme(array('webform_mail_' . $node->nid, 'webform_mail', 'webform_mail_message'), array('node' => $node, 'submission' => $submission, 'email' => $email));
|
|
255 |
}
|
|
256 |
else {
|
|
257 |
$email['message'] = $email['template'];
|
|
252 |
// Mail the webform results.
|
|
253 |
foreach ($addresses_final as $address) {
|
|
254 |
$message = drupal_mail('webform', 'submission', $address, $language, $mail_params, $email['from']);
|
|
255 |
if ($message['result']) {
|
|
256 |
$send_count += $send_increment;
|
|
257 |
}
|
|
258 | 258 |
} |
259 |
} |
|
259 | 260 |
|
260 |
// Replace tokens in the message.
|
|
261 |
$email['message'] = webform_replace_tokens($email['message'], $node, $submission, $email, (boolean) $email['html']);
|
|
261 |
return $send_count;
|
|
262 |
}
|
|
262 | 263 |
|
263 |
// Build the e-mail headers. |
|
264 |
$email['headers'] = theme(array('webform_mail_headers_' . $node->nid, 'webform_mail_headers'), array('node' => $node, 'submission' => $submission, 'email' => $email)); |
|
264 |
/** |
|
265 |
* Prepare a submission email for use by webform_submission_send_mail() |
|
266 |
* |
|
267 |
* @param $node |
|
268 |
* The node object containing the current webform. |
|
269 |
* @param $submission |
|
270 |
* The webform submission object to be used in sending e-mails. |
|
271 |
* @param $email |
|
272 |
* The e-mail settings to be used. This will have some of its values adjusted. |
|
273 |
* |
|
274 |
* @return array|null |
|
275 |
* An array of the information about the email needed by drupal_mail(). |
|
276 |
*/ |
|
277 |
function _webform_submission_prepare_mail($node, $submission, &$email) { |
|
278 |
global $user; |
|
265 | 279 |
|
266 |
// Assemble the From string. |
|
267 |
if (isset($email['headers']['From'])) { |
|
268 |
// If a header From is already set, don't override it. |
|
269 |
$email['from'] = $email['headers']['From']; |
|
270 |
unset($email['headers']['From']); |
|
271 |
} |
|
272 |
else { |
|
273 |
// Format the From address. |
|
274 |
$mapping = isset($email['extra']['from_address_mapping']) ? $email['extra']['from_address_mapping'] : NULL; |
|
275 |
$email['from'] = webform_format_email_address($email['from_address'], $email['from_name'], $node, $submission, TRUE, TRUE, NULL, $mapping); |
|
276 |
} |
|
280 |
// Don't process disabled emails. |
|
281 |
if (!$email['status']) { |
|
282 |
return; |
|
283 |
} |
|
277 | 284 |
|
278 |
// If requested and not already set, set Reply-To to the From and re-format From address. |
|
279 |
if (webform_variable_get('webform_email_replyto') && |
|
280 |
empty($email['headers']['Reply-To']) && |
|
281 |
($default_from_name = webform_variable_get('webform_default_from_name')) && |
|
282 |
($default_from_address = webform_variable_get('webform_default_from_address')) && |
|
283 |
($default_from_parts = explode('@', $default_from_address)) && |
|
284 |
count($default_from_parts) == 2 && |
|
285 |
$default_from_parts[1] && |
|
286 |
stripos($email['from'], '@' . $default_from_parts[1]) === FALSE) { |
|
287 |
// Message is not already being sent from the domain of the default |
|
288 |
// webform from address. |
|
289 |
$email['headers']['Reply-To'] = $email['from']; |
|
290 |
$email['from'] = $default_from_address; |
|
291 |
if (webform_variable_get('webform_email_address_format') == 'long') { |
|
292 |
$email_parts = webform_parse_email_address($email['headers']['Reply-To']); |
|
293 |
$from_name = t('!name via !site_name', |
|
294 |
array( |
|
295 |
'!name' => strlen($email_parts['name']) ? $email_parts['name'] : $email_parts['address'], |
|
296 |
'!site_name' => $default_from_name, |
|
297 |
)); |
|
298 |
$from_name = implode(' ', array_map('mime_header_encode', explode(' ', $from_name))); |
|
299 |
$email['from'] = '"' . $from_name . '" <' . $email['from'] . '>'; |
|
300 |
} |
|
301 |
} |
|
285 |
// Set the HTML property based on availablity of MIME Mail. |
|
286 |
$email['html'] = ($email['html'] && webform_variable_get('webform_email_html_capable')); |
|
302 | 287 |
|
303 |
// Update the subject if set in the themed headers. |
|
304 |
if (isset($email['headers']['Subject'])) { |
|
305 |
$email['subject'] = $email['headers']['Subject']; |
|
306 |
unset($email['headers']['Subject']); |
|
307 |
} |
|
308 |
else { |
|
309 |
$email['subject'] = webform_format_email_subject($email['subject'], $node, $submission); |
|
310 |
} |
|
288 |
// Pass through the theme layer if using the default template. |
|
289 |
if ($email['template'] == 'default') { |
|
290 |
$email['message'] = theme(array('webform_mail_' . $node->nid, 'webform_mail', 'webform_mail_message'), array('node' => $node, 'submission' => $submission, 'email' => $email)); |
|
291 |
} |
|
292 |
else { |
|
293 |
$email['message'] = $email['template']; |
|
294 |
} |
|
311 | 295 |
|
312 |
// Update the to e-mail if set in the themed headers. |
|
313 |
if (isset($email['headers']['To'])) { |
|
314 |
$email['email'] = $email['headers']['To']; |
|
315 |
unset($email['headers']['To']); |
|
316 |
$addresses = array_filter(explode(',', $email['email'])); |
|
317 |
} |
|
318 |
else { |
|
319 |
// Format the To address(es). |
|
320 |
$mapping = isset($email['extra']['email_mapping']) ? $email['extra']['email_mapping'] : NULL; |
|
321 |
$addresses = webform_format_email_address($email['email'], NULL, $node, $submission, TRUE, FALSE, NULL, $mapping); |
|
322 |
$email['email'] = implode(',', $addresses); |
|
323 |
} |
|
296 |
// Replace tokens in the message. |
|
297 |
$email['message'] = webform_replace_tokens($email['message'], $node, $submission, $email, (boolean) $email['html']); |
|
324 | 298 |
|
325 |
// Generate the list of addresses that this e-mail will be sent to.
|
|
326 |
$addresses_final = array_filter($addresses, 'webform_valid_email_address');
|
|
299 |
// Build the e-mail headers.
|
|
300 |
$email['headers'] = theme(array('webform_mail_headers_' . $node->nid, 'webform_mail_headers'), array('node' => $node, 'submission' => $submission, 'email' => $email));
|
|
327 | 301 |
|
328 |
if (!$addresses_final) { |
|
329 |
continue; |
|
330 |
} |
|
302 |
// Assemble the From string. |
|
303 |
if (isset($email['headers']['From'])) { |
|
304 |
// If a header From is already set, don't override it. |
|
305 |
$email['from'] = $email['headers']['From']; |
|
306 |
unset($email['headers']['From']); |
|
307 |
} |
|
308 |
else { |
|
309 |
// Format the From address. |
|
310 |
$mapping = isset($email['extra']['from_address_mapping']) ? $email['extra']['from_address_mapping'] : NULL; |
|
311 |
$email['from'] = webform_format_email_address($email['from_address'], $email['from_name'], $node, $submission, TRUE, TRUE, NULL, $mapping); |
|
312 |
} |
|
331 | 313 |
|
332 |
// Verify that this submission is not attempting to send any spam hacks. |
|
333 |
foreach ($addresses_final as $address) { |
|
334 |
if (_webform_submission_spam_check($address, $email['subject'], $email['from'], $email['headers'])) { |
|
335 |
watchdog('webform', 'Possible spam attempt from @remote !message', |
|
336 |
array('@remote' => ip_address(), '!message' => "<br />\n" . nl2br(htmlentities($email['message'])))); |
|
337 |
drupal_set_message(t('Illegal information. Data not submitted.'), 'error'); |
|
338 |
return FALSE; |
|
339 |
} |
|
314 |
// If "Use Reply-To header" is set in Webform settings and the Reply-To |
|
315 |
// header is not already set, set Reply-To to the From and set From address |
|
316 |
// to webform_default_from_name and webform_default_from_address. |
|
317 |
if (webform_variable_get('webform_email_replyto') && |
|
318 |
empty($email['headers']['Reply-To']) && |
|
319 |
($default_from_name = webform_variable_get('webform_default_from_name')) && |
|
320 |
($default_from_address = webform_variable_get('webform_default_from_address')) && |
|
321 |
$email['from'] !== $default_from_name) { |
|
322 |
$email['headers']['Reply-To'] = $email['from']; |
|
323 |
$email['from'] = $default_from_address; |
|
324 |
if (webform_variable_get('webform_email_address_format') == 'long') { |
|
325 |
$email_parts = webform_parse_email_address($email['headers']['Reply-To']); |
|
326 |
$from_name = t('!name via !site_name', |
|
327 |
array( |
|
328 |
'!name' => strlen($email_parts['name']) ? $email_parts['name'] : $email_parts['address'], |
|
329 |
'!site_name' => $default_from_name, |
|
330 |
)); |
|
331 |
$from_name = implode(' ', array_map('mime_header_encode', explode(' ', $from_name))); |
|
332 |
$email['from'] = '"' . $from_name . '" <' . $email['from'] . '>'; |
|
340 | 333 |
} |
334 |
} |
|
335 |
|
|
336 |
// Update the subject if set in the themed headers. |
|
337 |
if (isset($email['headers']['Subject'])) { |
|
338 |
$email['subject'] = $email['headers']['Subject']; |
|
339 |
unset($email['headers']['Subject']); |
|
340 |
} |
|
341 |
else { |
|
342 |
$email['subject'] = webform_format_email_subject($email['subject'], $node, $submission); |
|
343 |
} |
|
344 |
|
|
345 |
// Update the to e-mail if set in the themed headers. |
|
346 |
if (isset($email['headers']['To'])) { |
|
347 |
$email['email'] = $email['headers']['To']; |
|
348 |
unset($email['headers']['To']); |
|
349 |
$addresses = array_filter(explode(',', $email['email'])); |
|
350 |
} |
|
351 |
else { |
|
352 |
// Format the To address(es). |
|
353 |
$mapping = isset($email['extra']['email_mapping']) ? $email['extra']['email_mapping'] : NULL; |
|
354 |
$addresses = webform_format_email_address($email['email'], NULL, $node, $submission, TRUE, FALSE, NULL, $mapping); |
|
355 |
$email['email'] = implode(',', $addresses); |
|
356 |
} |
|
357 |
|
|
358 |
// Generate the list of addresses that this e-mail will be sent to. |
|
359 |
$addresses_final = array_filter($addresses, 'webform_valid_email_address'); |
|
360 |
|
|
361 |
if (!$addresses_final) { |
|
362 |
return; |
|
363 |
} |
|
341 | 364 |
|
342 |
// Consolidate addressees into one message if permitted by configuration. |
|
343 |
$send_increment = 1; |
|
344 |
if (!webform_variable_get('webform_email_address_individual')) { |
|
345 |
$send_increment = count($addresses_final); |
|
346 |
$addresses_final = array(implode(', ', $addresses_final)); |
|
365 |
// Verify that this submission is not attempting to send any spam hacks. |
|
366 |
foreach ($addresses_final as $address) { |
|
367 |
if (_webform_submission_spam_check($address, $email['subject'], $email['from'], $email['headers'])) { |
|
368 |
watchdog('webform', 'Possible spam attempt from @remote !message', |
|
369 |
array('@remote' => ip_address(), '!message' => "<br />\n" . nl2br(htmlentities($email['message'])))); |
|
370 |
drupal_set_message(t('Illegal information. Data not submitted.'), 'error'); |
|
371 |
return; |
|
347 | 372 |
} |
373 |
} |
|
348 | 374 |
|
349 |
// Mail the webform results. |
|
350 |
foreach ($addresses_final as $address) { |
|
375 |
// Consolidate addressees into one message if permitted by configuration. |
|
376 |
$send_increment = 1; |
|
377 |
if (!webform_variable_get('webform_email_address_individual')) { |
|
378 |
$send_increment = count($addresses_final); |
|
379 |
$addresses_final = array(implode(', ', $addresses_final)); |
|
380 |
} |
|
351 | 381 |
|
352 |
$language = $user->uid ? user_preferred_language($user) : language_default(); |
|
353 |
$mail_params = array( |
|
354 |
'message' => $email['message'], |
|
355 |
'subject' => $email['subject'], |
|
356 |
'headers' => $email['headers'], |
|
357 |
'node' => $node, |
|
358 |
'submission' => $submission, |
|
359 |
'email' => $email, |
|
360 |
); |
|
382 |
// Prepare the variables used by drupal_mail(). |
|
383 |
$language = $user->uid ? user_preferred_language($user) : language_default(); |
|
384 |
$mail_params = array( |
|
385 |
'message' => $email['message'], |
|
386 |
'subject' => $email['subject'], |
|
387 |
'headers' => $email['headers'], |
|
388 |
'node' => $node, |
|
389 |
'submission' => $submission, |
|
390 |
'email' => $email, |
|
391 |
); |
|
361 | 392 |
|
362 |
if (webform_variable_get('webform_email_html_capable')) { |
|
363 |
// Load attachments for the e-mail. |
|
364 |
$attachments = array(); |
|
365 |
if ($email['attachments']) { |
|
366 |
webform_component_include('file'); |
|
367 |
foreach ($node->webform['components'] as $component) { |
|
368 |
if (webform_component_feature($component['type'], 'attachment') && !empty($submission->data[$component['cid']][0])) { |
|
369 |
if (webform_component_implements($component['type'], 'attachments')) { |
|
370 |
$files = webform_component_invoke($component['type'], 'attachments', $component, $submission->data[$component['cid']]); |
|
371 |
if ($files) { |
|
372 |
$attachments = array_merge($attachments, $files); |
|
373 |
} |
|
374 |
} |
|
393 |
if (webform_variable_get('webform_email_html_capable')) { |
|
394 |
// Load attachments for the e-mail. |
|
395 |
$attachments = array(); |
|
396 |
if ($email['attachments']) { |
|
397 |
webform_component_include('file'); |
|
398 |
foreach ($node->webform['components'] as $component) { |
|
399 |
if (webform_component_feature($component['type'], 'attachment') && !empty($submission->data[$component['cid']][0])) { |
|
400 |
if (webform_component_implements($component['type'], 'attachments')) { |
|
401 |
$files = webform_component_invoke($component['type'], 'attachments', $component, $submission->data[$component['cid']]); |
|
402 |
if ($files) { |
|
403 |
$attachments = array_merge($attachments, $files); |
|
375 | 404 |
} |
376 | 405 |
} |
377 | 406 |
} |
378 |
|
|
379 |
// Add the attachments to the mail parameters. |
|
380 |
$mail_params['attachments'] = $attachments; |
|
381 |
|
|
382 |
// Set all other properties for HTML e-mail handling. |
|
383 |
$mail_params['plain'] = !$email['html']; |
|
384 |
$mail_params['plaintext'] = $email['html'] ? NULL : $email['message']; |
|
385 |
$mail_params['headers'] = $email['headers']; |
|
386 |
if ($email['html']) { |
|
387 |
// MIME Mail requires this header or it will filter all text. |
|
388 |
$mail_params['headers']['Content-Type'] = 'text/html; charset=UTF-8'; |
|
389 |
} |
|
390 | 407 |
} |
408 |
} |
|
391 | 409 |
|
392 |
// Mail the submission. |
|
393 |
$message = drupal_mail('webform', 'submission', $address, $language, $mail_params, $email['from']); |
|
394 |
if ($message['result']) { |
|
395 |
$send_count += $send_increment; |
|
396 |
} |
|
410 |
// Add the attachments to the mail parameters. |
|
411 |
$mail_params['attachments'] = $attachments; |
|
412 |
|
|
413 |
// Set all other properties for HTML e-mail handling. |
|
414 |
$mail_params['plain'] = !$email['html']; |
|
415 |
$mail_params['plaintext'] = $email['html'] ? NULL : $email['message']; |
|
416 |
$mail_params['headers'] = $email['headers']; |
|
417 |
if ($email['html']) { |
|
418 |
// MIME Mail requires this header or it will filter all text. |
|
419 |
$mail_params['headers']['Content-Type'] = 'text/html; charset=UTF-8'; |
|
397 | 420 |
} |
398 | 421 |
} |
399 | 422 |
|
400 |
return $send_count; |
|
423 |
return array( |
|
424 |
'addresses_final' => $addresses_final, |
|
425 |
'send_increment' => $send_increment, |
|
426 |
'language' => $language, |
|
427 |
'mail_params' => $mail_params, |
|
428 |
); |
|
401 | 429 |
} |
402 | 430 |
|
403 | 431 |
/** |
... | ... | |
435 | 463 |
} |
436 | 464 |
|
437 | 465 |
/** |
438 |
* |
|
466 |
* Form submit handler.
|
|
439 | 467 |
*/ |
440 | 468 |
function webform_submission_delete_form_submit($form, &$form_state) { |
441 | 469 |
$node = node_load($form_state['values']['details']['nid']); |
... | ... | |
953 | 981 |
} |
954 | 982 |
|
955 | 983 |
/** |
956 |
* |
|
984 |
* Verify that an email is not attempting to send any spam.
|
|
957 | 985 |
*/ |
958 | 986 |
function _webform_submission_spam_check($to, $subject, $from, $headers = array()) { |
959 | 987 |
$headers = implode('\n', (array) $headers); |
Also available in: Unified diff
Weekly update of contrib modules