]>
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: 7.11.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 if((!$user_field_data['optional']) && (!$user_field_content)) {
101 show_user_error($user_field_data['errors']['not_set']);
103 elseif($user_field_data['optional'] && (!$user_field_content)) {
104 $prepared_user_fields[$user_field_data['tpl_var']] = '';
108 if(isset($user_field_data['check']) && $user_field_data['check'] != '') {
109 if(function_exists($user_field_data['check']) && call_user_func($user_field_data['check'],$user_field_content) === false) {
110 show_user_error($user_field_data['errors']['check_fail']);
114 if(isset($user_field_data['regex']) && $user_field_data['regex'] != '') {
115 if(!preg_match($user_field_data['regex'],$user_field_content)) {
116 show_user_error($user_field_data['errors']['match_fail']);
120 $prepared_user_fields[$user_field_data['tpl_var']] = $user_field_content;
123 # Captcha ueberpruefen
125 if($captcha_enable) {
126 $captcha_input = formdata('captcha');
127 $captcha_incorrect = false;
129 if(isset($_SESSION) && isset($_SESSION['captcha'])) {
131 if(!$captcha_input ||
empty($captcha_input) ||
$captcha_input != $_SESSION['captcha']) $captcha_incorrect = true;
134 if(!$captcha_input ||
empty($captcha_input) ||
strtolower($captcha_input) != strtolower($_SESSION['captcha'])) $captcha_incorrect = true;
137 if($captcha_incorrect) {
139 if(isset($_SESSION['captcha_failed']) && $_SESSION['captcha_failed']) {
140 $_SESSION['captcha_failed']++
;
143 $_SESSION['captcha_failed'] = 1;
147 if($captcha_max && isset($_SESSION['captcha_failed']) && $_SESSION['captcha_failed'] >= $captcha_max) {
148 show_fatal($err_captcha_max);
151 show_user_error($err_captcha_incorrect);
156 show_fatal($err_captcha_session_failure);
160 # Referer ueberpruefen
163 $referer_ok = semicolon_split($referer_ok);
165 $referer_parts = parse_url($_SERVER['HTTP_REFERER']);
166 $referer_host = $referer_parts['host'];
168 if(in_array($referer_host,$referer_ok) === false) show_fatal($err_referer_not_ok);
171 # Zeilenumbrueche in Namen und Betreff durch Leerzeichen ersetzen
173 $name = preg_replace("/\015\012|\012|\015/",' ',$name);
174 $subject = preg_replace("/\015\012|\012|\015/",' ',$subject);
176 # Uhrzeit formatieren
178 date_default_timezone_set($timezone);
179 $time = strftime($timeformat);
185 if($friendly_sender) {
186 if($friendly_sender == 2) {
187 if(preg_match("/[\200-\377]/",$name)) {
188 # Absendernamen mit Base64 codieren
190 $encoded_name = base64_encode($name);
191 $encoded_name = wordwrap($encoded_name,56,"\015\012",1);
192 $encoded_name = "=?$charset?B?$encoded_name?=";
193 $encoded_name = str_replace("\015\012","?=\015\012 =?$charset?B?",$encoded_name);
196 # Bestimmte Zeichen im Absendernamen codieren
198 $encoded_name = str_replace('"','\"',$name);
199 $encoded_name = str_replace('(','\\(',$encoded_name);
200 $encoded_name = str_replace(')','\\)',$encoded_name);
201 $encoded_name = '"'.$encoded_name.'"';
204 $header = "From: $encoded_name <$email>\n";
206 else $header = "From: $email\n";
210 if(!$friendly_sender) $header .= "Reply-To: $email\n";
211 $header .= 'Content-Type: text/plain; charset="'.$charset.'"'."\n";
212 $header .= 'Content-Transfer-Encoding: 8bit'."\n";
213 $header .= 'MIME-Version: 1.0'."\n";
214 $header .= 'X-Mailer: Form E-Mail 3.4.1'."\n";
215 $header .= 'X-Sender-IP: ['.$_SERVER['REMOTE_ADDR'].']';
217 else $header .= "Reply-To: $email";
219 $header = preg_replace("/\015\012|\012|\015/","\n",$header);
221 # Betreff mit Base64 codieren, wenn er Zeichen enthaelt,
222 # die nicht aus dem ASCII-Zeichensatz stammen
224 if(preg_match("/[\200-\377]/",$subject)) {
225 $encoded_subject = base64_encode($subject);
226 $encoded_subject = wordwrap($encoded_subject,56,"\015\012",1);
227 $encoded_subject = "=?$charset?B?$encoded_subject?=";
228 $encoded_subject = str_replace("\015\012","?=\015\012 =?$charset?B?",$encoded_subject);
230 else $encoded_subject = $subject;
232 # Mailtext formatieren...
234 $mtpl = new Template
;
235 $mtpl->read_file($tpl_mail);
237 $mtpl->set_var('EMAIL', $email);
238 $mtpl->set_var('NAME', $name);
239 $mtpl->set_var('SUBJECT',$subject);
240 $mtpl->set_var('TIME', $time);
242 # ... Umgebungsvariablen einfuegen...
244 if(isset($allowed_envs) && is_array($allowed_envs)) {
245 foreach($allowed_envs as $allowed_env) {
246 if(isset($_SERVER[$allowed_env])) {
247 $mtpl->set_var('ENV_'.$allowed_env,$_SERVER[$allowed_env]);
250 $mtpl->set_var('ENV_'.$allowed_env,'');
255 # ... eigene Felder einfuegen
257 while(list($tpl_var,$user_field_content) = each($prepared_user_fields)) {
258 $mtpl->set_var($tpl_var,$user_field_content);
261 $mtpl->set_var('TEXT',$text);
265 $mailtext = $mtpl->get_template();
267 if($line_break) $mailtext = wordwrap($mailtext,$line_break);
269 $mailtext = preg_replace("/\015\012|\012|\015/","\n",$mailtext);
271 # ... und die Mail abschicken (bei einem Fehler erscheint eine Meldung)
273 if($recipient && is_array($aliases) && $aliases[$recipient]) {
274 # Empfaenger mit Alias-Namen
276 $mailto = $aliases[$recipient];
279 $mailto = semicolon_split($mailto);
281 for($x=0;$x<count($mailto);$x++
) {
282 mail($mailto[$x],$encoded_subject,$mailtext,$header) or die($err_send_error);
285 if($captcha_enable) session_destroy();
287 # Mail wurde erfolgreich versendet, also wird die Dankesseite angezeigt
290 # Per Redirect auf Seite umleiten
292 if(!preg_match('!://!',$return_url)) {
293 # URL vervollstaendigen
295 $http_host = ($_SERVER['HTTP_HOST']) ?
$_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
299 if($_SERVER['HTTPS']) {
300 # SSL-verschluesseltes HTTP
302 $protocol = 'https://';
303 if($_SERVER['SERVER_PORT'] != 443) $port = ':'.$_SERVER['SERVER_PORT'];
308 $protocol = 'http://';
309 if($_SERVER['SERVER_PORT'] != 80) $port = ':'.$_SERVER['SERVER_PORT'];
312 if(preg_match('!^/!',$return_url)) {
313 $return_url = $protocol.$http_host.$port.$return_url;
316 if(preg_match('!/$!',$_SERVER['SCRIPT_NAME'])) $path = substr($_SERVER['SCRIPT_NAME'],0,-1);
317 $path = substr($path,0,strrpos($path,'/')+
1);
318 if(!preg_match('!^/!',$path)) $path = '/'.$path;
320 $return_url = $protocol.$http_host.$port.$path.$return_url;
324 header('Status: 303 See Other');
325 header('Location: '.$return_url);
328 # Mit Template formatierte Seite anzeigen
330 $stpl = new Template
;
331 $stpl->read_file($tpl_sent);
333 $stpl->set_var('EMAIL', plain($email));
334 $stpl->set_var('MAIL', plain($mailtext));
335 $stpl->set_var('NAME', plain($name));
336 $stpl->set_var('SUBJECT',plain($subject));
337 $stpl->set_var('TEXT', plain($text));
338 $stpl->set_var('TIME', plain($time));
342 print $stpl->get_template();
patrick-canterino.de