]> git.p6c8.net - form-email.git/blob - form-email/mailer.php
Zwei Tippfehler korrigiert
[form-email.git] / form-email / mailer.php
1 <?php
2
3 #
4 # Form E-Mail 3.5
5 #
6 # Versenden einer E-Mail, die ueber ein Kontaktformular
7 # geschrieben wurde.
8 #
9 # Autor: Patrick Canterino <patrick@patshaping.de>
10 # Letzte Aenderung: 03.03.2012
11 #
12 # Copyright (C) 2002-2012 Patrick Canterino
13 #
14 # Diese Datei kann unter den Bedingungen der "Artistic License 2.0"
15 # weitergegeben und / oder veraendert werden.
16 # Siehe:
17 # http://www.opensource.org/licenses/artistic-license-2.0
18 #
19
20 # ===========
21 # Hauptteil
22 # ===========
23
24 $VERSION = '3.5';
25
26 require('config.php');
27 require('functions.php');
28 require('class.Template.php');
29
30 if($_SERVER['REQUEST_METHOD'] != 'POST') show_fatal($err_only_post);
31
32 # Wenn Captchas aktiviert sind, Session starten
33
34 if($captcha_enable) {
35 session_start();
36
37 # Pruefen, ob die zulaessige Zahl der Falscheingaben bei Captchas ueberschritten wurde
38
39 if($captcha_max && isset($_SESSION['captcha_failed']) && $_SESSION['captcha_failed'] >= $captcha_max) {
40 show_fatal($err_captcha_max);
41 }
42 }
43
44 # Bestimmte Werte in Integers umwandeln
45
46 $name_min = intval($name_min);
47 $subject_min = intval($subject_min);
48 $text_min = intval($text_min);
49 $name_max = intval($name_max);
50 $subject_max = intval($subject_max);
51 $text_max = intval($text_max);
52 $line_break = intval($line_break);
53
54 # Formulardaten auslesen
55
56 $email = formdata('email');
57 $name = formdata('name');
58 $recipient = formdata('recipient');
59 $subject = formdata('subject');
60 $text = formdata('text');
61
62 # Pruefen, ob ueberhaupt was ausgefuellt wurde
63
64 if(!$name && !$email && !$text) show_user_error($err_nothing);
65
66 # Pruefen, ob alle wichtigen Formular-Felder ausgefuellt wurden
67
68 if(!$name) show_user_error($err_no_name);
69 if(!$email) show_user_error($err_no_email);
70 if(!$text) show_user_error($err_no_text);
71
72 if(!$subject) $subject = $default_subject; # Standard-Betreff
73
74 # Pruefen, ob Name, Betreff und Text lang genug sind
75
76 if($name_min && strlen($name) < $name_min) show_user_error($err_name_short);
77 if($subject_min && strlen($subject) < $subject_min) show_user_error($err_subject_short);
78 if($text_min && strlen($text) < $text_min) show_user_error($err_text_short);
79
80 # Pruefen, ob Name, Betreff und Text nicht zu lang sind
81
82 if($name_max && strlen($name) > $name_max) show_user_error($err_name_long);
83 if($subject_max && strlen($subject) > $subject_max) show_user_error($err_subject_long);
84 if($text_max && strlen($text) > $text_max) show_user_error($err_text_long);
85
86 # Pruefen, ob eingegebene E-Mail-Adresse gueltig ist
87
88 if(!filter_var($email,FILTER_VALIDATE_EMAIL)) show_user_error($err_invalid_email);
89
90 # Individuelle Felder einlesen
91
92 $prepared_user_fields = array();
93
94 if(isset($user_fields)) {
95 while(list($user_field,$user_field_data) = each($user_fields)) {
96 $user_field_content = formdata($user_field);
97
98 # Pruefung, ob es ein Pflichtfeld ist
99
100 if(!$user_field_content) {
101 if(isset($user_field_data['required']) && $user_field_data['required']) {
102 if(isset($user_field_data['errors']['not_set'])) {
103 show_user_error($user_field_data['errors']['not_set']);
104 }
105 else {
106 show_fatal($err_indiv_errmsg_miss,array('ERRMSG' => 'not_set', 'INDIVIDUAL' => $user_field));
107 }
108 }
109 else {
110 # Wenn das Feld optional ist und nicht gesetzt wurde, brechen wir hier ab
111 # und machen mit dem naechsten Feld weiter
112
113 $prepared_user_fields[$user_field_data['tpl_var']] = '';
114 continue;
115 }
116 }
117
118 # Laengen-Pruefung
119
120 if(isset($user_field_data['min']) && $user_field_data['min']) {
121 if(strlen($user_field_content) < $user_field_data['min']) {
122 if(isset($user_field_data['errors']['too_short'])) {
123 show_user_error($user_field_data['errors']['too_short']);
124 }
125 else {
126 show_fatal($err_indiv_errmsg_miss,array('ERRMSG' => 'too_short', 'INDIVIDUAL' => $user_field));
127 }
128 }
129 }
130
131 if(isset($user_field_data['min']) && $user_field_data['max']) {
132 if(strlen($user_field_content) > $user_field_data['max']) {
133 if(isset($user_field_data['errors']['too_long'])) {
134 show_user_error($user_field_data['errors']['too_long']);
135 }
136 else {
137 show_fatal($err_indiv_errmsg_miss,array('ERRMSG' => 'too_long', 'INDIVIDUAL' => $user_field));
138 }
139 }
140 }
141
142 # Pruefung gegen frei definierbare Pruef-Funktion
143
144 if(isset($user_field_data['check']) && $user_field_data['check'] != '') {
145 if(function_exists($user_field_data['check']) && call_user_func($user_field_data['check'],$user_field_content) === false) {
146 if(isset($user_field_data['errors']['check_fail'])) {
147 show_user_error($user_field_data['errors']['check_fail']);
148 }
149 else {
150 show_fatal($err_indiv_errmsg_miss,array('ERRMSG' => 'check_fail', 'INDIVIDUAL' => $user_field));
151 }
152 }
153 }
154
155 # Pruefung gegen frei definierbaren regulaeren Ausdruck
156
157 if(isset($user_field_data['regex']) && $user_field_data['regex'] != '') {
158 if(!preg_match($user_field_data['regex'],$user_field_content)) {
159 if(isset($user_field_data['errors']['match_fail'])) {
160 show_user_error($user_field_data['errors']['match_fail']);
161 }
162 else {
163 show_fatal($err_indiv_errmsg_miss,array('ERRMSG' => 'match_fail', 'INDIVIDUAL' => $user_field));
164 }
165 }
166 }
167
168 # Formularfeld durch frei definierbare Funktion bearbeiten
169
170 if(isset($user_field_data['prepare']) && $user_field_data['prepare'] != '') {
171 if(function_exists($user_field_data['check'])) {
172 $user_field_content = call_user_func($user_field_data['prepare'],$user_field_content);
173 }
174 }
175
176 # Name der Template-Variable ermitteln
177
178 if(isset($user_field_data['tpl_var']) && $user_field_data['tpl_var'] != '') {
179 $tpl_var = $user_field_data['tpl_var'];
180 }
181 else {
182 $tpl_var = 'USER_'.$user_field;
183 }
184
185 # Fertige Formulardaten wegkopieren und fuer spaeter zur Verfuegung stellen
186
187 $prepared_user_fields[$tpl_var] = $user_field_content;
188 }
189 }
190
191 # Captcha ueberpruefen
192
193 if($captcha_enable) {
194 $captcha_input = formdata('captcha');
195 $captcha_incorrect = false;
196
197 if(isset($_SESSION) && isset($_SESSION['captcha'])) {
198 if($captcha_case) {
199 if(!$captcha_input || empty($captcha_input) || $captcha_input != $_SESSION['captcha']) $captcha_incorrect = true;
200 }
201 else {
202 if(!$captcha_input || empty($captcha_input) || strtolower($captcha_input) != strtolower($_SESSION['captcha'])) $captcha_incorrect = true;
203 }
204
205 if($captcha_incorrect) {
206 if($captcha_max) {
207 if(isset($_SESSION['captcha_failed']) && $_SESSION['captcha_failed']) {
208 $_SESSION['captcha_failed']++;
209 }
210 else {
211 $_SESSION['captcha_failed'] = 1;
212 }
213 }
214
215 if($captcha_max && isset($_SESSION['captcha_failed']) && $_SESSION['captcha_failed'] >= $captcha_max) {
216 show_fatal($err_captcha_max);
217 }
218 else {
219 show_user_error($err_captcha_incorrect);
220 }
221 }
222 }
223 else {
224 show_fatal($err_captcha_session_failure);
225 }
226 }
227
228 # Referer ueberpruefen
229
230 if($check_referer) {
231 $referer_ok = semicolon_split($referer_ok);
232
233 $referer_parts = parse_url($_SERVER['HTTP_REFERER']);
234 $referer_host = $referer_parts['host'];
235
236 if(in_array($referer_host,$referer_ok) === false) show_fatal($err_referer_not_ok);
237 }
238
239 # Zeilenumbrueche in Namen und Betreff durch Leerzeichen ersetzen
240
241 $name = preg_replace("/\015\012|\012|\015/",' ',$name);
242 $subject = preg_replace("/\015\012|\012|\015/",' ',$subject);
243
244 # Uhrzeit formatieren
245
246 $timestamp = time();
247
248 date_default_timezone_set($timezone);
249 $time = strftime($timeformat,$timestamp);
250
251 # Header der Mail
252
253 $header = '';
254
255 if($friendly_sender) {
256 if($friendly_sender == 2) {
257 if(preg_match("/[\200-\377]/",$name)) {
258 # Absendernamen mit Base64 codieren
259
260 $encoded_name = base64_encode($name);
261 $encoded_name = wordwrap($encoded_name,56,"\015\012",1);
262 $encoded_name = "=?$charset?B?$encoded_name?=";
263 $encoded_name = str_replace("\015\012","?=\015\012 =?$charset?B?",$encoded_name);
264 }
265 else {
266 # Bestimmte Zeichen im Absendernamen codieren
267
268 $encoded_name = str_replace('"','\"',$name);
269 $encoded_name = str_replace('(','\\(',$encoded_name);
270 $encoded_name = str_replace(')','\\)',$encoded_name);
271 $encoded_name = '"'.$encoded_name.'"';
272 }
273
274 $header = "From: $encoded_name <$email>\n";
275 }
276 else $header = "From: $email\n";
277 }
278
279 if($add_header) {
280 if(!$friendly_sender) $header .= "Reply-To: $email\n";
281 $header .= 'Content-Type: text/plain; charset="'.$charset.'"'."\n";
282 $header .= 'Content-Transfer-Encoding: 8bit'."\n";
283 $header .= 'MIME-Version: 1.0'."\n";
284 $header .= 'X-Mailer: Form E-Mail '.$VERSION."\n";
285 $header .= 'X-Sender-IP: ['.$_SERVER['REMOTE_ADDR'].']';
286 }
287 else $header .= "Reply-To: $email";
288
289 $header = preg_replace("/\015\012|\012|\015/","\n",$header);
290
291 # Betreff mit Base64 codieren, wenn er Zeichen enthaelt,
292 # die nicht aus dem ASCII-Zeichensatz stammen
293
294 if(preg_match("/[\200-\377]/",$subject)) {
295 $encoded_subject = base64_encode($subject);
296 $encoded_subject = wordwrap($encoded_subject,56,"\015\012",1);
297 $encoded_subject = "=?$charset?B?$encoded_subject?=";
298 $encoded_subject = str_replace("\015\012","?=\015\012 =?$charset?B?",$encoded_subject);
299 }
300 else $encoded_subject = $subject;
301
302 # Mailtext formatieren...
303
304 $mtpl = new Template;
305 $mtpl->read_file($tpl_mail);
306
307 $mtpl->set_var('EMAIL', $email);
308 $mtpl->set_var('NAME', $name);
309 $mtpl->set_var('SUBJECT',$subject);
310 $mtpl->set_var('TIME', $time);
311
312 # ... Umgebungsvariablen einfuegen...
313
314 if(isset($allowed_envs) && is_array($allowed_envs)) {
315 foreach($allowed_envs as $allowed_env) {
316 if(isset($_SERVER[$allowed_env])) {
317 $mtpl->set_var('ENV_'.$allowed_env,$_SERVER[$allowed_env]);
318 }
319 else {
320 $mtpl->set_var('ENV_'.$allowed_env,'');
321 }
322 }
323 }
324
325 # ... eigene Felder einfuegen
326
327 while(list($tpl_var,$user_field_content) = each($prepared_user_fields)) {
328 $mtpl->set_var($tpl_var,$user_field_content);
329 }
330
331 $mtpl->set_var('TEXT',$text);
332
333 $mtpl->parse();
334
335 $mailtext = $mtpl->get_template();
336
337 if($line_break) $mailtext = wordwrap($mailtext,$line_break);
338
339 $mailtext = preg_replace("/\015\012|\012|\015/","\n",$mailtext);
340
341 # ... und die Mail abschicken (bei einem Fehler erscheint eine Meldung)
342
343 if($recipient && is_array($aliases) && $aliases[$recipient]) {
344 # Empfaenger mit Alias-Namen
345
346 $mailto = $aliases[$recipient];
347 }
348
349 $mailto = semicolon_split($mailto);
350
351 for($x=0;$x<count($mailto);$x++) {
352 mail($mailto[$x],$encoded_subject,$mailtext,$header) or show_fatal($err_send_error);
353 }
354
355 # Mails auf Festplatte speichern
356
357 if(isset($store_mails) && $store_mails) {
358 # Standard-Werte fuer einige Variablen
359
360 if(!isset($store_mails_dir) || $store_mails_dir == '') $store_mails_dir = 'stored_mails';
361 if(!isset($store_mails_prefix) || $store_mails_prefix == '') $store_mails_prefix = 'feml_';
362
363 # Verzeichnis ggf. anlegen
364
365 if(!is_dir($store_mails_dir)) mkdir($store_mails_dir);
366
367 # Betreff saeubern
368
369 $clean_subject = str_replace(' ','_',$subject);
370 $clean_subject = preg_replace('/[^a-z0-9\-_]/i','',$clean_subject);
371
372 # Eindeutigen Dateinamen der Mail zusammensetzen
373
374 $file_name = $store_mails_dir.'/'.uniqid($store_mails_prefix.$email.'_'.$clean_subject.'_'.strftime('%d%m%Y-%H%M%S',$timestamp));
375
376 # Daten in Datei schreiben
377
378 file_put_contents($file_name,$mailtext) or show_fatal($err_store_error);
379
380 # Alte Dateien loeschen
381
382 if(isset($store_mails_max) && $store_mails_max > 0) {
383 $dp = opendir($store_mails_dir);
384
385 while($stored_mail_file = readdir($dp)) {
386 if($stored_mail_file != '.' && $stored_mail_file != '..' && substr($stored_mail_file,0,strlen($store_mails_prefix)) == $store_mails_prefix) {
387 $stored_mail_files[$store_mails_dir.'/'.$stored_mail_file] = filemtime($store_mails_dir.'/'.$stored_mail_file);
388 }
389 }
390
391 closedir($dp);
392
393 arsort($stored_mail_files);
394
395 $x = 0;
396
397 foreach(array_keys($stored_mail_files) as $stored_mail_filename) {
398 $x++;
399
400 if($x > $store_mails_max) {
401 unlink($stored_mail_filename);
402 }
403 }
404 }
405 }
406
407 if($captcha_enable) session_destroy();
408
409 # Mail wurde erfolgreich versendet, also wird die Dankesseite angezeigt
410
411 if($return_url) {
412 # Per Redirect auf Seite umleiten
413
414 if(strpos($return_url,'://') === false) {
415 # URL vervollstaendigen
416
417 $http_host = ($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
418 $protocol = '';
419 $port = '';
420 $path = '';
421
422 if(isset($_SERVER['HTTPS'])) {
423 # SSL-verschluesseltes HTTP
424
425 $protocol = 'https://';
426 if($_SERVER['SERVER_PORT'] != 443) $port = ':'.$_SERVER['SERVER_PORT'];
427 }
428 else {
429 # Normales HTTP
430
431 $protocol = 'http://';
432 if($_SERVER['SERVER_PORT'] != 80) $port = ':'.$_SERVER['SERVER_PORT'];
433 }
434
435 if(substr($return_url,0,1) == '/') {
436 $return_url = $protocol.$http_host.$port.$return_url;
437 }
438 else {
439 if(substr($_SERVER['SCRIPT_NAME'],-1,1) == '/') {
440 $path = substr($_SERVER['SCRIPT_NAME'],0,-1);
441 }
442 else {
443 $path = $_SERVER['SCRIPT_NAME'];
444 }
445
446 $path = substr($path,0,strrpos($path,'/')+1);
447 if(substr($path,0,1) != '/') $path = '/'.$path;
448
449 $return_url = $protocol.$http_host.$port.$path.$return_url;
450 }
451 }
452
453 header('Status: 303 See Other');
454 header('Location: '.$return_url);
455 }
456 else {
457 # Mit Template formatierte Seite anzeigen
458
459 $stpl = new Template;
460 $stpl->read_file($tpl_sent);
461
462 $stpl->set_var('EMAIL', plain($email));
463 $stpl->set_var('MAIL', plain($mailtext));
464 $stpl->set_var('NAME', plain($name));
465 $stpl->set_var('SUBJECT',plain($subject));
466 $stpl->set_var('TEXT', plain($text));
467 $stpl->set_var('TIME', plain($time));
468
469 if(isset($user_fields)) {
470 reset($user_fields);
471
472 while(list($user_field,$user_field_data) = each($user_fields)) {
473 if(isset($user_field_data['tpl_var']) && $user_field_data['tpl_var'] != '') {
474 $tpl_var = $user_field_data['tpl_var'];
475 }
476 else {
477 $tpl_var = 'USER_'.$user_field;
478 }
479
480 $stpl->set_var($tpl_var,htmlspecialchars(formdata($user_field)));
481 }
482 }
483
484 $stpl->parse();
485
486 print $stpl->get_template();
487 }
488
489 #
490 ### Ende ###
491
492 ?>

patrick-canterino.de