You are here

function mimemail_mailengine in Mime Mail 6

Same name and namespace in other branches
  1. 5 mimemail.module \mimemail_mailengine()
  2. 7 mimemail.module \mimemail_mailengine()

The default mailengine.

Parameters

$op: The operation to perform on the message.

$message: The message to be sent.

Return value

Returns TRUE if the operation was successful or FALSE if it was not.

File

./mimemail.module, line 338
Component module for sending MIME-encoded e-mails.

Code

function mimemail_mailengine($op, $message = array()) {
  module_load_include('inc', 'mimemail');

  // Default values.
  $message = array_merge(array(
    'address' => '',
    'subject' => '',
    'body' => '',
    'sender' => '',
    'headers' => '',
  ), $message);
  switch ($op) {
    case 'name':
      return t('Mime Mail');
    case 'description':
      return t("Default mailing engine using drupal_mail().");

    // Not implemented.
    case 'settings':
      return FALSE;
    case 'multiple':
    case 'single':
    case 'send':
      if (!is_array($message['address'])) {
        $message['address'] = array(
          $message['address'],
        );
      }

      // If 'Return-Path' isn't already set in php.ini, we pass it separately
      // as an additional parameter instead of in the header.
      // However, if PHP's 'safe_mode' is on, this is not allowed.
      if (isset($message['headers']['Return-Path']) && !ini_get('safe_mode')) {
        $return_path_set = strpos(ini_get('sendmail_path'), ' -f');
        if (!$return_path_set) {
          $return_path = trim($message['headers']['Return-Path'], '<,>');
          unset($message['headers']['Return-Path']);
        }
      }
      $status = TRUE;
      foreach ($message['address'] as $to) {
        $subject = $message['subject'];
        $body = $message['body'];
        $headers = mimemail_rfc_headers($message['headers']);

        // We validate the return path, unless it is equal to the site mail, which
        // we assume to be safe.
        if (isset($return_path) && !empty($return_path) && (variable_get('site_mail', ini_get('sendmail_from')) === $return_path || mimemail_isshellsafe($return_path))) {
          if (isset($_SERVER['WINDIR']) || strpos($_SERVER['SERVER_SOFTWARE'], 'Win32') !== FALSE) {

            // On Windows, PHP will use the value of sendmail_from for the
            // Return-Path header.
            $old_from = ini_get('sendmail_from');
            ini_set('sendmail_from', $return_path);
            $status = @mail($to, $subject, $body, $headers) && $status;
            ini_set('sendmail_from', $old_from);
          }
          else {

            // On most non-Windows systems, the "-f" option to the sendmail command
            // is used to set the Return-Path.
            $status = @mail($to, $subject, $body, $headers, '-f' . $return_path) && $status;
          }
        }
        else {

          // The optional $additional_parameters argument to mail() is not allowed
          // if safe_mode is enabled. Passing any value throws a PHP warning and
          // makes mail() return FALSE.
          $status = @mail($to, $subject, $body, $headers) && $status;
        }
      }
      return $status;
  }
  return FALSE;
}