]> git.p6c8.net - form-email.git/blob - form-email/mailer.php
Form E-Mail 3.4.1 hinzugefuegt
[form-email.git] / form-email / mailer.php
1 <?php
2
3 #
4 # Form E-Mail 3.4.1
5 #
6 # Versenden einer E-Mail, die ueber ein Kontaktformular
7 # geschrieben wurde.
8 #
9 # Autor: Patrick Canterino <patrick@patshaping.de>
10 # Letzte Aenderung: 24.2.2011
11 #
12 # Copyright (C) 2002-2011 Patrick Canterino
13 #
14 # Diese Datei kann unter den Bedingungen der "Artistic License 1.0"
15 # weitergegeben und / oder veraendert werden.
16 # Siehe:
17 # http://www.opensource.org/licenses/artistic-license-1.0.php
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 {
34 session_start();
35
36 # Pruefen, ob die zulaessige Zahl der Falscheingaben bei Captchas ueberschritten wurde
37
38 if($captcha_max && isset($_SESSION['captcha_failed']) && $_SESSION['captcha_failed'] >= $captcha_max)
39 {
40 show_fatal($err_captcha_max);
41 }
42 }
43
44 # Bestimmte Werte in Integers umwandeln
45
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);
53
54 # Formulardaten auslesen
55
56 $email = formdata('email');
57 $name = formdata('name');
58 $recipient = formdata('recipient');
59 $subject = formdata('subject');
60 $text = formdata('text');
61
62 # Pruefen, ob ueberhaupt was ausgefuellt wurde
63
64 if(!$name && !$email && !$text) show_user_error($err_nothing);
65
66 # Pruefen, ob alle wichtigen Formular-Felder ausgefuellt wurden
67
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);
71
72 if(!$subject) $subject = $default_subject; # Standard-Betreff
73
74 # Pruefen, ob Name, Betreff und Text lang genug sind
75
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);
79
80 # Pruefen, ob Name, Betreff und Text nicht zu lang sind
81
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);
85
86 # Pruefen, ob eingegebene E-Mail-Adresse gueltig ist
87 # Danke an "ich" aus dem SELFHTML Forum fuer die Muehe
88
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';
92
93 if(!preg_match($mail_regexp,$email)) show_user_error($err_invalid_email);
94
95 # Captcha ueberpruefen
96
97 if($captcha_enable)
98 {
99 $captcha_input = formdata('captcha');
100 $captcha_incorrect = false;
101
102 if(isset($_SESSION) && isset($_SESSION['captcha']))
103 {
104 if($captcha_case)
105 {
106 if(!$captcha_input || empty($captcha_input) || $captcha_input != $_SESSION['captcha']) $captcha_incorrect = true;
107 }
108 else
109 {
110 if(!$captcha_input || empty($captcha_input) || strtolower($captcha_input) != strtolower($_SESSION['captcha'])) $captcha_incorrect = true;
111 }
112
113 if($captcha_incorrect)
114 {
115 if($captcha_max)
116 {
117 if(isset($_SESSION['captcha_failed']) && $_SESSION['captcha_failed'])
118 {
119 $_SESSION['captcha_failed']++;
120 }
121 else
122 {
123 $_SESSION['captcha_failed'] = 1;
124 }
125 }
126
127 if($captcha_max && isset($_SESSION['captcha_failed']) && $_SESSION['captcha_failed'] >= $captcha_max)
128 {
129 show_fatal($err_captcha_max);
130 }
131 else
132 {
133 show_user_error($err_captcha_incorrect);
134 }
135 }
136 }
137 else
138 {
139 show_fatal($err_captcha_session_failure);
140 }
141 }
142
143 # Referer ueberpruefen
144
145 if($check_referer)
146 {
147 $referer_ok = semicolon_split($referer_ok);
148
149 $referer_parts = parse_url($_SERVER['HTTP_REFERER']);
150 $referer_host = $referer_parts['host'];
151
152 if(in_array($referer_host,$referer_ok) === false) show_fatal($err_referer_not_ok);
153 }
154
155 # Zeilenumbrueche in Namen und Betreff durch Leerzeichen ersetzen
156
157 $name = preg_replace("/\015\012|\012|\015/",' ',$name);
158 $subject = preg_replace("/\015\012|\012|\015/",' ',$subject);
159
160 # Uhrzeit formatieren
161
162 date_default_timezone_set($timezone);
163 $time = strftime($timeformat);
164
165 # Header der Mail
166
167 $header = '';
168
169 if($friendly_sender)
170 {
171 if($friendly_sender == 2)
172 {
173 if(preg_match("/[\200-\377]/",$name))
174 {
175 # Absendernamen mit Base64 codieren
176
177 $encoded_name = base64_encode($name);
178 $encoded_name = wordwrap($encoded_name,56,"\015\012",1);
179 $encoded_name = "=?$charset?B?$encoded_name?=";
180 $encoded_name = str_replace("\015\012","?=\015\012 =?$charset?B?",$encoded_name);
181 }
182 else
183 {
184 # Bestimmte Zeichen im Absendernamen codieren
185
186 $encoded_name = str_replace('"','\"',$name);
187 $encoded_name = str_replace('(','\\(',$encoded_name);
188 $encoded_name = str_replace(')','\\)',$encoded_name);
189 $encoded_name = '"'.$encoded_name.'"';
190 }
191
192 $header = "From: $encoded_name <$email>\n";
193 }
194 else $header = "From: $email\n";
195 }
196
197 if($add_header)
198 {
199 if(!$friendly_sender) $header .= "Reply-To: $email\n";
200 $header .= 'Content-Type: text/plain; charset="'.$charset.'"'."\n";
201 $header .= 'Content-Transfer-Encoding: 8bit'."\n";
202 $header .= 'MIME-Version: 1.0'."\n";
203 $header .= 'X-Mailer: Form E-Mail 3.4.1'."\n";
204 $header .= 'X-Sender-IP: ['.$_SERVER['REMOTE_ADDR'].']';
205 }
206 else $header .= "Reply-To: $email";
207
208 $header = preg_replace("/\015\012|\012|\015/","\n",$header);
209
210 # Betreff mit Base64 codieren, wenn er Zeichen enthaelt,
211 # die nicht aus dem ASCII-Zeichensatz stammen
212
213 if(preg_match("/[\200-\377]/",$subject))
214 {
215 $encoded_subject = base64_encode($subject);
216 $encoded_subject = wordwrap($encoded_subject,56,"\015\012",1);
217 $encoded_subject = "=?$charset?B?$encoded_subject?=";
218 $encoded_subject = str_replace("\015\012","?=\015\012 =?$charset?B?",$encoded_subject);
219 }
220 else $encoded_subject = $subject;
221
222 # Mailtext formatieren...
223
224 $mtpl = new Template;
225 $mtpl->read_file($tpl_mail);
226
227 $mtpl->set_var('EMAIL', $email);
228 $mtpl->set_var('NAME', $name);
229 $mtpl->set_var('SUBJECT',$subject);
230 $mtpl->set_var('TIME', $time);
231
232 # ... Umgebungsvariablen einfuegen
233
234 if(isset($allowed_envs) && is_array($allowed_envs))
235 {
236 foreach($allowed_envs as $allowed_env)
237 {
238 if(isset($_SERVER[$allowed_env]))
239 {
240 $mtpl->set_var('ENV_'.$allowed_env,$_SERVER[$allowed_env]);
241 }
242 else
243 {
244 $mtpl->set_var('ENV_'.$allowed_env,'');
245 }
246 }
247 }
248
249 $mtpl->set_var('TEXT',$text);
250
251 $mtpl->parse();
252
253 $mailtext = $mtpl->get_template();
254
255 if($line_break) $mailtext = wordwrap($mailtext,$line_break);
256
257 $mailtext = preg_replace("/\015\012|\012|\015/","\n",$mailtext);
258
259 # ... und die Mail abschicken (bei einem Fehler erscheint eine Meldung)
260
261 if($recipient && is_array($aliases) && $aliases[$recipient])
262 {
263 # Empfaenger mit Alias-Namen
264
265 $mailto = $aliases[$recipient];
266 }
267
268 $mailto = semicolon_split($mailto);
269
270 for($x=0;$x<count($mailto);$x++)
271 {
272 mail($mailto[$x],$encoded_subject,$mailtext,$header) or die($err_send_error);
273 }
274
275 if($captcha_enable) session_destroy();
276
277 # Mail wurde erfolgreich versendet, also wird die Dankesseite angezeigt
278
279 if($return_url)
280 {
281 # Per Redirect auf Seite umleiten
282
283 if(!preg_match('!://!',$return_url))
284 {
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 {
293 # SSL-verschluesseltes HTTP
294
295 $protocol = 'https://';
296 if($_SERVER['SERVER_PORT'] != 443) $port = ':'.$_SERVER['SERVER_PORT'];
297 }
298 else
299 {
300 # Normales HTTP
301
302 $protocol = 'http://';
303 if($_SERVER['SERVER_PORT'] != 80) $port = ':'.$_SERVER['SERVER_PORT'];
304 }
305
306 if(preg_match('!^/!',$return_url))
307 {
308 $return_url = $protocol.$http_host.$port.$return_url;
309 }
310 else
311 {
312 if(preg_match('!/$!',$_SERVER['SCRIPT_NAME'])) $path = substr($_SERVER['SCRIPT_NAME'],0,-1);
313 $path = substr($path,0,strrpos($path,'/')+1);
314 if(!preg_match('!^/!',$path)) $path = '/'.$path;
315
316 $return_url = $protocol.$http_host.$port.$path.$return_url;
317 }
318 }
319
320 header('Status: 303 See Other');
321 header('Location: '.$return_url);
322 }
323 else
324 {
325 # Mit Template formatierte Seite anzeigen
326
327 $stpl = new Template;
328 $stpl->read_file($tpl_sent);
329
330 $stpl->set_var('EMAIL', plain($email));
331 $stpl->set_var('MAIL', plain($mailtext));
332 $stpl->set_var('NAME', plain($name));
333 $stpl->set_var('SUBJECT',plain($subject));
334 $stpl->set_var('TEXT', plain($text));
335 $stpl->set_var('TIME', plain($time));
336
337 $stpl->parse();
338
339 print $stpl->get_template();
340 }
341
342 #
343 ### Ende ###
344
345 ?>

patrick-canterino.de