/** * Implements Mail::send() function using the sendmail * command-line binary. * * @param mixed $recipients Either a comma-seperated list of recipients * (RFC822 compliant), or an array of recipients, * each RFC822 valid. This may contain recipients not * specified in the headers, for Bcc:, resending * messages, etc. * * @param array $headers The array of headers to send with the mail, in an * associative array, where the array key is the * header name (ie, 'Subject'), and the array value * is the header value (ie, 'test'). The header * produced from those values would be 'Subject: * test'. * * @param string $body The full text of the message body, including any * Mime parts, etc. * * @return mixed Returns true on success, or a PEAR_Error * containing a descriptive error message on * failure. * @access public */ function send($recipients, $headers, $body) { if (defined('CIVICRM_MAIL_LOG')) { require_once 'CRM/Utils/Mail.php'; CRM_Utils_Mail::logger($recipients, $headers, $body); return true; } if (!is_array($headers)) { return PEAR::raiseError('$headers must be an array'); } $result = $this->_sanitizeHeaders($headers); if (is_a($result, 'PEAR_Error')) { return $result; } $recipients = $this->parseRecipients($recipients); if (is_a($recipients, 'PEAR_Error')) { return $recipients; } $recipients = implode(' ', array_map('escapeshellarg', $recipients)); $headerElements = $this->prepareHeaders($headers); if (is_a($headerElements, 'PEAR_Error')) { return $headerElements; } list($from, $text_headers) = $headerElements; /* Since few MTAs are going to allow this header to be forged * unless it's in the MAIL FROM: exchange, we'll use * Return-Path instead of From: if it's set. */ if (!empty($headers['Return-Path'])) { $from = $headers['Return-Path']; } if (!isset($from)) { return PEAR::raiseError('No from address given.'); } elseif (strpos($from, ' ') !== false || strpos($from, ';') !== false || strpos($from, '&') !== false || strpos($from, '`') !== false) { return PEAR::raiseError('From address specified with dangerous characters.'); } $from = escapeshellarg($from); // Security bug #16200 $mail = @popen($this->sendmail_path . (!empty($this->sendmail_args) ? ' ' . $this->sendmail_args : '') . " -f{$from} -- {$recipients}", 'w'); if (!$mail) { return PEAR::raiseError('Failed to open sendmail [' . $this->sendmail_path . '] for execution.'); } // Write the headers following by two newlines: one to end the headers // section and a second to separate the headers block from the body. fputs($mail, $text_headers . $this->sep . $this->sep); fputs($mail, $body); $result = pclose($mail); if (version_compare(phpversion(), '4.2.3') == -1) { // With older php versions, we need to shift the pclose // result to get the exit code. $result = $result >> 8 & 0xff; } if ($result != 0) { return PEAR::raiseError('sendmail returned error code ' . $result, $result); } return true; }
/** * Implements Mail_mail::send() function using php's built-in mail() * command. * * @param mixed $recipients Either a comma-seperated list of recipients * (RFC822 compliant), or an array of recipients, * each RFC822 valid. This may contain recipients not * specified in the headers, for Bcc:, resending * messages, etc. * * @param array $headers The array of headers to send with the mail, in an * associative array, where the array key is the * header name (ie, 'Subject'), and the array value * is the header value (ie, 'test'). The header * produced from those values would be 'Subject: * test'. * * @param string $body The full text of the message body, including any * Mime parts, etc. * * @return mixed Returns true on success, or a PEAR_Error * containing a descriptive error message on * failure. * * @access public */ function send($recipients, $headers, $body) { if (defined('CIVICRM_MAIL_LOG')) { require_once 'CRM/Utils/Mail.php'; CRM_Utils_Mail::logger($recipients, $headers, $body); return true; } $this->_sanitizeHeaders($headers); // If we're passed an array of recipients, implode it. if (is_array($recipients)) { $recipients = implode(', ', $recipients); } // Get the Subject out of the headers array so that we can // pass it as a seperate argument to mail(). $subject = ''; if (isset($headers['Subject'])) { $subject = $headers['Subject']; unset($headers['Subject']); } /* * Also remove the To: header. The mail() function will add its own * To: header based on the contents of $recipients. */ unset($headers['To']); // Flatten the headers out. $headerElements = $this->prepareHeaders($headers); if (PEAR::isError($headerElements)) { return $headerElements; } list(, $text_headers) = $headerElements; /* * We only use mail()'s optional fifth parameter if the additional * parameters have been provided and we're not running in safe mode. */ if (empty($this->_params) || ini_get('safe_mode')) { $result = mail($recipients, $subject, $body, $text_headers); } else { $result = mail($recipients, $subject, $body, $text_headers, $this->_params); } /* * If the mail() function returned failure, we need to create a * PEAR_Error object and return it instead of the boolean result. */ if ($result === false) { $result = PEAR::raiseError('mail() returned failure'); } return $result; }
/** * Implements Mail::send() function using SMTP. * * @param mixed $recipients Either a comma-seperated list of recipients * (RFC822 compliant), or an array of recipients, * each RFC822 valid. This may contain recipients not * specified in the headers, for Bcc:, resending * messages, etc. * * @param array $headers The array of headers to send with the mail, in an * associative array, where the array key is the * header name (e.g., 'Subject'), and the array value * is the header value (e.g., 'test'). The header * produced from those values would be 'Subject: * test'. * * @param string $body The full text of the message body, including any * MIME parts, etc. * * @return mixed Returns true on success, or a PEAR_Error * containing a descriptive error message on * failure. * @access public */ function send($recipients, $headers, $body) { if (defined('CIVICRM_MAIL_LOG')) { require_once 'CRM/Utils/Mail.php'; CRM_Utils_Mail::logger($recipients, $headers, $body); return true; } /* If we don't already have an SMTP object, create one. */ $result =& $this->getSMTPObject(); if (PEAR::isError($result)) { return $result; } if (!is_array($headers)) { return PEAR::raiseError('$headers must be an array'); } $this->_sanitizeHeaders($headers); $headerElements = $this->prepareHeaders($headers); if (is_a($headerElements, 'PEAR_Error')) { $this->_smtp->rset(); return $headerElements; } list($from, $textHeaders) = $headerElements; /* Since few MTAs are going to allow this header to be forged * unless it's in the MAIL FROM: exchange, we'll use * Return-Path instead of From: if it's set. */ if (!empty($headers['Return-Path'])) { $from = $headers['Return-Path']; } if (!isset($from)) { $this->_smtp->rset(); return PEAR::raiseError('No From: address has been provided', PEAR_MAIL_SMTP_ERROR_FROM); } $params = null; if (!empty($this->_extparams)) { foreach ($this->_extparams as $key => $val) { $params .= ' ' . $key . (is_null($val) ? '' : '=' . $val); } } if (PEAR::isError($res = $this->_smtp->mailFrom($from, ltrim($params)))) { $error = $this->_error("Failed to set sender: {$from}", $res); $this->_smtp->rset(); return PEAR::raiseError($error, PEAR_MAIL_SMTP_ERROR_SENDER); } $recipients = $this->parseRecipients($recipients); if (is_a($recipients, 'PEAR_Error')) { $this->_smtp->rset(); return $recipients; } foreach ($recipients as $recipient) { $res = $this->_smtp->rcptTo($recipient); if (is_a($res, 'PEAR_Error')) { $error = $this->_error("Failed to add recipient: {$recipient}", $res); $this->_smtp->rset(); return PEAR::raiseError($error, PEAR_MAIL_SMTP_ERROR_RECIPIENT); } } /* Send the message's headers and the body as SMTP data. */ $res = $this->_smtp->data($textHeaders . "\r\n\r\n" . $body); list(, $args) = $this->_smtp->getResponse(); if (preg_match("/Ok: queued as (.*)/", $args, $queued)) { $this->queued_as = $queued[1]; } /* we need the greeting; from it we can extract the authorative name of the mail server we've really connected to. * ideal if we're connecting to a round-robin of relay servers and need to track which exact one took the email */ $this->greeting = $this->_smtp->getGreeting(); if (is_a($res, 'PEAR_Error')) { $error = $this->_error('Failed to send data', $res); $this->_smtp->rset(); return PEAR::raiseError($error, PEAR_MAIL_SMTP_ERROR_DATA); } /* If persistent connections are disabled, destroy our SMTP object. */ if ($this->persist === false) { $this->disconnect(); } return true; }
/** * Implements Mail::send() function using the sendmail * command-line binary. * * @param mixed $recipients Either a comma-seperated list of recipients * (RFC822 compliant), or an array of recipients, * each RFC822 valid. This may contain recipients not * specified in the headers, for Bcc:, resending * messages, etc. * * @param array $headers The array of headers to send with the mail, in an * associative array, where the array key is the * header name (ie, 'Subject'), and the array value * is the header value (ie, 'test'). The header * produced from those values would be 'Subject: * test'. * * @param string $body The full text of the message body, including any * Mime parts, etc. * * @return mixed Returns true on success, or a PEAR_Error * containing a descriptive error message on * failure. * @access public */ function send($recipients, $headers, $body) { if (defined('CIVICRM_MAIL_LOG')) { require_once 'CRM/Utils/Mail.php'; CRM_Utils_Mail::logger($recipients, $headers, $body); return true; } $recipients = $this->parseRecipients($recipients); if (PEAR::isError($recipients)) { return $recipients; } $recipients = escapeShellCmd(implode(' ', $recipients)); $this->_sanitizeHeaders($headers); $headerElements = $this->prepareHeaders($headers); if (PEAR::isError($headerElements)) { return $headerElements; } list($from, $text_headers) = $headerElements; // use Return-Path for SMTP envelope’s FROM address (if set), CRM-5946 if (!empty($headers['Return-Path'])) { $from = $headers['Return-Path']; } if (!isset($from)) { return PEAR::raiseError('No from address given.'); } elseif (strpos($from, ' ') !== false || strpos($from, ';') !== false || strpos($from, '&') !== false || strpos($from, '`') !== false) { return PEAR::raiseError('From address specified with dangerous characters.'); } $from = escapeShellCmd($from); $mail = @popen($this->sendmail_path . (!empty($this->sendmail_args) ? ' ' . $this->sendmail_args : '') . " -f{$from} -- {$recipients}", 'w'); if (!$mail) { return PEAR::raiseError('Failed to open sendmail [' . $this->sendmail_path . '] for execution.'); } // Write the headers following by two newlines: one to end the headers // section and a second to separate the headers block from the body. fputs($mail, $text_headers . $this->sep . $this->sep); fputs($mail, $body); $result = pclose($mail); if (version_compare(phpversion(), '4.2.3') == -1) { // With older php versions, we need to shift the pclose // result to get the exit code. $result = $result >> 8 & 0xff; } if ($result != 0) { return PEAR::raiseError('sendmail returned error code ' . $result, $result); } return true; }
/** * Implements Mail_mail::send() function using php's built-in mail() * command. * * @param mixed $recipients Either a comma-seperated list of recipients * (RFC822 compliant), or an array of recipients, * each RFC822 valid. This may contain recipients not * specified in the headers, for Bcc:, resending * messages, etc. * * @param array $headers The array of headers to send with the mail, in an * associative array, where the array key is the * header name (ie, 'Subject'), and the array value * is the header value (ie, 'test'). The header * produced from those values would be 'Subject: * test'. * * @param string $body The full text of the message body, including any * Mime parts, etc. * * @return mixed Returns true on success, or a PEAR_Error * containing a descriptive error message on * failure. * * @access public */ function send($recipients, $headers, $body) { if (defined('CIVICRM_MAIL_LOG')) { CRM_Utils_Mail::logger($recipients, $headers, $body); if (!defined('CIVICRM_MAIL_LOG_AND SEND')) { return true; } } if (!is_array($headers)) { return PEAR::raiseError('$headers must be an array'); } $result = $this->_sanitizeHeaders($headers); if (is_a($result, 'PEAR_Error')) { return $result; } // If we're passed an array of recipients, implode it. if (is_array($recipients)) { $recipients = implode(', ', $recipients); } // Get the Subject out of the headers array so that we can // pass it as a seperate argument to mail(). $subject = ''; if (isset($headers['Subject'])) { $subject = $headers['Subject']; unset($headers['Subject']); } // Also remove the To: header. The mail() function will add its own // To: header based on the contents of $recipients. unset($headers['To']); // Flatten the headers out. $headerElements = $this->prepareHeaders($headers); if (is_a($headerElements, 'PEAR_Error')) { return $headerElements; } list($from, $text_headers) = $headerElements; // use Return-Path for SMTP envelope’s FROM address (if set), CRM-5946 if (!empty($headers['Return-Path'])) { $from = $headers['Return-Path']; } $this->_params = "-f" . $from; // We only use mail()'s optional fifth parameter if the additional // parameters have been provided and we're not running in safe mode. if (empty($this->_params) || ini_get('safe_mode')) { $result = mail($recipients, $subject, $body, $text_headers); } else { $result = mail($recipients, $subject, $body, $text_headers, $this->_params); } // If the mail() function returned failure, we need to create a // PEAR_Error object and return it instead of the boolean result. if ($result === false) { $result = PEAR::raiseError('mail() returned failure'); } return $result; }
/** * Implements Mail::send() function using SMTP. * * @param mixed $recipients Either a comma-seperated list of recipients * (RFC822 compliant), or an array of recipients, * each RFC822 valid. This may contain recipients not * specified in the headers, for Bcc:, resending * messages, etc. * * @param array $headers The array of headers to send with the mail, in an * associative array, where the array key is the * header name (e.g., 'Subject'), and the array value * is the header value (e.g., 'test'). The header * produced from those values would be 'Subject: * test'. * * @param string $body The full text of the message body, including any * Mime parts, etc. * * @return mixed Returns true on success, or a PEAR_Error * containing a descriptive error message on * failure. * @access public */ function send($recipients, $headers, $body) { if (defined('CIVICRM_MAIL_LOG')) { require_once 'CRM/Utils/Mail.php'; CRM_Utils_Mail::logger($recipients, $headers, $body); return true; } include_once 'Net/SMTP.php'; /* If we don't already have an SMTP object, create one. */ if (is_object($this->_smtp) === false) { $this->_smtp =& new Net_SMTP($this->host, $this->port, $this->localhost); /* If we still don't have an SMTP object at this point, fail. */ if (is_object($this->_smtp) === false) { return PEAR::raiseError('Failed to create a Net_SMTP object', PEAR_MAIL_SMTP_ERROR_CREATE); } /* Configure the SMTP connection. */ if ($this->debug) { $this->_smtp->setDebug(true); } /* Attempt to connect to the configured SMTP server. */ if (PEAR::isError($res = $this->_smtp->connect($this->timeout))) { $error = $this->_error('Failed to connect to ' . $this->host . ':' . $this->port, $res); return PEAR::raiseError($error, PEAR_MAIL_SMTP_ERROR_CONNECT); } /* Attempt to authenticate if authentication has been enabled. */ if ($this->auth) { $method = is_string($this->auth) ? $this->auth : ''; if (PEAR::isError($res = $this->_smtp->auth($this->username, $this->password, $method))) { $error = $this->_error("{$method} authentication failure", $res); $this->_smtp->rset(); return PEAR::raiseError($error, PEAR_MAIL_SMTP_ERROR_AUTH); } } } $this->_sanitizeHeaders($headers); $headerElements = $this->prepareHeaders($headers); if (PEAR::isError($headerElements)) { $this->_smtp->rset(); return $headerElements; } list($from, $textHeaders) = $headerElements; /* Since few MTAs are going to allow this header to be forged * unless it's in the MAIL FROM: exchange, we'll use * Return-Path instead of From: if it's set. */ if (!empty($headers['Return-Path'])) { $from = $headers['Return-Path']; } if (!isset($from)) { $this->_smtp->rset(); return PEAR::raiseError('No From: address has been provided', PEAR_MAIL_SMTP_ERROR_FROM); } $args['verp'] = $this->verp; if (PEAR::isError($res = $this->_smtp->mailFrom($from, $args))) { $error = $this->_error("Failed to set sender: {$from}", $res); $this->_smtp->rset(); return PEAR::raiseError($error, PEAR_MAIL_SMTP_ERROR_SENDER); } $recipients = $this->parseRecipients($recipients); if (PEAR::isError($recipients)) { $this->_smtp->rset(); return $recipients; } foreach ($recipients as $recipient) { if (PEAR::isError($res = $this->_smtp->rcptTo($recipient))) { $error = $this->_error("Failed to add recipient: {$recipient}", $res); $this->_smtp->rset(); return PEAR::raiseError($error, PEAR_MAIL_SMTP_ERROR_RECIPIENT); } } /* Send the message's headers and the body as SMTP data. */ if (PEAR::isError($res = $this->_smtp->data($textHeaders . "\r\n\r\n" . $body))) { $error = $this->_error('Failed to send data', $res); $this->_smtp->rset(); return PEAR::raiseError($error, PEAR_MAIL_SMTP_ERROR_DATA); } /* If persistent connections are disabled, destroy our SMTP object. */ if ($this->persist === false) { $this->disconnect(); } return true; }