+<?php
+/************************************************************************
+ * Mailer v0.2.1-FINAL Start: 07/01/2012 *
+ * =================== Last change: 07/01/2012 *
+ * *
+ * -------------------------------------------------------------------- *
+ * File : email-functions.php *
+ * -------------------------------------------------------------------- *
+ * Short description : Email-delivery-related functions *
+ * -------------------------------------------------------------------- *
+ * Kurzbeschreibung : Mailversand bezogene Funktionen *
+ * -------------------------------------------------------------------- *
+ * $Revision:: 2773 $ *
+ * $Date:: 2012-06-26 01:02:44 +0200 (Tue, 26 Jun 2012) $ *
+ * $Tag:: 0.2.1-FINAL $ *
+ * $Author:: quix0r $ *
+ * -------------------------------------------------------------------- *
+ * Copyright (c) 2003 - 2009 by Roland Haeder *
+ * Copyright (c) 2009 - 2012 by Mailer Developer Team *
+ * For more information visit: http://mxchange.org *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; if not, write to the Free Software *
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
+ * MA 02110-1301 USA *
+ ************************************************************************/
+
+// Some security stuff...
+if (!defined('__SECURITY')) {
+ die();
+} // END - if
+
+// 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
+ reportBug(__FUNCTION__, __LINE__, 'isHtml is empty.');
+ } // END - if
+
+ // Set from header
+ if ((!isInString('@', $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);
+
+ // Check for PHPMailer or debug-mode
+ if ((!isPhpMailerConfigured()) || (isDebugModeEnabled())) {
+ // Prefix is '' for text mails
+ $prefix = '';
+
+ // Is HTML?
+ if ($isHtml == 'Y') {
+ // Set prefix
+ $prefix = 'html_';
+ } // END - if
+
+ // Load email header template
+ $mailHeader = loadEmailTemplate($prefix . 'header', $mailHeader);
+ } // 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>
+<strong>Headers</strong> : ' . htmlentities(trim($mailHeader)) . '
+<strong>To</strong> : ' . htmlentities($toEmail) . '
+<strong>Subject</strong> : ' . htmlentities($subject) . '
+<strong>Message(' . strlen($message) . ')</strong> : ' . htmlentities($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
+ reportBug(__FUNCTION__, __LINE__, 'Ending up: template=' . $template);
+}
+
+/**
+ * Check to use whether plain mail() command or PHPMailer class
+ * @TODO Rewrite this to an extension 'smtp'
+ * @private
+ */
+function isPhpMailerConfigured () {
+ return ((getConfig('SMTP_HOSTNAME') != '') && (getConfig('SMTP_USER') != ''));
+}
+
+// Send out a raw email with PHPMailer class or plain 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 plain mail() command?
+ if (isPhpMailerConfigured()) {
+ // 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 = decodeEntities($message);
+ $mail->WordWrap = 70;
+ $mail->IsHTML(true);
+ } else {
+ $mail->Body = decodeEntities(strip_tags($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 plain mail() command
+ return mail($toEmail, $subject, decodeEntities($message), $headers);
+ }
+}
+
+// Send notification to admin
+function sendAdminNotification ($subject, $templateName, $content = array(), $userid = NULL) {
+ if ((isExtensionInstalledAndNewer('admins', '0.4.1')) && (function_exists('sendAdminsEmails'))) {
+ // Send new way
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'admins=Y,subject=' . $subject . ',templateName=' . $templateName . ' - OKAY!');
+ sendAdminsEmails($subject, $templateName, $content, $userid);
+ } else {
+ // Send out-dated way
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'admins=N,subject=' . $subject . ',templateName=' . $templateName . ' - OUT-DATED!');
+ $message = loadEmailTemplate($templateName, $content, $userid);
+ sendAdminEmails($subject, $message, ($templateName == 'admin_report_bug'));
+ }
+}
+
+// ----------------------------------------------------------------------------
+// Template helper functions
+// ----------------------------------------------------------------------------
+
+// Helper function to add extra headers to text mails
+function doTemplateAddExtraTextMailHeaders ($templateName, $clear, $extraHeaders = '') {
+ // Run the header through the filter
+ $extraHeaders = runFilterChain('add_extra_text_mail_headers', $extraHeaders);
+
+ // And return it
+ return $extraHeaders;
+}
+
+// Helper function to add extra headers to HTML mails
+function doTemplateAddExtraHtmlMailHeaders ($templateName, $clear, $extraHeaders = '') {
+ // Run the header through the filter
+ $extraHeaders = runFilterChain('add_extra_html_mail_headers', $extraHeaders);
+
+ // And return it
+ return $extraHeaders;
+}
+
+// [EOF]
+?>