]>
git.p6c8.net - form-email.git/blob - form-email/mailer.php
6 # Versenden einer E-Mail, die ueber ein Kontaktformular
9 # Autor: Patrick Canterino <patrick@patshaping.de>
10 # Letzte Aenderung: 4.12.2011
12 # Copyright (C) 2002-2011 Patrick Canterino
14 # Diese Datei kann unter den Bedingungen der "Artistic License 2.0"
15 # weitergegeben und / oder veraendert werden.
17 # http://www.opensource.org/licenses/artistic-license-2.0
26 require('config.php');
27 require('functions.php');
28 require('class.Template.php');
30 if($_SERVER['REQUEST_METHOD'] != 'POST') show_fatal($err_only_post);
32 # Wenn Captchas aktiviert sind, Session starten
37 # Pruefen, ob die zulaessige Zahl der Falscheingaben bei Captchas ueberschritten wurde
39 if($captcha_max && isset($_SESSION['captcha_failed']) && $_SESSION['captcha_failed'] >= $captcha_max) {
40 show_fatal($err_captcha_max);
44 # Bestimmte Werte in Integers umwandeln
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);
54 # Formulardaten auslesen
56 $email = formdata('email');
57 $name = formdata('name');
58 $recipient = formdata('recipient');
59 $subject = formdata('subject');
60 $text = formdata('text');
62 # Pruefen, ob ueberhaupt was ausgefuellt wurde
64 if(!$name && !$email && !$text) show_user_error($err_nothing);
66 # Pruefen, ob alle wichtigen Formular-Felder ausgefuellt wurden
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);
72 if(!$subject) $subject = $default_subject; # Standard-Betreff
74 # Pruefen, ob Name, Betreff und Text lang genug sind
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);
80 # Pruefen, ob Name, Betreff und Text nicht zu lang sind
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);
86 # Pruefen, ob eingegebene E-Mail-Adresse gueltig ist
87 # Danke an "ich" aus dem SELFHTML Forum fuer die Muehe
89 $mail_regexp = ($no_tld)
90 ?
'/^([a-z0-9]+([\.]?|[-]?|[_]?)?)*([a-z0-9]+([\.]?[_]?)?)@([a-z0-9]+([\.]?|[-]?)?)*[a-z0-9]$/i'
91 : '/^([a-z0-9]+([\.]?|[-]?|[_]?)?)*([a-z0-9]+([\.]?[_]?)?)@([a-z0-9]+([\.]?|[-]?)?)*[a-z0-9]+\.{1}[a-z]{2,6}$/i';
93 if(!preg_match($mail_regexp,$email)) show_user_error($err_invalid_email);
95 # Individuelle Felder einlesen
97 $prepared_user_fields = array();
99 while(list($user_field,$user_field_data) = each($user_fields)) {
100 $user_field_content = formdata($user_field);
102 # Pruefung, ob das Feld gesetzt wurde und ob es optional ist
104 if((!$user_field_data['optional']) && (!$user_field_content)) {
105 show_user_error($user_field_data['errors']['not_set']);
107 elseif($user_field_data['optional'] && (!$user_field_content)) {
108 # Wenn das Feld optional ist und nicht gesetzt wurde, brechen wir hier ab
109 # und machen mit dem naechsten Feld weiter
111 $prepared_user_fields[$user_field_data['tpl_var']] = '';
117 if(isset($user_field_data['min']) && $user_field_data['min']) {
118 if(strlen($user_field_content) < $user_field_data['min']) {
119 show_user_error($user_field_data['errors']['too_short']);
123 if(isset($user_field_data['min']) && $user_field_data['max']) {
124 if(strlen($user_field_content) > $user_field_data['max']) {
125 show_user_error($user_field_data['errors']['too_long']);
129 # Pruefung gegen frei definierbare Pruef-Funktion
131 if(isset($user_field_data['check']) && $user_field_data['check'] != '') {
132 if(function_exists($user_field_data['check']) && call_user_func($user_field_data['check'],$user_field_content) === false) {
133 show_user_error($user_field_data['errors']['check_fail']);
137 # Pruefung gegen frei definierbaren regulaeren Ausdruck
139 if(isset($user_field_data['regex']) && $user_field_data['regex'] != '') {
140 if(!preg_match($user_field_data['regex'],$user_field_content)) {
141 show_user_error($user_field_data['errors']['match_fail']);
145 # Formularfeld durch frei definierbare Funktion bearbeiten
147 if(isset($user_field_data['prepare']) && $user_field_data['prepare'] != '') {
148 if(function_exists($user_field_data['check'])) {
149 $user_field_content = call_user_func($user_field_data['prepare'],$user_field_content);
153 # Fertige Formulardaten wegkopieren und fuer spaeter zur Verfuegung stellen
155 $prepared_user_fields[$user_field_data['tpl_var']] = $user_field_content;
158 # Captcha ueberpruefen
160 if($captcha_enable) {
161 $captcha_input = formdata('captcha');
162 $captcha_incorrect = false;
164 if(isset($_SESSION) && isset($_SESSION['captcha'])) {
166 if(!$captcha_input ||
empty($captcha_input) ||
$captcha_input != $_SESSION['captcha']) $captcha_incorrect = true;
169 if(!$captcha_input ||
empty($captcha_input) ||
strtolower($captcha_input) != strtolower($_SESSION['captcha'])) $captcha_incorrect = true;
172 if($captcha_incorrect) {
174 if(isset($_SESSION['captcha_failed']) && $_SESSION['captcha_failed']) {
175 $_SESSION['captcha_failed']++
;
178 $_SESSION['captcha_failed'] = 1;
182 if($captcha_max && isset($_SESSION['captcha_failed']) && $_SESSION['captcha_failed'] >= $captcha_max) {
183 show_fatal($err_captcha_max);
186 show_user_error($err_captcha_incorrect);
191 show_fatal($err_captcha_session_failure);
195 # Referer ueberpruefen
198 $referer_ok = semicolon_split($referer_ok);
200 $referer_parts = parse_url($_SERVER['HTTP_REFERER']);
201 $referer_host = $referer_parts['host'];
203 if(in_array($referer_host,$referer_ok) === false) show_fatal($err_referer_not_ok);
206 # Zeilenumbrueche in Namen und Betreff durch Leerzeichen ersetzen
208 $name = preg_replace("/\015\012|\012|\015/",' ',$name);
209 $subject = preg_replace("/\015\012|\012|\015/",' ',$subject);
211 # Uhrzeit formatieren
213 date_default_timezone_set($timezone);
214 $time = strftime($timeformat);
220 if($friendly_sender) {
221 if($friendly_sender == 2) {
222 if(preg_match("/[\200-\377]/",$name)) {
223 # Absendernamen mit Base64 codieren
225 $encoded_name = base64_encode($name);
226 $encoded_name = wordwrap($encoded_name,56,"\015\012",1);
227 $encoded_name = "=?$charset?B?$encoded_name?=";
228 $encoded_name = str_replace("\015\012","?=\015\012 =?$charset?B?",$encoded_name);
231 # Bestimmte Zeichen im Absendernamen codieren
233 $encoded_name = str_replace('"','\"',$name);
234 $encoded_name = str_replace('(','\\(',$encoded_name);
235 $encoded_name = str_replace(')','\\)',$encoded_name);
236 $encoded_name = '"'.$encoded_name.'"';
239 $header = "From: $encoded_name <$email>\n";
241 else $header = "From: $email\n";
245 if(!$friendly_sender) $header .= "Reply-To: $email\n";
246 $header .= 'Content-Type: text/plain; charset="'.$charset.'"'."\n";
247 $header .= 'Content-Transfer-Encoding: 8bit'."\n";
248 $header .= 'MIME-Version: 1.0'."\n";
249 $header .= 'X-Mailer: Form E-Mail '.$VERSION."\n";
250 $header .= 'X-Sender-IP: ['.$_SERVER['REMOTE_ADDR'].']';
252 else $header .= "Reply-To: $email";
254 $header = preg_replace("/\015\012|\012|\015/","\n",$header);
256 # Betreff mit Base64 codieren, wenn er Zeichen enthaelt,
257 # die nicht aus dem ASCII-Zeichensatz stammen
259 if(preg_match("/[\200-\377]/",$subject)) {
260 $encoded_subject = base64_encode($subject);
261 $encoded_subject = wordwrap($encoded_subject,56,"\015\012",1);
262 $encoded_subject = "=?$charset?B?$encoded_subject?=";
263 $encoded_subject = str_replace("\015\012","?=\015\012 =?$charset?B?",$encoded_subject);
265 else $encoded_subject = $subject;
267 # Mailtext formatieren...
269 $mtpl = new Template
;
270 $mtpl->read_file($tpl_mail);
272 $mtpl->set_var('EMAIL', $email);
273 $mtpl->set_var('NAME', $name);
274 $mtpl->set_var('SUBJECT',$subject);
275 $mtpl->set_var('TIME', $time);
277 # ... Umgebungsvariablen einfuegen...
279 if(isset($allowed_envs) && is_array($allowed_envs)) {
280 foreach($allowed_envs as $allowed_env) {
281 if(isset($_SERVER[$allowed_env])) {
282 $mtpl->set_var('ENV_'.$allowed_env,$_SERVER[$allowed_env]);
285 $mtpl->set_var('ENV_'.$allowed_env,'');
290 # ... eigene Felder einfuegen
292 while(list($tpl_var,$user_field_content) = each($prepared_user_fields)) {
293 $mtpl->set_var($tpl_var,$user_field_content);
296 $mtpl->set_var('TEXT',$text);
300 $mailtext = $mtpl->get_template();
302 if($line_break) $mailtext = wordwrap($mailtext,$line_break);
304 $mailtext = preg_replace("/\015\012|\012|\015/","\n",$mailtext);
306 # ... und die Mail abschicken (bei einem Fehler erscheint eine Meldung)
308 if($recipient && is_array($aliases) && $aliases[$recipient]) {
309 # Empfaenger mit Alias-Namen
311 $mailto = $aliases[$recipient];
314 $mailto = semicolon_split($mailto);
316 for($x=0;$x<count($mailto);$x++
) {
317 mail($mailto[$x],$encoded_subject,$mailtext,$header) or die($err_send_error);
320 if($captcha_enable) session_destroy();
322 # Mail wurde erfolgreich versendet, also wird die Dankesseite angezeigt
325 # Per Redirect auf Seite umleiten
327 if(!preg_match('!://!',$return_url)) {
328 # URL vervollstaendigen
330 $http_host = ($_SERVER['HTTP_HOST']) ?
$_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
334 if($_SERVER['HTTPS']) {
335 # SSL-verschluesseltes HTTP
337 $protocol = 'https://';
338 if($_SERVER['SERVER_PORT'] != 443) $port = ':'.$_SERVER['SERVER_PORT'];
343 $protocol = 'http://';
344 if($_SERVER['SERVER_PORT'] != 80) $port = ':'.$_SERVER['SERVER_PORT'];
347 if(preg_match('!^/!',$return_url)) {
348 $return_url = $protocol.$http_host.$port.$return_url;
351 if(preg_match('!/$!',$_SERVER['SCRIPT_NAME'])) $path = substr($_SERVER['SCRIPT_NAME'],0,-1);
352 $path = substr($path,0,strrpos($path,'/')+
1);
353 if(!preg_match('!^/!',$path)) $path = '/'.$path;
355 $return_url = $protocol.$http_host.$port.$path.$return_url;
359 header('Status: 303 See Other');
360 header('Location: '.$return_url);
363 # Mit Template formatierte Seite anzeigen
365 $stpl = new Template
;
366 $stpl->read_file($tpl_sent);
368 $stpl->set_var('EMAIL', plain($email));
369 $stpl->set_var('MAIL', plain($mailtext));
370 $stpl->set_var('NAME', plain($name));
371 $stpl->set_var('SUBJECT',plain($subject));
372 $stpl->set_var('TEXT', plain($text));
373 $stpl->set_var('TIME', plain($time));
377 print $stpl->get_template();
patrick-canterino.de