]>
git.p6c8.net - form-email.git/blob - form-email/mailer.php
b32f095ca6efd6f911c8ab76183e19ee2e751ff7
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(isset($user_field_data['check']) && $user_field_data['check'] != '') {
101 if(function_exists($user_field_data['check']) && call_user_func($user_field_data['check'],$user_field_content) === false) {
102 show_user_error($user_field_data['errors']['check_fail']);
106 if(isset($user_field_data['regex']) && $user_field_data['regex'] != '') {
107 if(!preg_match($user_field_data['regex'],$user_field_content)) {
108 show_user_error($user_field_data['errors']['match_fail']);
112 $prepared_user_fields[$user_field_data['tpl_var']] = $user_field_content;
115 # Captcha ueberpruefen
117 if($captcha_enable) {
118 $captcha_input = formdata('captcha');
119 $captcha_incorrect = false;
121 if(isset($_SESSION) && isset($_SESSION['captcha'])) {
123 if(!$captcha_input ||
empty($captcha_input) ||
$captcha_input != $_SESSION['captcha']) $captcha_incorrect = true;
126 if(!$captcha_input ||
empty($captcha_input) ||
strtolower($captcha_input) != strtolower($_SESSION['captcha'])) $captcha_incorrect = true;
129 if($captcha_incorrect) {
131 if(isset($_SESSION['captcha_failed']) && $_SESSION['captcha_failed']) {
132 $_SESSION['captcha_failed']++
;
135 $_SESSION['captcha_failed'] = 1;
139 if($captcha_max && isset($_SESSION['captcha_failed']) && $_SESSION['captcha_failed'] >= $captcha_max) {
140 show_fatal($err_captcha_max);
143 show_user_error($err_captcha_incorrect);
148 show_fatal($err_captcha_session_failure);
152 # Referer ueberpruefen
155 $referer_ok = semicolon_split($referer_ok);
157 $referer_parts = parse_url($_SERVER['HTTP_REFERER']);
158 $referer_host = $referer_parts['host'];
160 if(in_array($referer_host,$referer_ok) === false) show_fatal($err_referer_not_ok);
163 # Zeilenumbrueche in Namen und Betreff durch Leerzeichen ersetzen
165 $name = preg_replace("/\015\012|\012|\015/",' ',$name);
166 $subject = preg_replace("/\015\012|\012|\015/",' ',$subject);
168 # Uhrzeit formatieren
170 date_default_timezone_set($timezone);
171 $time = strftime($timeformat);
177 if($friendly_sender) {
178 if($friendly_sender == 2) {
179 if(preg_match("/[\200-\377]/",$name)) {
180 # Absendernamen mit Base64 codieren
182 $encoded_name = base64_encode($name);
183 $encoded_name = wordwrap($encoded_name,56,"\015\012",1);
184 $encoded_name = "=?$charset?B?$encoded_name?=";
185 $encoded_name = str_replace("\015\012","?=\015\012 =?$charset?B?",$encoded_name);
188 # Bestimmte Zeichen im Absendernamen codieren
190 $encoded_name = str_replace('"','\"',$name);
191 $encoded_name = str_replace('(','\\(',$encoded_name);
192 $encoded_name = str_replace(')','\\)',$encoded_name);
193 $encoded_name = '"'.$encoded_name.'"';
196 $header = "From: $encoded_name <$email>\n";
198 else $header = "From: $email\n";
202 if(!$friendly_sender) $header .= "Reply-To: $email\n";
203 $header .= 'Content-Type: text/plain; charset="'.$charset.'"'."\n";
204 $header .= 'Content-Transfer-Encoding: 8bit'."\n";
205 $header .= 'MIME-Version: 1.0'."\n";
206 $header .= 'X-Mailer: Form E-Mail 3.4.1'."\n";
207 $header .= 'X-Sender-IP: ['.$_SERVER['REMOTE_ADDR'].']';
209 else $header .= "Reply-To: $email";
211 $header = preg_replace("/\015\012|\012|\015/","\n",$header);
213 # Betreff mit Base64 codieren, wenn er Zeichen enthaelt,
214 # die nicht aus dem ASCII-Zeichensatz stammen
216 if(preg_match("/[\200-\377]/",$subject)) {
217 $encoded_subject = base64_encode($subject);
218 $encoded_subject = wordwrap($encoded_subject,56,"\015\012",1);
219 $encoded_subject = "=?$charset?B?$encoded_subject?=";
220 $encoded_subject = str_replace("\015\012","?=\015\012 =?$charset?B?",$encoded_subject);
222 else $encoded_subject = $subject;
224 # Mailtext formatieren...
226 $mtpl = new Template
;
227 $mtpl->read_file($tpl_mail);
229 $mtpl->set_var('EMAIL', $email);
230 $mtpl->set_var('NAME', $name);
231 $mtpl->set_var('SUBJECT',$subject);
232 $mtpl->set_var('TIME', $time);
234 # ... Umgebungsvariablen einfuegen...
236 if(isset($allowed_envs) && is_array($allowed_envs)) {
237 foreach($allowed_envs as $allowed_env) {
238 if(isset($_SERVER[$allowed_env])) {
239 $mtpl->set_var('ENV_'.$allowed_env,$_SERVER[$allowed_env]);
242 $mtpl->set_var('ENV_'.$allowed_env,'');
247 # ... eigene Felder einfuegen
249 while(list($tpl_var,$user_field_content) = each($prepared_user_fields)) {
250 $mtpl->set_var($tpl_var,$user_field_content);
253 $mtpl->set_var('TEXT',$text);
257 $mailtext = $mtpl->get_template();
259 if($line_break) $mailtext = wordwrap($mailtext,$line_break);
261 $mailtext = preg_replace("/\015\012|\012|\015/","\n",$mailtext);
263 # ... und die Mail abschicken (bei einem Fehler erscheint eine Meldung)
265 if($recipient && is_array($aliases) && $aliases[$recipient]) {
266 # Empfaenger mit Alias-Namen
268 $mailto = $aliases[$recipient];
271 $mailto = semicolon_split($mailto);
273 for($x=0;$x<count($mailto);$x++
) {
274 mail($mailto[$x],$encoded_subject,$mailtext,$header) or die($err_send_error);
277 if($captcha_enable) session_destroy();
279 # Mail wurde erfolgreich versendet, also wird die Dankesseite angezeigt
282 # Per Redirect auf Seite umleiten
284 if(!preg_match('!://!',$return_url)) {
285 # URL vervollstaendigen
287 $http_host = ($_SERVER['HTTP_HOST']) ?
$_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
291 if($_SERVER['HTTPS']) {
292 # SSL-verschluesseltes HTTP
294 $protocol = 'https://';
295 if($_SERVER['SERVER_PORT'] != 443) $port = ':'.$_SERVER['SERVER_PORT'];
300 $protocol = 'http://';
301 if($_SERVER['SERVER_PORT'] != 80) $port = ':'.$_SERVER['SERVER_PORT'];
304 if(preg_match('!^/!',$return_url)) {
305 $return_url = $protocol.$http_host.$port.$return_url;
308 if(preg_match('!/$!',$_SERVER['SCRIPT_NAME'])) $path = substr($_SERVER['SCRIPT_NAME'],0,-1);
309 $path = substr($path,0,strrpos($path,'/')+
1);
310 if(!preg_match('!^/!',$path)) $path = '/'.$path;
312 $return_url = $protocol.$http_host.$port.$path.$return_url;
316 header('Status: 303 See Other');
317 header('Location: '.$return_url);
320 # Mit Template formatierte Seite anzeigen
322 $stpl = new Template
;
323 $stpl->read_file($tpl_sent);
325 $stpl->set_var('EMAIL', plain($email));
326 $stpl->set_var('MAIL', plain($mailtext));
327 $stpl->set_var('NAME', plain($name));
328 $stpl->set_var('SUBJECT',plain($subject));
329 $stpl->set_var('TEXT', plain($text));
330 $stpl->set_var('TIME', plain($time));
334 print $stpl->get_template();
patrick-canterino.de