]>
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: 3.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
24 require('config.php');
25 require('functions.php');
26 require('class.Template.php');
28 if($only_post && $_SERVER['REQUEST_METHOD'] != 'POST') show_fatal($err_only_post);
30 # Wenn Captchas aktiviert sind, Session starten
35 # Pruefen, ob die zulaessige Zahl der Falscheingaben bei Captchas ueberschritten wurde
37 if($captcha_max && isset($_SESSION['captcha_failed']) && $_SESSION['captcha_failed'] >= $captcha_max) {
38 show_fatal($err_captcha_max);
42 # Bestimmte Werte in Integers umwandeln
44 $name_min = intval($name_min);
45 $subject_min = intval($subject_min);
46 $text_min = intval($text_min);
47 $name_max = intval($name_max);
48 $subject_max = intval($subject_max);
49 $text_max = intval($text_max);
50 $line_break = intval($line_break);
52 # Formulardaten auslesen
54 $email = formdata('email');
55 $name = formdata('name');
56 $recipient = formdata('recipient');
57 $subject = formdata('subject');
58 $text = formdata('text');
60 # Pruefen, ob ueberhaupt was ausgefuellt wurde
62 if(!$name && !$email && !$text) show_user_error($err_nothing);
64 # Pruefen, ob alle wichtigen Formular-Felder ausgefuellt wurden
66 if(!$name) show_user_error($err_no_name);
67 if(!$email) show_user_error($err_no_email);
68 if(!$text) show_user_error($err_no_text);
70 if(!$subject) $subject = $default_subject; # Standard-Betreff
72 # Pruefen, ob Name, Betreff und Text lang genug sind
74 if($name_min && strlen($name) < $name_min) show_user_error($err_name_short);
75 if($subject_min && strlen($subject) < $subject_min) show_user_error($err_subject_short);
76 if($text_min && strlen($text) < $text_min) show_user_error($err_text_short);
78 # Pruefen, ob Name, Betreff und Text nicht zu lang sind
80 if($name_max && strlen($name) > $name_max) show_user_error($err_name_long);
81 if($subject_max && strlen($subject) > $subject_max) show_user_error($err_subject_long);
82 if($text_max && strlen($text) > $text_max) show_user_error($err_text_long);
84 # Pruefen, ob eingegebene E-Mail-Adresse gueltig ist
85 # Danke an "ich" aus dem SELFHTML Forum fuer die Muehe
87 $mail_regexp = ($no_tld)
88 ?
'/^([a-z0-9]+([\.]?|[-]?|[_]?)?)*([a-z0-9]+([\.]?[_]?)?)@([a-z0-9]+([\.]?|[-]?)?)*[a-z0-9]$/i'
89 : '/^([a-z0-9]+([\.]?|[-]?|[_]?)?)*([a-z0-9]+([\.]?[_]?)?)@([a-z0-9]+([\.]?|[-]?)?)*[a-z0-9]+\.{1}[a-z]{2,6}$/i';
91 if(!preg_match($mail_regexp,$email)) show_user_error($err_invalid_email);
93 # Individuelle Felder einlesen
95 $prepared_user_fields = array();
97 while(list($user_field,$user_field_data) = each($user_fields)) {
98 $user_field_content = formdata($user_field);
100 # Pruefung, ob das Feld gesetzt wurde und ob es optional ist
102 if((!$user_field_data['optional']) && (!$user_field_content)) {
103 show_user_error($user_field_data['errors']['not_set']);
105 elseif($user_field_data['optional'] && (!$user_field_content)) {
106 # Wenn das Feld optional ist und nicht gesetzt wurde, brechen wir hier ab
107 # und machen mit dem naechsten Feld weiter
109 $prepared_user_fields[$user_field_data['tpl_var']] = '';
115 if(isset($user_field_data['min']) && $user_field_data['min']) {
116 if(strlen($user_field_content) < $user_field_data['min']) {
117 show_user_error($user_field_data['errors']['too_short']);
121 if(isset($user_field_data['min']) && $user_field_data['max']) {
122 if(strlen($user_field_content) > $user_field_data['max']) {
123 show_user_error($user_field_data['errors']['too_long']);
127 # Pruefung gegen frei definierbare Pruef-Funktion
129 if(isset($user_field_data['check']) && $user_field_data['check'] != '') {
130 if(function_exists($user_field_data['check']) && call_user_func($user_field_data['check'],$user_field_content) === false) {
131 show_user_error($user_field_data['errors']['check_fail']);
135 # Pruefung gegen frei definierbaren regulaeren Ausdruck
137 if(isset($user_field_data['regex']) && $user_field_data['regex'] != '') {
138 if(!preg_match($user_field_data['regex'],$user_field_content)) {
139 show_user_error($user_field_data['errors']['match_fail']);
143 # Formularfeld durch frei definierbare Funktion bearbeiten
145 if(isset($user_field_data['prepare']) && $user_field_data['prepare'] != '') {
146 if(function_exists($user_field_data['check'])) {
147 $user_field_content = call_user_func($user_field_data['prepare'],$user_field_content);
151 # Fertige Formulardaten wegkopieren und fuer spaeter zur Verfuegung stellen
153 $prepared_user_fields[$user_field_data['tpl_var']] = $user_field_content;
156 # Captcha ueberpruefen
158 if($captcha_enable) {
159 $captcha_input = formdata('captcha');
160 $captcha_incorrect = false;
162 if(isset($_SESSION) && isset($_SESSION['captcha'])) {
164 if(!$captcha_input ||
empty($captcha_input) ||
$captcha_input != $_SESSION['captcha']) $captcha_incorrect = true;
167 if(!$captcha_input ||
empty($captcha_input) ||
strtolower($captcha_input) != strtolower($_SESSION['captcha'])) $captcha_incorrect = true;
170 if($captcha_incorrect) {
172 if(isset($_SESSION['captcha_failed']) && $_SESSION['captcha_failed']) {
173 $_SESSION['captcha_failed']++
;
176 $_SESSION['captcha_failed'] = 1;
180 if($captcha_max && isset($_SESSION['captcha_failed']) && $_SESSION['captcha_failed'] >= $captcha_max) {
181 show_fatal($err_captcha_max);
184 show_user_error($err_captcha_incorrect);
189 show_fatal($err_captcha_session_failure);
193 # Referer ueberpruefen
196 $referer_ok = semicolon_split($referer_ok);
198 $referer_parts = parse_url($_SERVER['HTTP_REFERER']);
199 $referer_host = $referer_parts['host'];
201 if(in_array($referer_host,$referer_ok) === false) show_fatal($err_referer_not_ok);
204 # Zeilenumbrueche in Namen und Betreff durch Leerzeichen ersetzen
206 $name = preg_replace("/\015\012|\012|\015/",' ',$name);
207 $subject = preg_replace("/\015\012|\012|\015/",' ',$subject);
209 # Uhrzeit formatieren
211 date_default_timezone_set($timezone);
212 $time = strftime($timeformat);
218 if($friendly_sender) {
219 if($friendly_sender == 2) {
220 if(preg_match("/[\200-\377]/",$name)) {
221 # Absendernamen mit Base64 codieren
223 $encoded_name = base64_encode($name);
224 $encoded_name = wordwrap($encoded_name,56,"\015\012",1);
225 $encoded_name = "=?$charset?B?$encoded_name?=";
226 $encoded_name = str_replace("\015\012","?=\015\012 =?$charset?B?",$encoded_name);
229 # Bestimmte Zeichen im Absendernamen codieren
231 $encoded_name = str_replace('"','\"',$name);
232 $encoded_name = str_replace('(','\\(',$encoded_name);
233 $encoded_name = str_replace(')','\\)',$encoded_name);
234 $encoded_name = '"'.$encoded_name.'"';
237 $header = "From: $encoded_name <$email>\n";
239 else $header = "From: $email\n";
243 if(!$friendly_sender) $header .= "Reply-To: $email\n";
244 $header .= 'Content-Type: text/plain; charset="'.$charset.'"'."\n";
245 $header .= 'Content-Transfer-Encoding: 8bit'."\n";
246 $header .= 'MIME-Version: 1.0'."\n";
247 $header .= 'X-Mailer: Form E-Mail 3.4.1'."\n";
248 $header .= 'X-Sender-IP: ['.$_SERVER['REMOTE_ADDR'].']';
250 else $header .= "Reply-To: $email";
252 $header = preg_replace("/\015\012|\012|\015/","\n",$header);
254 # Betreff mit Base64 codieren, wenn er Zeichen enthaelt,
255 # die nicht aus dem ASCII-Zeichensatz stammen
257 if(preg_match("/[\200-\377]/",$subject)) {
258 $encoded_subject = base64_encode($subject);
259 $encoded_subject = wordwrap($encoded_subject,56,"\015\012",1);
260 $encoded_subject = "=?$charset?B?$encoded_subject?=";
261 $encoded_subject = str_replace("\015\012","?=\015\012 =?$charset?B?",$encoded_subject);
263 else $encoded_subject = $subject;
265 # Mailtext formatieren...
267 $mtpl = new Template
;
268 $mtpl->read_file($tpl_mail);
270 $mtpl->set_var('EMAIL', $email);
271 $mtpl->set_var('NAME', $name);
272 $mtpl->set_var('SUBJECT',$subject);
273 $mtpl->set_var('TIME', $time);
275 # ... Umgebungsvariablen einfuegen...
277 if(isset($allowed_envs) && is_array($allowed_envs)) {
278 foreach($allowed_envs as $allowed_env) {
279 if(isset($_SERVER[$allowed_env])) {
280 $mtpl->set_var('ENV_'.$allowed_env,$_SERVER[$allowed_env]);
283 $mtpl->set_var('ENV_'.$allowed_env,'');
288 # ... eigene Felder einfuegen
290 while(list($tpl_var,$user_field_content) = each($prepared_user_fields)) {
291 $mtpl->set_var($tpl_var,$user_field_content);
294 $mtpl->set_var('TEXT',$text);
298 $mailtext = $mtpl->get_template();
300 if($line_break) $mailtext = wordwrap($mailtext,$line_break);
302 $mailtext = preg_replace("/\015\012|\012|\015/","\n",$mailtext);
304 # ... und die Mail abschicken (bei einem Fehler erscheint eine Meldung)
306 if($recipient && is_array($aliases) && $aliases[$recipient]) {
307 # Empfaenger mit Alias-Namen
309 $mailto = $aliases[$recipient];
312 $mailto = semicolon_split($mailto);
314 for($x=0;$x<count($mailto);$x++
) {
315 mail($mailto[$x],$encoded_subject,$mailtext,$header) or die($err_send_error);
318 if($captcha_enable) session_destroy();
320 # Mail wurde erfolgreich versendet, also wird die Dankesseite angezeigt
323 # Per Redirect auf Seite umleiten
325 if(!preg_match('!://!',$return_url)) {
326 # URL vervollstaendigen
328 $http_host = ($_SERVER['HTTP_HOST']) ?
$_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
332 if($_SERVER['HTTPS']) {
333 # SSL-verschluesseltes HTTP
335 $protocol = 'https://';
336 if($_SERVER['SERVER_PORT'] != 443) $port = ':'.$_SERVER['SERVER_PORT'];
341 $protocol = 'http://';
342 if($_SERVER['SERVER_PORT'] != 80) $port = ':'.$_SERVER['SERVER_PORT'];
345 if(preg_match('!^/!',$return_url)) {
346 $return_url = $protocol.$http_host.$port.$return_url;
349 if(preg_match('!/$!',$_SERVER['SCRIPT_NAME'])) $path = substr($_SERVER['SCRIPT_NAME'],0,-1);
350 $path = substr($path,0,strrpos($path,'/')+
1);
351 if(!preg_match('!^/!',$path)) $path = '/'.$path;
353 $return_url = $protocol.$http_host.$port.$path.$return_url;
357 header('Status: 303 See Other');
358 header('Location: '.$return_url);
361 # Mit Template formatierte Seite anzeigen
363 $stpl = new Template
;
364 $stpl->read_file($tpl_sent);
366 $stpl->set_var('EMAIL', plain($email));
367 $stpl->set_var('MAIL', plain($mailtext));
368 $stpl->set_var('NAME', plain($name));
369 $stpl->set_var('SUBJECT',plain($subject));
370 $stpl->set_var('TEXT', plain($text));
371 $stpl->set_var('TIME', plain($time));
375 print $stpl->get_template();
patrick-canterino.de