2 /************************************************************************
3 * Mailer v0.2.1-FINAL Start: 07/01/2012 *
4 * =================== Last change: 07/01/2012 *
6 * -------------------------------------------------------------------- *
7 * File : email-functions.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Email-delivery-related functions *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Mailversand bezogene Funktionen *
12 * -------------------------------------------------------------------- *
13 * $Revision:: 2773 $ *
14 * $Date:: 2012-06-26 01:02:44 +0200 (Tue, 26 Jun 2012) $ *
15 * $Tag:: 0.2.1-FINAL $ *
16 * $Author:: quix0r $ *
17 * -------------------------------------------------------------------- *
18 * Copyright (c) 2003 - 2009 by Roland Haeder *
19 * Copyright (c) 2009 - 2012 by Mailer Developer Team *
20 * For more information visit: http://mxchange.org *
22 * This program is free software; you can redistribute it and/or modify *
23 * it under the terms of the GNU General Public License as published by *
24 * the Free Software Foundation; either version 2 of the License, or *
25 * (at your option) any later version. *
27 * This program is distributed in the hope that it will be useful, *
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
30 * GNU General Public License for more details. *
32 * You should have received a copy of the GNU General Public License *
33 * along with this program; if not, write to the Free Software *
34 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
36 ************************************************************************/
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
43 // Send mail out to an email address
44 function sendEmail ($toEmail, $subject, $message, $isHtml = 'N', $mailHeader = '') {
45 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'toEmail=' . $toEmail . ',subject=' . $subject . ',isHtml=' . $isHtml);
46 // Empty parameters should be avoided, so we need to find them
49 reportBug(__FUNCTION__, __LINE__, 'isHtml is empty.');
53 if ((!isInString('@', $toEmail)) && ($toEmail > 0)) {
54 // Does the user exist?
55 if ((isExtensionActive('user')) && (fetchUserData($toEmail))) {
57 $toEmail = getUserData('email');
60 $toEmail = getWebmaster();
62 } elseif (($toEmail == '0') || (is_null($toEmail))) {
64 $toEmail = getWebmaster();
66 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'TO=' . $toEmail);
68 // Check for PHPMailer or debug-mode
69 if ((!isPhpMailerConfigured()) || (isDebugModeEnabled())) {
70 // Prefix is '' for text mails
79 // Load email header template
80 $mailHeader = loadEmailTemplate($prefix . 'header', $mailHeader);
83 // Debug mode enabled?
84 if (isDebugModeEnabled()) {
85 // In debug mode we want to display the mail instead of sending it away so we can debug this part
87 <strong>Headers</strong> : ' . htmlentities(trim($mailHeader)) . '
88 <strong>To</strong> : ' . htmlentities($toEmail) . '
89 <strong>Subject</strong> : ' . htmlentities($subject) . '
90 <strong>Message(' . strlen($message) . ')</strong> : ' . htmlentities($message) . '
93 // This is always fine
95 } elseif (!empty($toEmail)) {
97 return sendRawEmail($toEmail, $subject, $message, $mailHeader);
98 } elseif ($isHtml != 'Y') {
99 // Problem detected while sending a mail, forward it to admin
100 return sendRawEmail(getWebmaster(), '[PROBLEM:]' . $subject, $message, $mailHeader);
103 // Why did we end up here? This should not happen
104 reportBug(__FUNCTION__, __LINE__, 'Ending up: template=' . $template);
108 * Check to use whether plain mail() command or PHPMailer class
109 * @TODO Rewrite this to an extension 'smtp'
112 function isPhpMailerConfigured () {
113 return ((getConfig('SMTP_HOSTNAME') != '') && (getConfig('SMTP_USER') != ''));
116 // Send out a raw email with PHPMailer class or plain mail() command
117 function sendRawEmail ($toEmail, $subject, $message, $headers) {
118 // Just compile all to put out all configs, etc.
119 $eval = '$toEmail = decodeEntities("' . escapeQuotes(doFinalCompilation(compileRawCode($toEmail), FALSE)) . '"); ';
120 $eval .= '$subject = decodeEntities("' . escapeQuotes(doFinalCompilation(compileRawCode($subject), FALSE)) . '"); ';
121 $eval .= '$headers = decodeEntities("' . escapeQuotes(doFinalCompilation(compileRawCode($headers), FALSE)) . '"); ';
123 // Do not decode entities in the message because we also send HTML mails through this function
124 $eval .= '$message = "' . escapeQuotes(doFinalCompilation(compileRawCode($message), FALSE)) . '";';
126 // Run the final eval() command
129 // Shall we use PHPMailer class or plain mail() command?
130 if (isPhpMailerConfigured()) {
131 // Use PHPMailer class with SMTP enabled
132 loadIncludeOnce('inc/phpmailer/class.phpmailer.php');
133 loadIncludeOnce('inc/phpmailer/class.smtp.php');
136 $mail = new PHPMailer();
138 // Set charset to UTF-8
139 $mail->CharSet = 'UTF-8';
141 // Path for PHPMailer
142 $mail->PluginDir = sprintf("%sinc/phpmailer/", getPath());
145 $mail->SMTPAuth = TRUE;
146 $mail->Host = getConfig('SMTP_HOSTNAME');
148 $mail->Username = getConfig('SMTP_USER');
149 $mail->Password = getConfig('SMTP_PASSWORD');
150 if (empty($headers)) {
151 $mail->From = getWebmaster();
153 $mail->From = $headers;
155 $mail->FromName = getMainTitle();
156 $mail->Subject = $subject;
157 if ((isExtensionActive('html_mail')) && (secureString($message) != $message)) {
158 $mail->Body = $message;
159 $mail->AltBody = decodeEntities($message);
160 $mail->WordWrap = 70;
163 $mail->Body = decodeEntities(strip_tags($message));
166 $mail->AddAddress($toEmail, '');
167 $mail->AddReplyTo(getWebmaster(), getMainTitle());
168 $mail->AddCustomHeader('Errors-To: ' . getWebmaster());
169 $mail->AddCustomHeader('X-Loop: ' . getWebmaster());
170 $mail->AddCustomHeader('Bounces-To: ' . getWebmaster());
173 // Has an error occured?
174 if (!empty($mail->ErrorInfo)) {
176 logDebugMessage(__FUNCTION__, __LINE__, 'Error while sending mail: ' . $mail->ErrorInfo);
185 // Use plain mail() command
186 return mail($toEmail, $subject, decodeEntities($message), $headers);
190 // Send notification to admin
191 function sendAdminNotification ($subject, $templateName, $content = array(), $userid = NULL) {
192 if ((isExtensionInstalledAndNewer('admins', '0.4.1')) && (function_exists('sendAdminsEmails'))) {
194 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'admins=Y,subject=' . $subject . ',templateName=' . $templateName . ' - OKAY!');
195 sendAdminsEmails($subject, $templateName, $content, $userid);
197 // Send out-dated way
198 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'admins=N,subject=' . $subject . ',templateName=' . $templateName . ' - OUT-DATED!');
199 $message = loadEmailTemplate($templateName, $content, $userid);
200 sendAdminEmails($subject, $message, ($templateName == 'admin_report_bug'));
204 // ----------------------------------------------------------------------------
205 // Template helper functions
206 // ----------------------------------------------------------------------------
208 // Helper function to add extra headers to text mails
209 function doTemplateAddExtraTextMailHeaders ($templateName, $clear, $extraHeaders = '') {
210 // Run the header through the filter
211 $extraHeaders = runFilterChain('add_extra_text_mail_headers', $extraHeaders);
214 return $extraHeaders;
217 // Helper function to add extra headers to HTML mails
218 function doTemplateAddExtraHtmlMailHeaders ($templateName, $clear, $extraHeaders = '') {
219 // Run the header through the filter
220 $extraHeaders = runFilterChain('add_extra_html_mail_headers', $extraHeaders);
223 return $extraHeaders;
226 // Send mails for del/edit/lock build modes
227 // @TODO $rawUserId is currently unused
228 function sendGenericBuildMails ($mode, $tableName, $content, $id, $subjectPart = '', $userIdColumn = array('userid'), $rawUserId = array('userid')) {
229 // $tableName must be an array
230 if ((!is_array($tableName)) || (count($tableName) != 1)) {
231 // $tableName is no array
232 reportBug(__FUNCTION__, __LINE__, 'tableName[]=' . gettype($tableName) . '!=array: userIdColumn=' . $userIdColumn);
233 } elseif ((!is_array($userIdColumn)) || (count($userIdColumn) != 1)) {
234 // $tableName is no array
235 reportBug(__FUNCTION__, __LINE__, 'userIdColumn[]=' . gettype($userIdColumn) . '!=array: userIdColumn=' . $userIdColumn);
238 // Default subject is the subject part
239 $subject = $subjectPart;
241 // Is the subject part not set?
242 if (empty($subjectPart)) {
243 // Then use it from the mode
244 $subject = strtoupper($mode);
247 // Is the raw userid set?
248 if (postRequestElement($userIdColumn[0], $id) > 0) {
249 // Load email template
250 if (!empty($subjectPart)) {
251 $mail = loadEmailTemplate('member_' . $mode . '_' . strtolower($subjectPart) . '_' . $tableName[0], $content);
253 $mail = loadEmailTemplate('member_' . $mode . '_' . $tableName[0], $content);
257 sendEmail(postRequestElement($userIdColumn[0], $id), strtoupper('{--MEMBER_' . $subject . '_' . $tableName[0] . '_SUBJECT--}'), $mail);
261 $subject = strtoupper('{--ADMIN_' . $subject . '_' . $tableName[0] . '_SUBJECT--}');
263 // Send admin notification out
264 if (!empty($subjectPart)) {
265 sendAdminNotification($subject, 'admin_' . $mode . '_' . strtolower($subjectPart) . '_' . $tableName[0], $content, postRequestElement($userIdColumn[0], $id));
267 sendAdminNotification($subject, 'admin_' . $mode . '_' . $tableName[0], $content, postRequestElement($userIdColumn[0], $id));