You are here

function messaging_phpmailer_drupal_mail in Messaging 6.2

Same name and namespace in other branches
  1. 5 messaging_phpmailer/messaging_phpmailer.module \messaging_phpmailer_drupal_mail()
  2. 6.4 messaging_phpmailer/messaging_phpmailer.module \messaging_phpmailer_drupal_mail()
  3. 6 messaging_phpmailer/messaging_phpmailer.module \messaging_phpmailer_drupal_mail()
  4. 6.3 messaging_phpmailer/messaging_phpmailer.module \messaging_phpmailer_drupal_mail()

Send a message via PHPMailer. This function mimics drupal_mail. We do not use drupal_mail instead because we want to be able to send mail with both PHPMailer and MIMEMail.

1 call to messaging_phpmailer_drupal_mail()
messaging_phpmailer_send_msg in messaging_phpmailer/messaging_phpmailer.module
Send mail message to user account. Supports bulk sending

File

messaging_phpmailer/messaging_phpmailer.module, line 168
HTML Mail using PHPMailer. Messaging method plug-in.

Code

function messaging_phpmailer_drupal_mail($message) {
  if (!messaging_phpmailer_load_library()) {
    watchdog('messaging', 'Could not locate PHPMailer library.', array(), WATCHDOG_ERROR);
    return FALSE;
  }
  $mail = new PHPMailer();
  $mail
    ->IsSMTP();

  // telling the class to use SMTP
  $mail->CharSet = 'utf-8';
  $mail->ContentType = 'text/html';
  if (variable_get('messaging_phpmailer_smtp_secure', '') != '') {
    $mail->SMTPSecure = variable_get('messaging_phpmailer_smtp_secure', '');
  }
  $mail->Port = variable_get('messaging_phpmailer_smtp_port', 25);

  // Set the authentication settings.
  $username = variable_get('messaging_phpmailer_smtp_username', '');
  $password = variable_get('messaging_phpmailer_smtp_password', '');

  // If username and password are given, use SMTP authentication.
  if ($username && $password) {
    $mail->SMTPAuth = TRUE;
    $mail->Username = $username;
    $mail->Password = $password;
  }
  $host = variable_get('messaging_phpmailer_smtp_server', ini_get('SMTP'));
  if ($host) {
    $mail->Host = $host;

    // SMTP server
  }
  else {
    watchdog('messaging', 'SMTP server cannot be reached.', array(), WATCHDOG_ERROR);
    return FALSE;
  }

  // Theme the mail message
  list($subject, $body) = theme('messaging_phpmailer', $message);

  // Add some headers. Look for rfc2822 "From" formatting (ex: "name" <foo@bar.com>)
  $matches = array();
  preg_match('/["\']?(.*)["\']? <(.*)>/', $message['headers']['From'], $matches);
  if (!empty($matches[1])) {
    $from['name'] = $matches[1];
  }
  if (!empty($matches[2])) {
    $from['email'] = $matches[2];
  }
  $matches = array();
  preg_match('/["\']?(.*)["\']? <(.*)>/', $message['headers']['Reply-To'], $matches);
  if (!empty($matches[1])) {
    $replyto['name'] = $matches[1];
  }
  if (!empty($matches[2])) {
    $replyto['email'] = $matches[2];
  }
  $mail->From = !empty($from['email']) ? $from['email'] : $message['headers']['From'];
  $mail->FromName = !empty($from['name']) ? $from['name'] : '';
  if (variable_get('messaging_phpmailer_bcc', 0)) {
    $mail
      ->AddBCC($message['to']);
  }
  else {
    $mail
      ->AddAddress($message['to']);
  }
  if (!empty($message['headers']['Sender'])) {
    $mail->Sender = $message['headers']['Sender'];
  }
  if (!empty($message['headers']['Errors-To'])) {
    $mail
      ->AddCustomHeader('Errors-To:' . $message['headers']['Errors-To']);
  }
  if (!empty($message['headers']['Message-ID'])) {
    $mail->MessageID = $message['headers']['Message-ID'];
  }
  if (!empty($replyto['name']) && !empty($replyto['email'])) {
    $mail
      ->AddReplyTo($replyto['email'], $replyto['name']);
  }

  // Strip HTML out of $body for plaintext equivalent of HTML email.
  $mail->AltBody = drupal_html_to_text($body);

  // The subject has been already filtered by messaging module
  $mail->Subject = $subject;
  $mail->Body = str_replace("\r", '', $body);
  if (!empty($message['attachments']) && variable_get('messaging_phpmailer_attach', 0)) {
    foreach ($message['attachments'] as $attachment) {
      $mail
        ->AddAttachment($attachment->filepath, $attachment->filename, 'base64', $attachment->filemime);
    }
  }

  // If enabled debug option, log everything...
  if (variable_get('messaging_phpmailer_debug', 0)) {
    $mail->SMTPDebug = TRUE;
    watchdog('messaging', 'PHPMailer debug message: ' . $mail
      ->Send() . ' - ' . $mail->ErrorInfo);
  }
  else {
    $result = $mail
      ->Send();
    if (!$result) {
      watchdog('messaging', 'PHPMailer exception: !details', array(
        '!details' => $mail->ErrorInfo,
      ), WATCHDOG_ERROR);
    }
    return $result;
  }
}