]> git.p6c8.net - form-email.git/blob - form-email/mailer.php
license.txt aktualisiert
[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: 7.11.2011
11 #
12 # Copyright (C) 2002-2011 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 require('config.php');
25 require('functions.php');
26 require('class.Template.php');
27
28 if($only_post && $_SERVER['REQUEST_METHOD'] != 'POST') show_fatal($err_only_post);
29
30 # Wenn Captchas aktiviert sind, Session starten
31
32 if($captcha_enable) {
33 session_start();
34
35 # Pruefen, ob die zulaessige Zahl der Falscheingaben bei Captchas ueberschritten wurde
36
37 if($captcha_max && isset($_SESSION['captcha_failed']) && $_SESSION['captcha_failed'] >= $captcha_max) {
38 show_fatal($err_captcha_max);
39 }
40 }
41
42 # Bestimmte Werte in Integers umwandeln
43
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);
51
52 # Formulardaten auslesen
53
54 $email = formdata('email');
55 $name = formdata('name');
56 $recipient = formdata('recipient');
57 $subject = formdata('subject');
58 $text = formdata('text');
59
60 # Pruefen, ob ueberhaupt was ausgefuellt wurde
61
62 if(!$name && !$email && !$text) show_user_error($err_nothing);
63
64 # Pruefen, ob alle wichtigen Formular-Felder ausgefuellt wurden
65
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);
69
70 if(!$subject) $subject = $default_subject; # Standard-Betreff
71
72 # Pruefen, ob Name, Betreff und Text lang genug sind
73
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);
77
78 # Pruefen, ob Name, Betreff und Text nicht zu lang sind
79
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);
83
84 # Pruefen, ob eingegebene E-Mail-Adresse gueltig ist
85 # Danke an "ich" aus dem SELFHTML Forum fuer die Muehe
86
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';
90
91 if(!preg_match($mail_regexp,$email)) show_user_error($err_invalid_email);
92
93 # Individuelle Felder einlesen
94
95 $prepared_user_fields = array();
96
97 while(list($user_field,$user_field_data) = each($user_fields)) {
98 $user_field_content = formdata($user_field);
99
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)) {
102 show_user_error($user_field_data['errors']['check_fail']);
103 }
104 }
105
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']);
109 }
110 }
111
112 $prepared_user_fields[$user_field_data['tpl_var']] = $user_field_content;
113 }
114
115 # Captcha ueberpruefen
116
117 if($captcha_enable) {
118 $captcha_input = formdata('captcha');
119 $captcha_incorrect = false;
120
121 if(isset($_SESSION) && isset($_SESSION['captcha'])) {
122 if($captcha_case) {
123 if(!$captcha_input || empty($captcha_input) || $captcha_input != $_SESSION['captcha']) $captcha_incorrect = true;
124 }
125 else {
126 if(!$captcha_input || empty($captcha_input) || strtolower($captcha_input) != strtolower($_SESSION['captcha'])) $captcha_incorrect = true;
127 }
128
129 if($captcha_incorrect) {
130 if($captcha_max) {
131 if(isset($_SESSION['captcha_failed']) && $_SESSION['captcha_failed']) {
132 $_SESSION['captcha_failed']++;
133 }
134 else {
135 $_SESSION['captcha_failed'] = 1;
136 }
137 }
138
139 if($captcha_max && isset($_SESSION['captcha_failed']) && $_SESSION['captcha_failed'] >= $captcha_max) {
140 show_fatal($err_captcha_max);
141 }
142 else {
143 show_user_error($err_captcha_incorrect);
144 }
145 }
146 }
147 else {
148 show_fatal($err_captcha_session_failure);
149 }
150 }
151
152 # Referer ueberpruefen
153
154 if($check_referer) {
155 $referer_ok = semicolon_split($referer_ok);
156
157 $referer_parts = parse_url($_SERVER['HTTP_REFERER']);
158 $referer_host = $referer_parts['host'];
159
160 if(in_array($referer_host,$referer_ok) === false) show_fatal($err_referer_not_ok);
161 }
162
163 # Zeilenumbrueche in Namen und Betreff durch Leerzeichen ersetzen
164
165 $name = preg_replace("/\015\012|\012|\015/",' ',$name);
166 $subject = preg_replace("/\015\012|\012|\015/",' ',$subject);
167
168 # Uhrzeit formatieren
169
170 date_default_timezone_set($timezone);
171 $time = strftime($timeformat);
172
173 # Header der Mail
174
175 $header = '';
176
177 if($friendly_sender) {
178 if($friendly_sender == 2) {
179 if(preg_match("/[\200-\377]/",$name)) {
180 # Absendernamen mit Base64 codieren
181
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);
186 }
187 else {
188 # Bestimmte Zeichen im Absendernamen codieren
189
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.'"';
194 }
195
196 $header = "From: $encoded_name <$email>\n";
197 }
198 else $header = "From: $email\n";
199 }
200
201 if($add_header) {
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'].']';
208 }
209 else $header .= "Reply-To: $email";
210
211 $header = preg_replace("/\015\012|\012|\015/","\n",$header);
212
213 # Betreff mit Base64 codieren, wenn er Zeichen enthaelt,
214 # die nicht aus dem ASCII-Zeichensatz stammen
215
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);
221 }
222 else $encoded_subject = $subject;
223
224 # Mailtext formatieren...
225
226 $mtpl = new Template;
227 $mtpl->read_file($tpl_mail);
228
229 $mtpl->set_var('EMAIL', $email);
230 $mtpl->set_var('NAME', $name);
231 $mtpl->set_var('SUBJECT',$subject);
232 $mtpl->set_var('TIME', $time);
233
234 # ... Umgebungsvariablen einfuegen...
235
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]);
240 }
241 else {
242 $mtpl->set_var('ENV_'.$allowed_env,'');
243 }
244 }
245 }
246
247 # ... eigene Felder einfuegen
248
249 while(list($tpl_var,$user_field_content) = each($prepared_user_fields)) {
250 $mtpl->set_var($tpl_var,$user_field_content);
251 }
252
253 $mtpl->set_var('TEXT',$text);
254
255 $mtpl->parse();
256
257 $mailtext = $mtpl->get_template();
258
259 if($line_break) $mailtext = wordwrap($mailtext,$line_break);
260
261 $mailtext = preg_replace("/\015\012|\012|\015/","\n",$mailtext);
262
263 # ... und die Mail abschicken (bei einem Fehler erscheint eine Meldung)
264
265 if($recipient && is_array($aliases) && $aliases[$recipient]) {
266 # Empfaenger mit Alias-Namen
267
268 $mailto = $aliases[$recipient];
269 }
270
271 $mailto = semicolon_split($mailto);
272
273 for($x=0;$x<count($mailto);$x++) {
274 mail($mailto[$x],$encoded_subject,$mailtext,$header) or die($err_send_error);
275 }
276
277 if($captcha_enable) session_destroy();
278
279 # Mail wurde erfolgreich versendet, also wird die Dankesseite angezeigt
280
281 if($return_url) {
282 # Per Redirect auf Seite umleiten
283
284 if(!preg_match('!://!',$return_url)) {
285 # URL vervollstaendigen
286
287 $http_host = ($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
288 $protocol = '';
289 $port = '';
290
291 if($_SERVER['HTTPS']) {
292 # SSL-verschluesseltes HTTP
293
294 $protocol = 'https://';
295 if($_SERVER['SERVER_PORT'] != 443) $port = ':'.$_SERVER['SERVER_PORT'];
296 }
297 else {
298 # Normales HTTP
299
300 $protocol = 'http://';
301 if($_SERVER['SERVER_PORT'] != 80) $port = ':'.$_SERVER['SERVER_PORT'];
302 }
303
304 if(preg_match('!^/!',$return_url)) {
305 $return_url = $protocol.$http_host.$port.$return_url;
306 }
307 else {
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;
311
312 $return_url = $protocol.$http_host.$port.$path.$return_url;
313 }
314 }
315
316 header('Status: 303 See Other');
317 header('Location: '.$return_url);
318 }
319 else {
320 # Mit Template formatierte Seite anzeigen
321
322 $stpl = new Template;
323 $stpl->read_file($tpl_sent);
324
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));
331
332 $stpl->parse();
333
334 print $stpl->get_template();
335 }
336
337 #
338 ### Ende ###
339
340 ?>

patrick-canterino.de