You are here

function webform_submission_send_mail in Webform 6.3

Same name and namespace in other branches
  1. 7.4 includes/webform.submissions.inc \webform_submission_send_mail()
  2. 7.3 includes/webform.submissions.inc \webform_submission_send_mail()

Send related e-mails related to a submission.

This function is usually invoked when a submission is completed, but may be called any time e-mails should be redelivered.

Parameters

$node: The node object containing the current webform.

$submission: The webform submission object to be used in sending e-mails.

$emails: (optional) An array of specific e-mail settings to be used. If omitted, all e-mails in $node->webform['emails'] will be sent.

2 calls to webform_submission_send_mail()
webform_client_form_submit in ./webform.module
Submit handler for saving the form values and sending e-mails.
webform_submission_resend_submit in includes/webform.submissions.inc
Submit handler for webform_submission_resend().

File

includes/webform.submissions.inc, line 170
This file is loaded when handling submissions, either submitting new, editing, or viewing. It also contains all CRUD functions for submissions.

Code

function webform_submission_send_mail($node, $submission, $emails = NULL) {
  global $user;

  // Get the list of e-mails we'll be sending.
  $emails = isset($emails) ? $emails : $node->webform['emails'];

  // Create a themed message for mailing.
  $send_count = 0;
  foreach ($emails as $eid => $email) {

    // Set the HTML property based on availablity of MIME Mail.
    $email['html'] = $email['html'] && 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',
      ), $node, $submission, $email);
    }
    else {
      $email['message'] = $email['template'];
    }

    // Replace tokens in the message.
    $email['message'] = _webform_filter_values($email['message'], $node, $submission, $email, FALSE, TRUE);

    // Build the e-mail headers.
    $email['headers'] = theme(array(
      'webform_mail_headers_' . $node->nid,
      'webform_mail_headers',
    ), $node, $submission, $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 {
      $email['from'] = webform_format_email_address($email['from_address'], $email['from_name'], $node, $submission);
    }

    // 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']);
    }

    // Generate the list of addresses that this e-mail will be sent to.
    $addresses = array_filter(explode(',', $email['email']));
    $addresses_final = array();
    foreach ($addresses as $address) {
      $address = trim($address);

      // After filtering e-mail addresses with component values, a single value
      // might contain multiple addresses (such as from checkboxes or selects).
      $address = webform_format_email_address($address, NULL, $node, $submission, TRUE, FALSE, 'short');
      if (is_array($address)) {
        foreach ($address as $new_address) {
          $new_address = trim($new_address);
          if (valid_email_address($new_address)) {
            $addresses_final[] = $new_address;
          }
        }
      }
      elseif (valid_email_address($address)) {
        $addresses_final[] = $address;
      }
    }

    // Mail the webform results.
    foreach ($addresses_final as $address) {

      // Verify that this submission is not attempting to send any spam hacks.
      if (_webform_submission_spam_check($address, $email['subject'], $email['from'], $email['headers'])) {
        watchdog('webform', 'Possible spam attempt from @remote_addr' . "<br />\n" . nl2br(htmlentities($email['message'])), array(
          '@remote_add' => ip_address(),
        ));
        drupal_set_message(t('Illegal information. Data not submitted.'), 'error');
        return FALSE;
      }
      $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_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']]['value'][0])) {
              if (webform_component_implements($component['type'], 'attachments')) {
                $files = webform_component_invoke($component['type'], 'attachments', $component, $submission->data[$component['cid']]['value']);
                if ($files) {
                  $attachments = array_merge($attachments, $files);
                }
              }
            }
          }
        }

        // Enable drupal_mail_alter() to alter attachments.
        $mail_params['attachments'] = $attachments;

        // Prepare (but don't send) the e-mail normally.
        $message = drupal_mail('webform', 'submission', $address, $language, $mail_params, $email['from'], FALSE);

        // Send the e-mail via MIME mail.
        $message = mimemail($message['from'], $message['to'], $message['subject'], $message['body'], !$email['html'], $message['headers'], $email['html'] ? NULL : $message['body'], $message['params']['attachments'], 'webform');

        // Support boolean (older) or array-based return values from MIME Mail.
        if (is_array($message) && $message['result'] || !is_array($message) && $message) {
          $send_count++;
        }
      }
      else {

        // Normal Drupal mailer.
        $message = drupal_mail('webform', 'submission', $address, $language, $mail_params, $email['from']);
        if ($message['result']) {
          $send_count++;
        }
      }
    }
  }
  return $send_count;
}