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 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
17 * -------------------------------------------------------------------- *
18 * Copyright (c) 2003 - 2009 by Roland Haeder *
19 * Copyright (c) 2009 - 2013 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()) && (!isAjaxOutputMode())) {
87 'headers' => htmlentities(trim($mailHeader)),
88 'to' => htmlentities($toEmail),
89 'subject' => htmlentities($subject),
90 'message' => htmlentities($message),
91 'length' => strlen($message)
94 // In debug mode display the mail instead of sending it away so it can be debugged
95 loadTemplate('display_email', FALSE, $content);
97 // This is always fine
99 } elseif (!empty($toEmail)) {
101 return sendRawEmail($toEmail, $subject, $message, $mailHeader);
102 } elseif ($isHtml != 'Y') {
103 // Problem detected while sending a mail, forward it to admin
104 return sendRawEmail(getWebmaster(), '[PROBLEM:]' . $subject, $message, $mailHeader);
107 // Why did we end up here? This should not happen
108 reportBug(__FUNCTION__, __LINE__, 'Ending up: template=' . $template);
112 * Check to use whether plain mail() command or PHPMailer class
113 * @TODO Rewrite this to an extension 'smtp'
116 function isPhpMailerConfigured () {
117 return ((getConfig('SMTP_HOSTNAME') != '') && (getConfig('SMTP_USER') != ''));
120 // Send out a raw email with PHPMailer class or plain mail() command
121 function sendRawEmail ($toEmail, $subject, $message, $headers) {
122 // Just compile all to put out all configs, etc.
123 $eval = '$toEmail = decodeEntities("' . escapeQuotes(doFinalCompilation(compileRawCode($toEmail), FALSE)) . '"); ';
124 $eval .= '$subject = decodeEntities("' . escapeQuotes(doFinalCompilation(compileRawCode($subject), FALSE)) . '"); ';
125 $eval .= '$headers = decodeEntities("' . escapeQuotes(doFinalCompilation(compileRawCode($headers), FALSE)) . '"); ';
127 // Do not decode entities in the message because we also send HTML mails through this function
128 $eval .= '$message = "' . escapeQuotes(doFinalCompilation(compileRawCode($message), FALSE)) . '";';
130 // Run the final eval() command
133 // Shall we use PHPMailer class or plain mail() command?
134 if (isPhpMailerConfigured()) {
135 // Use PHPMailer class with SMTP enabled
136 loadIncludeOnce('inc/phpmailer/class.phpmailer.php');
137 loadIncludeOnce('inc/phpmailer/class.smtp.php');
140 $mail = new PHPMailer();
142 // Set charset to UTF-8
143 $mail->CharSet = 'UTF-8';
145 // Path for PHPMailer
146 $mail->PluginDir = sprintf("%sinc/phpmailer/", getPath());
149 $mail->SMTPAuth = TRUE;
150 $mail->Host = getConfig('SMTP_HOSTNAME');
152 $mail->Username = getConfig('SMTP_USER');
153 $mail->Password = getConfig('SMTP_PASSWORD');
154 if (empty($headers)) {
155 $mail->From = getWebmaster();
157 $mail->From = $headers;
159 $mail->FromName = getMainTitle();
160 $mail->Subject = $subject;
161 if ((isExtensionActive('html_mail')) && (secureString($message) != $message)) {
162 $mail->Body = $message;
163 $mail->AltBody = decodeEntities($message);
164 $mail->WordWrap = 70;
167 $mail->Body = decodeEntities(strip_tags($message));
170 $mail->AddAddress($toEmail, '');
171 $mail->AddReplyTo(getWebmaster(), getMainTitle());
172 $mail->AddCustomHeader('Errors-To: ' . getWebmaster());
173 $mail->AddCustomHeader('X-Loop: ' . getWebmaster());
174 $mail->AddCustomHeader('Bounces-To: ' . getWebmaster());
177 // Has an error occured?
178 if (!empty($mail->ErrorInfo)) {
180 logDebugMessage(__FUNCTION__, __LINE__, 'Error while sending mail: ' . $mail->ErrorInfo);
189 // Use plain mail() command
190 return mail($toEmail, $subject, decodeEntities($message), $headers);
194 // Send notification to admin
195 function sendAdminNotification ($subject, $templateName, $content = array(), $userid = NULL) {
196 if ((isExtensionInstalledAndNewer('admins', '0.4.1')) && (function_exists('sendAdminsEmails'))) {
198 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'admins=Y,subject=' . $subject . ',templateName=' . $templateName . ' - OKAY!');
199 sendAdminsEmails($subject, $templateName, $content, $userid);
201 // Send out-dated way
202 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'admins=N,subject=' . $subject . ',templateName=' . $templateName . ' - OUT-DATED!');
203 $message = loadEmailTemplate($templateName, $content, $userid);
204 sendAdminEmails($subject, $message, ($templateName == 'admin_report_bug'));
208 // Send mails for del/edit/lock build modes
209 // @TODO $rawUserId is currently unused
210 function sendGenericBuildMails ($mode, $tableName, $content, $id, $subjectPart = '', $userIdColumn = array('userid'), $rawUserId = array('userid')) {
211 // $tableName must be an array
212 if ((!is_array($tableName)) || (count($tableName) != 1)) {
213 // $tableName is no array
214 reportBug(__FUNCTION__, __LINE__, 'tableName[]=' . gettype($tableName) . '!=array: userIdColumn=' . $userIdColumn);
215 } elseif ((!is_array($userIdColumn)) || (count($userIdColumn) != 1)) {
216 // $tableName is no array
217 reportBug(__FUNCTION__, __LINE__, 'userIdColumn[]=' . gettype($userIdColumn) . '!=array: userIdColumn=' . $userIdColumn);
220 // Default subject is the subject part
221 $subject = $subjectPart;
223 // Is the subject part not set?
224 if (empty($subjectPart)) {
225 // Then use it from the mode
226 $subject = strtoupper($mode);
229 // Is the raw userid set?
230 if (isValidId(postRequestElement($userIdColumn[0], $id))) {
232 $content[$userIdColumn[0]] = bigintval(postRequestElement($userIdColumn[0], $id));
234 // Load email template
235 if (!empty($subjectPart)) {
236 $mail = loadEmailTemplate('member_' . $mode . '_' . strtolower($subjectPart) . '_' . $tableName[0], $content);
238 $mail = loadEmailTemplate('member_' . $mode . '_' . $tableName[0], $content);
242 sendEmail(postRequestElement($userIdColumn[0], $id), strtoupper('{--MEMBER_' . $subject . '_' . $tableName[0] . '_SUBJECT--}'), $mail);
246 $subject = strtoupper('{--ADMIN_' . $subject . '_' . $tableName[0] . '_SUBJECT--}');
248 // Send admin notification out
249 if (!empty($subjectPart)) {
250 sendAdminNotification($subject, 'admin_' . $mode . '_' . strtolower($subjectPart) . '_' . $tableName[0], $content, postRequestElement($userIdColumn[0], $id));
252 sendAdminNotification($subject, 'admin_' . $mode . '_' . $tableName[0], $content, postRequestElement($userIdColumn[0], $id));
256 // ----------------------------------------------------------------------------
257 // Template helper functions
258 // ----------------------------------------------------------------------------
260 // Helper function to add extra headers to text mails
261 function doTemplateAddExtraTextMailHeaders ($templateName, $clear, $extraHeaders = '') {
262 // Run the header through the filter
263 $extraHeaders = runFilterChain('add_extra_text_mail_headers', $extraHeaders);
266 return $extraHeaders;
269 // Helper function to add extra headers to HTML mails
270 function doTemplateAddExtraHtmlMailHeaders ($templateName, $clear, $extraHeaders = '') {
271 // Run the header through the filter
272 $extraHeaders = runFilterChain('add_extra_html_mail_headers', $extraHeaders);
275 return $extraHeaders;