You are here

function mimemail_phpmailer_send in PHPMailer 5

Same name and namespace in other branches
  1. 8.3 includes/phpmailer.mimemail.inc \mimemail_phpmailer_send()
  2. 5.2 includes/phpmailer.mimemail.inc \mimemail_phpmailer_send()
  3. 6.3 includes/phpmailer.mimemail.inc \mimemail_phpmailer_send()
  4. 6 includes/mimemail.inc \mimemail_phpmailer_send()
  5. 6.2 includes/phpmailer.mimemail.inc \mimemail_phpmailer_send()
  6. 7.4 includes/phpmailer.mimemail.inc \mimemail_phpmailer_send()
  7. 7.3 includes/phpmailer.mimemail.inc \mimemail_phpmailer_send()

Sends out the email.

Parameters

$addresses: Array of recipient addresses.

$subject: Message subject.

$body: Message body.

$from: Sender e-mail address.

$header: Message header.

1 call to mimemail_phpmailer_send()
phpmailer_mailengine in ./phpmailer.module
Implementation of hook_mailengine().

File

includes/mimemail.inc, line 81

Code

function mimemail_phpmailer_send($recipients, $subject, $body, $from, $header) {
  $mail = new MimeMailPHPMailer();
  settype($header, 'array');

  // Select which From address to use:
  // $from parameter takes precedence over $header['From'].
  if (isset($header['From'])) {

    // Check whether a full e-mail address has been specified
    // ("some name" <user@example.com>).
    if ($parsed = phpmailer_parse_address($header['From'])) {
      $mail->FromName = $parsed['name'];
      $from_address = $parsed['address'];
    }
    else {
      $from_address = $header['From'];
    }
    unset($header['From']);
  }
  if (empty($from)) {
    if (!empty($from_address)) {
      $from = $from_address;
    }
    else {
      $from = variable_get('site_mail', ini_get('sendmail_from'));
    }
  }
  else {
    if ($parsed = phpmailer_parse_address($from)) {
      $from = $parsed['address'];
    }
  }
  $mail->From = $from;

  // Set additional properties.
  $properties = array(
    'X-Priority' => 'Priority',
  );
  foreach ($properties as $source => $property) {
    if (isset($header[$source])) {
      $mail->{$property} = $header[$source];
      unset($header[$source]);
    }
  }

  // Always set by PHPMailer.
  unset($header['MIME-Version']);

  // Add remaining header lines.
  // Note: Any header lines MUST be checked by the caller for unwanted
  // newline characters to avoid header injection. Use PHPMailer::SecureHeader()
  // for that purpose.
  foreach ($header as $key => $value) {
    $mail
      ->AddCustomHeader("{$key}:{$value}");
  }

  // Check if debugging is turned on and replace recipient.
  $debug_email = variable_get('phpmailer_debug_email', '');
  if ($debug_email) {
    $recipients = array(
      $debug_email,
    );
  }

  // Add recipients.
  foreach ($recipients as $recipient) {
    $mail
      ->AddAddress($recipient);
  }
  $mail->Subject = $subject;

  // Create SMTP data header.
  $header = $mail
    ->Createheader();
  if (!($result = $mail
    ->SmtpSend($header, $body))) {
    watchdog('smtp', t('Error sending email: From: @from To: @to Error: @error', array(
      '@from' => '<' . $from . '>',
      '@to' => '<' . implode('>, <', $recipients) . '>',
      '@error' => $mail->ErrorInfo,
    )), WATCHDOG_ERROR);
  }

  // Commented out to benefit from keep-alive feature.

  //$mail->SmtpClose();
  return $result;
}