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 * Copyright (c) 2003 - 2009 by Roland Haeder *
14 * Copyright (c) 2009 - 2013 by Mailer Developer Team *
15 * For more information visit: http://mxchange.org *
17 * This program is free software; you can redistribute it and/or modify *
18 * it under the terms of the GNU General Public License as published by *
19 * the Free Software Foundation; either version 2 of the License, or *
20 * (at your option) any later version. *
22 * This program is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
25 * GNU General Public License for more details. *
27 * You should have received a copy of the GNU General Public License *
28 * along with this program; if not, write to the Free Software *
29 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
31 ************************************************************************/
33 // Some security stuff...
34 if (!defined('__SECURITY')) {
38 // Send mail out to an email address
39 function sendEmail ($toEmail, $subject, $message, $isHtml = 'N', $mailHeader = '') {
40 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'toEmail=' . $toEmail . ',subject=' . $subject . ',isHtml=' . $isHtml);
41 // Empty parameters should be avoided, so we need to find them
44 reportBug(__FUNCTION__, __LINE__, 'isHtml is empty.');
48 if ((!isInString('@', $toEmail)) && (isValidId($toEmail))) {
49 // Does the user exist?
50 if ((isExtensionActive('user')) && (fetchUserData($toEmail))) {
52 $toEmail = getUserData('email');
55 $toEmail = getWebmaster();
57 } elseif (($toEmail == '0') || (is_null($toEmail))) {
59 $toEmail = getWebmaster();
61 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'TO=' . $toEmail);
63 // Check for PHPMailer or debug-mode
64 if ((!isPhpMailerConfigured()) || (isDebugModeEnabled())) {
65 // Prefix is '' for text mails
74 // Load email header template
75 $mailHeader = loadEmailTemplate($prefix . 'header', $mailHeader);
78 // Debug mode enabled?
79 if ((isDebugModeEnabled()) && (!isAjaxOutputMode())) {
82 'headers' => htmlentities(trim($mailHeader)),
83 'to' => htmlentities($toEmail),
84 'subject' => htmlentities($subject),
85 'message' => htmlentities($message),
86 'length' => strlen($message)
89 // In debug mode display the mail instead of sending it away so it can be debugged
90 loadTemplate('display_email', FALSE, $content);
92 // This is always fine
94 } elseif (!empty($toEmail)) {
96 return sendRawEmail($toEmail, $subject, $message, $mailHeader);
97 } elseif ($isHtml != 'Y') {
98 // Problem detected while sending a mail, forward it to admin
99 return sendRawEmail(getWebmaster(), '[PROBLEM:]' . $subject, $message, $mailHeader);
102 // Why did we end up here? This should not happen
103 reportBug(__FUNCTION__, __LINE__, 'Ending up: template=' . $template);
107 * Check to use whether plain mail() command or PHPMailer class
108 * @TODO Rewrite this to an extension 'smtp'
111 function isPhpMailerConfigured () {
112 return ((getConfig('SMTP_HOSTNAME') != '') && (getConfig('SMTP_USER') != ''));
115 // Send out a raw email with PHPMailer class or plain mail() command
116 function sendRawEmail ($toEmail, $subject, $message, $headers) {
117 // Just compile all to put out all configs, etc.
118 $eval = '$toEmail = decodeEntities("' . escapeQuotes(doFinalCompilation(compileRawCode($toEmail), FALSE)) . '"); ';
119 $eval .= '$subject = decodeEntities("' . escapeQuotes(doFinalCompilation(compileRawCode($subject), FALSE)) . '"); ';
120 $eval .= '$headers = decodeEntities("' . escapeQuotes(doFinalCompilation(compileRawCode($headers), FALSE)) . '"); ';
122 // Do not decode entities in the message because we also send HTML mails through this function
123 $eval .= '$message = "' . escapeQuotes(doFinalCompilation(compileRawCode($message), FALSE)) . '";';
125 // Run the final eval() command
128 // Shall we use PHPMailer class or plain mail() command?
129 if (isPhpMailerConfigured()) {
130 // Use PHPMailer class with SMTP enabled
131 loadIncludeOnce('inc/phpmailer/class.phpmailer.php');
132 loadIncludeOnce('inc/phpmailer/class.smtp.php');
135 $mail = new PHPMailer();
137 // Set charset to UTF-8
138 $mail->CharSet = 'UTF-8';
140 // Path for PHPMailer
141 $mail->PluginDir = sprintf('%sinc/phpmailer/', getPath());
144 $mail->SMTPAuth = TRUE;
145 $mail->Host = getConfig('SMTP_HOSTNAME');
147 $mail->Username = getConfig('SMTP_USER');
148 $mail->Password = getConfig('SMTP_PASSWORD');
149 if (empty($headers)) {
150 $mail->From = getWebmaster();
152 $mail->From = $headers;
154 $mail->FromName = getMainTitle();
155 $mail->Subject = $subject;
156 if ((isExtensionActive('html_mail')) && (secureString($message) != $message)) {
157 $mail->Body = $message;
158 $mail->AltBody = decodeEntities($message);
159 $mail->WordWrap = 70;
162 $mail->Body = decodeEntities(strip_tags($message));
165 $mail->AddAddress($toEmail, '');
166 $mail->AddReplyTo(getWebmaster(), getMainTitle());
167 $mail->AddCustomHeader('Errors-To: ' . getWebmaster());
168 $mail->AddCustomHeader('X-Loop: ' . getWebmaster());
169 $mail->AddCustomHeader('Bounces-To: ' . getWebmaster());
172 // Has an error occured?
173 if (!empty($mail->ErrorInfo)) {
175 logDebugMessage(__FUNCTION__, __LINE__, 'Error while sending mail: ' . $mail->ErrorInfo);
184 // Use plain mail() command
185 return mail($toEmail, $subject, decodeEntities($message), $headers);
189 // Send notification to admin
190 function sendAdminNotification ($subject, $templateName, $content = array(), $userid = NULL) {
191 if ((isExtensionInstalledAndNewer('admins', '0.4.1')) && (function_exists('sendAdminsEmails'))) {
193 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'admins=Y,subject=' . $subject . ',templateName=' . $templateName . ' - OKAY!');
194 sendAdminsEmails($subject, $templateName, $content, $userid);
196 // Send out-dated way
197 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'admins=N,subject=' . $subject . ',templateName=' . $templateName . ' - OUT-DATED!');
198 $message = loadEmailTemplate($templateName, $content, $userid);
199 sendAdminEmails($subject, $message, ($templateName == 'admin_report_bug'));
203 // Send mails for del/edit/lock build modes
204 // @TODO $rawUserId is currently unused
205 function sendGenericBuildMails ($mode, $tableName, $content, $id, $subjectPart = '', $userIdColumn = array('userid'), $rawUserId = array('userid')) {
206 // $tableName must be an array
207 if ((!is_array($tableName)) || (count($tableName) != 1)) {
208 // $tableName is no array
209 reportBug(__FUNCTION__, __LINE__, 'tableName[]=' . gettype($tableName) . '!=array: userIdColumn=' . $userIdColumn);
210 } elseif ((!is_array($userIdColumn)) || (count($userIdColumn) != 1)) {
211 // $tableName is no array
212 reportBug(__FUNCTION__, __LINE__, 'userIdColumn[]=' . gettype($userIdColumn) . '!=array: userIdColumn=' . $userIdColumn);
215 // Default subject is the subject part
216 $subject = $subjectPart;
218 // Is the subject part not set?
219 if (empty($subjectPart)) {
220 // Then use it from the mode
221 $subject = strtoupper($mode);
224 // Is the raw userid set?
225 if (isValidId(postRequestElement($userIdColumn[0], $id))) {
227 $content[$userIdColumn[0]] = bigintval(postRequestElement($userIdColumn[0], $id));
229 // Load email template
230 if (!empty($subjectPart)) {
231 $mail = loadEmailTemplate('member_' . $mode . '_' . strtolower($subjectPart) . '_' . $tableName[0], $content);
233 $mail = loadEmailTemplate('member_' . $mode . '_' . $tableName[0], $content);
237 sendEmail(postRequestElement($userIdColumn[0], $id), strtoupper('{--MEMBER_' . $subject . '_' . $tableName[0] . '_SUBJECT--}'), $mail);
241 $subject = strtoupper('{--ADMIN_' . $subject . '_' . $tableName[0] . '_SUBJECT--}');
243 // Send admin notification out
244 if (!empty($subjectPart)) {
245 sendAdminNotification($subject, 'admin_' . $mode . '_' . strtolower($subjectPart) . '_' . $tableName[0], $content, postRequestElement($userIdColumn[0], $id));
247 sendAdminNotification($subject, 'admin_' . $mode . '_' . $tableName[0], $content, postRequestElement($userIdColumn[0], $id));
251 // ----------------------------------------------------------------------------
252 // Template helper functions
253 // ----------------------------------------------------------------------------
255 // Helper function to add extra headers to text mails
256 function doTemplateAddExtraTextMailHeaders ($templateName, $clear, $extraHeaders = '') {
257 // Run the header through the filter
258 $extraHeaders = runFilterChain('add_extra_text_mail_headers', $extraHeaders);
261 return $extraHeaders;
264 // Helper function to add extra headers to HTML mails
265 function doTemplateAddExtraHtmlMailHeaders ($templateName, $clear, $extraHeaders = '') {
266 // Run the header through the filter
267 $extraHeaders = runFilterChain('add_extra_html_mail_headers', $extraHeaders);
270 return $extraHeaders;