]> git.mxchange.org Git - mailer.git/blob - inc/email-functions.php
Continued a bit:
[mailer.git] / inc / email-functions.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 07/01/2012 *
4  * ===================                          Last change: 07/01/2012 *
5  *                                                                      *
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 - 2016 by Mailer Developer Team                   *
15  * For more information visit: http://mxchange.org                      *
16  *                                                                      *
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.                                  *
21  *                                                                      *
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.                         *
26  *                                                                      *
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,               *
30  * MA  02110-1301  USA                                                  *
31  ************************************************************************/
32
33 // Some security stuff...
34 if (!defined('__SECURITY')) {
35         die();
36 } // END - if
37
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
42         if (empty($isHtml)) {
43                 // isHtml is empty
44                 reportBug(__FUNCTION__, __LINE__, 'isHtml is empty.');
45         } // END - if
46
47         // Set from header
48         if ((!isInString('@', $toEmail)) && (isValidId($toEmail))) {
49                 // Does the user exist?
50                 if ((isExtensionActive('user')) && (fetchUserData($toEmail))) {
51                         // Get the email
52                         $toEmail = getUserData('email');
53                 } else {
54                         // Set webmaster
55                         $toEmail = getWebmaster();
56                 }
57         } elseif (($toEmail == '0') || (is_null($toEmail))) {
58                 // Is the webmaster!
59                 $toEmail = getWebmaster();
60         }
61         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'TO=' . $toEmail);
62
63         // Check for PHPMailer or debug-mode
64         if ((!isPhpMailerConfigured()) || (isDebugModeEnabled())) {
65                 // Prefix is '' for text mails
66                 $prefix = '';
67
68                 // Is HTML?
69                 if ($isHtml == 'Y') {
70                         // Set prefix
71                         $prefix = 'html_';
72                 } // END - if
73
74                 // Load email header template
75                 $mailHeader = loadEmailTemplate($prefix . 'header', $mailHeader);
76         } // END - if
77
78         // Debug mode enabled?
79         if ((isDebugModeEnabled()) && (!isAjaxOutputMode())) {
80                 // Init content array
81                 $content = array(
82                         'headers' => htmlentities(trim($mailHeader)),
83                         'to'      => htmlentities($toEmail),
84                         'subject' => htmlentities($subject),
85                         'message' => htmlentities($message),
86                         'length'  => strlen($message)
87                 );
88
89                 // In debug mode display the mail instead of sending it away so it can be debugged
90                 loadTemplate('display_email', FALSE, $content);
91
92                 // This is always fine
93                 return TRUE;
94         } elseif (!empty($toEmail)) {
95                 // Send Mail away
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);
100         }
101
102         // Why did we end up here? This should not happen
103         reportBug(__FUNCTION__, __LINE__, 'Ending up: template=' . $template);
104 }
105
106 /**
107  * Check to use whether plain mail() command or PHPMailer class
108  * @TODO Rewrite this to an extension 'smtp'
109  * @private
110  */
111 function isPhpMailerConfigured () {
112         return ((getConfig('SMTP_HOSTNAME') != '') && (getConfig('SMTP_USER') != ''));
113 }
114
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)) . '"); ';
121
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)) . '";';
124
125         // Run the final eval() command
126         eval($eval);
127
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');
133
134                 // get new instance
135                 $mail = new PHPMailer();
136
137                 // Set charset to UTF-8
138                 $mail->CharSet = 'UTF-8';
139
140                 // Path for PHPMailer
141                 $mail->PluginDir  = sprintf('%sinc/phpmailer/', getPath());
142
143                 $mail->IsSMTP();
144                 $mail->SMTPAuth   = TRUE;
145                 $mail->Host       = getConfig('SMTP_HOSTNAME');
146                 $mail->Port       = 25;
147                 $mail->Username   = getConfig('SMTP_USER');
148                 $mail->Password   = getConfig('SMTP_PASSWORD');
149                 if (empty($headers)) {
150                         $mail->From = getWebmaster();
151                 } else {
152                         $mail->From = $headers;
153                 }
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;
160                         $mail->IsHTML(TRUE);
161                 } else {
162                         $mail->Body       = decodeEntities(strip_tags($message));
163                 }
164
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());
170                 $mail->Send();
171
172                 // Has an error occured?
173                 if (!empty($mail->ErrorInfo)) {
174                         // Log message
175                         logDebugMessage(__FUNCTION__, __LINE__, 'Error while sending mail: ' . $mail->ErrorInfo);
176
177                         // Raise an error
178                         return FALSE;
179                 } else {
180                         // All fine!
181                         return TRUE;
182                 }
183         } else {
184                 // Use plain mail() command
185                 return mail($toEmail, $subject, decodeEntities($message), $headers);
186         }
187 }
188
189 // Send notification to admin
190 function sendAdminNotification ($subject, $templateName, $content = array(), $userid = NULL) {
191         if ((isExtensionInstalledAndNewer('admins', '0.4.1')) && (function_exists('sendAdminsEmails'))) {
192                 // Send new way
193                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'admins=Y,subject=' . $subject . ',templateName=' . $templateName . ' - OKAY!');
194                 sendAdminsEmails($subject, $templateName, $content, $userid);
195         } else {
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'));
200         }
201 }
202
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);
213         } // END - if
214
215         // Default subject is the subject part
216         $subject = $subjectPart;
217
218         // Is the subject part not set?
219         if (empty($subjectPart)) {
220                 // Then use it from the mode
221                 $subject = strtoupper($mode);
222         } // END - if
223
224         // Is the raw userid set?
225         if (isValidId(postRequestElement($userIdColumn[0], $id))) {
226                 // Set it in content
227                 $content[$userIdColumn[0]] = bigintval(postRequestElement($userIdColumn[0], $id));
228
229                 // Load email template
230                 if (!empty($subjectPart)) {
231                         $mail = loadEmailTemplate('member_' . $mode . '_' . strtolower($subjectPart) . '_' . $tableName[0], $content);
232                 } else {
233                         $mail = loadEmailTemplate('member_' . $mode . '_' . $tableName[0], $content);
234                 }
235
236                 // Send email out
237                 sendEmail(postRequestElement($userIdColumn[0], $id), strtoupper('{--MEMBER_' . $subject . '_' . $tableName[0] . '_SUBJECT--}'), $mail);
238         } // END - if
239
240         // Generate subject
241         $subject = strtoupper('{--ADMIN_' . $subject . '_' . $tableName[0] . '_SUBJECT--}');
242
243         // Send admin notification out
244         if (!empty($subjectPart)) {
245                 sendAdminNotification($subject, 'admin_' . $mode . '_' . strtolower($subjectPart) . '_' . $tableName[0], $content, postRequestElement($userIdColumn[0], $id));
246         } else {
247                 sendAdminNotification($subject, 'admin_' . $mode . '_' . $tableName[0], $content, postRequestElement($userIdColumn[0], $id));
248         }
249 }
250
251 // ----------------------------------------------------------------------------
252 //                           Template helper functions
253 // ----------------------------------------------------------------------------
254
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);
259
260         // And return it
261         return $extraHeaders;
262 }
263
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);
268
269         // And return it
270         return $extraHeaders;
271 }
272
273 // [EOF]
274 ?>