-// Send mail out to an email address
-function sendEmail ($toEmail, $subject, $message, $isHtml = 'N', $mailHeader = '') {
- //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'toEmail=' . $toEmail . ',subject=' . $subject . ',isHtml=' . $isHtml);
- // Empty parameters should be avoided, so we need to find them
- if (empty($isHtml)) {
- // isHtml is empty
- debug_report_bug(__FUNCTION__, __LINE__, 'isHtml is empty.');
- } // END - if
-
- // Set from header
- if ((!isInStringIgnoreCase('@', $toEmail)) && ($toEmail > 0)) {
- // Does the user exist?
- if ((isExtensionActive('user')) && (fetchUserData($toEmail))) {
- // Get the email
- $toEmail = getUserData('email');
- } else {
- // Set webmaster
- $toEmail = getWebmaster();
- }
- } elseif ($toEmail == '0') {
- // Is the webmaster!
- $toEmail = getWebmaster();
- }
- //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "TO={$toEmail}<br />");
-
- // Check for PHPMailer or debug-mode
- if ((!checkPhpMailerUsage()) || (isDebugModeEnabled())) {
- // Prefix is '' for text mails
- $prefix = '';
-
- // Is HTML?
- if ($isHtml == 'Y') {
- // Set prefix
- $prefix = 'html_';
- } // END - if
-
- // Not in PHPMailer-Mode
- if (empty($mailHeader)) {
- // Load email header template
- $mailHeader = loadEmailTemplate($prefix . 'header');
- } else {
- // Append header
- $mailHeader .= loadEmailTemplate($prefix . 'header');
- }
- } // END - if
-
- // Debug mode enabled?
- if (isDebugModeEnabled()) {
- // In debug mode we want to display the mail instead of sending it away so we can debug this part
- outputHtml('<pre>
-Headers : ' . htmlentities(utf8_decode(trim($mailHeader))) . '
-To : ' . htmlentities(utf8_decode($toEmail)) . '
-Subject : ' . htmlentities(utf8_decode($subject)) . '
-Message : ' . htmlentities(utf8_decode($message)) . '
-</pre>');
-
- // This is always fine
- return true;
- } elseif (!empty($toEmail)) {
- // Send Mail away
- return sendRawEmail($toEmail, $subject, $message, $mailHeader);
- } elseif ($isHtml != 'Y') {
- // Problem detected while sending a mail, forward it to admin
- return sendRawEmail(getWebmaster(), '[PROBLEM:]' . $subject, $message, $mailHeader);
- }
-
- // Why did we end up here? This should not happen
- debug_report_bug(__FUNCTION__, __LINE__, 'Ending up: template=' . $template);
-}
-
-// Check to use wether legacy mail() command or PHPMailer class
-// @TODO Rewrite this to an extension 'smtp'
-// @private
-function checkPhpMailerUsage() {
- return ((getConfig('SMTP_HOSTNAME') != '') && (getConfig('SMTP_USER') != ''));
-}
-
-// Send out a raw email with PHPMailer class or legacy mail() command
-function sendRawEmail ($toEmail, $subject, $message, $headers) {
- // Just compile all to put out all configs, etc.
- $eval = '$toEmail = decodeEntities("' . escapeQuotes(doFinalCompilation(compileRawCode($toEmail), false)) . '"); ';
- $eval .= '$subject = decodeEntities("' . escapeQuotes(doFinalCompilation(compileRawCode($subject), false)) . '"); ';
- $eval .= '$headers = decodeEntities("' . escapeQuotes(doFinalCompilation(compileRawCode($headers), false)) . '"); ';
-
- // Do not decode entities in the message because we also send HTML mails through this function
- $eval .= '$message = "' . escapeQuotes(doFinalCompilation(compileRawCode($message), false)) . '";';
-
- // Run the final eval() command
- eval($eval);
-
- // Shall we use PHPMailer class or legacy mode?
- if (checkPhpMailerUsage()) {
- // Use PHPMailer class with SMTP enabled
- loadIncludeOnce('inc/phpmailer/class.phpmailer.php');
- loadIncludeOnce('inc/phpmailer/class.smtp.php');
-
- // get new instance
- $mail = new PHPMailer();
-
- // Set charset to UTF-8
- $mail->CharSet = 'UTF-8';
-
- // Path for PHPMailer
- $mail->PluginDir = sprintf("%sinc/phpmailer/", getPath());
-
- $mail->IsSMTP();
- $mail->SMTPAuth = true;
- $mail->Host = getConfig('SMTP_HOSTNAME');
- $mail->Port = 25;
- $mail->Username = getConfig('SMTP_USER');
- $mail->Password = getConfig('SMTP_PASSWORD');
- if (empty($headers)) {
- $mail->From = getWebmaster();
- } else {
- $mail->From = $headers;
- }
- $mail->FromName = getMainTitle();
- $mail->Subject = $subject;
- if ((isExtensionActive('html_mail')) && (secureString($message) != $message)) {
- $mail->Body = $message;
- $mail->AltBody = 'Your mail program required HTML support to read this mail!';
- $mail->WordWrap = 70;
- $mail->IsHTML(true);
- } else {
- $mail->Body = decodeEntities($message);
- }
-
- $mail->AddAddress($toEmail, '');
- $mail->AddReplyTo(getWebmaster(), getMainTitle());
- $mail->AddCustomHeader('Errors-To:' . getWebmaster());
- $mail->AddCustomHeader('X-Loop:' . getWebmaster());
- $mail->AddCustomHeader('Bounces-To:' . getWebmaster());
- $mail->Send();
-
- // Has an error occured?
- if (!empty($mail->ErrorInfo)) {
- // Log message
- logDebugMessage(__FUNCTION__, __LINE__, 'Error while sending mail: ' . $mail->ErrorInfo);
-
- // Raise an error
- return false;
- } else {
- // All fine!
- return true;
- }
- } else {
- // Use legacy mail() command
- return mail($toEmail, $subject, decodeEntities($message), $headers);
- }
-}
-