Diesen PHP-Code hatte ich für mein Kontaktformular genutzt. Er sollte einigermaßen Spam-sicher sein. Ich hatte es auf einer anderen Webseite über mehrere Jahre genutzt und keine diesbezüglichen Probleme gehabt. Die Parameter zu Beginn müssen angepasst werden, insbesondere „$to“, welcher den Empfänger definiert, zu welchem die Mail geschickt wird.
<?php
$to = 'Mailadresse von Webmaster';
$lifetime = 86400; // lebensdauer der session, 24 stunden
$min_response_time = 10; // eine nachricht kann nicht in weniger als 10 sekunden geschrieben werden
$max_response_time = 1800; // das schreiben einer nachricht dauert maximal 30 minuten
$max_number_of_attempts = 10; // maximal 10 Versuche in $lifetime
$max_number_of_unfilled_form_attempts = 3; // maximal 3 versuche mit nicht richtig ausgefülltem formular
$stylesheet = 'http://www.no-brain-no-pain.de/static_styles.css';
session_set_cookie_params($lifetime);
session_name('mail');
session_start();
if(!isset($_SESSION['state'])){$_SESSION['state'] = '0';};
if(!isset($_SESSION['start_time'])){$_SESSION['start_time'] = time();};
if(!isset($_SESSION['number_of_attempts'])){$_SESSION['number_of_attempts'] = 0;};
if(!isset($_SESSION['number_of_unfilled_form_attempts'])){$_SESSION['number_of_unfilled_form_attempts'] = 0;};
$response_time = time() - $_SESSION['start_time'];
if($_SESSION['number_of_attempts'] > $max_number_of_attempts){
//error
$text = mail_box_open($stylesheet);
$text .= '<h2>Die maximale Anzahl der Mail-Versuche in den letzten 24h wurde überschritten!</h2>'."\n";
$text = mail_box_close();
echo $text;
exit();
};
$text = mail_box_open($stylesheet);
if($_SESSION['state'] == 0 && !isset($_POST['submit'])){ //mail neu aufgerufen
// send mail form
$_SESSION['start_time'] = time();
$text .= form('','','',false);
$_SESSION['state'] = 1;
}elseif($_SESSION['state'] == 0 && isset($_POST['submit'])){
// error, form abgeschickt und cookie gelöscht...
$_SESSION['start_time'] = time();
$text .= form('','','',false);
$_SESSION['state'] = 1;
}elseif($_SESSION['state'] == 1 && !isset($_POST['submit'])){
// error, wenn fenster nur geschlossen ohne abschicken
$_SESSION['start_time'] = time();
$text .= form('','','',false);
$_SESSION['state'] = 1;
}elseif($_SESSION['state'] == 1 && isset($_POST['submit'])){ //ausgefülltes formular erwartet
if($response_time < $min_response_time){
// error
$text .= '<h2>Die schreiben zu schnell!</h2>'."\n";
$_SESSION['state'] = 0;
}elseif($response_time > $max_response_time){
// error
$text .= '<h2>Die schreiben zu langsam!</h2>'."\n";
$_SESSION['state'] = 0;
}else{
if($_SESSION['number_of_unfilled_form_attempts'] > $max_number_of_unfilled_form_attempts){
//error
$text .= '<h2>Die maximale Anzahl ungültiger Mail-Versuche wurde überschritten!</h2>'."\n";
$_SESSION['number_of_unfilled_form_attempts'] = 0;
$_SESSION['state'] = 0;
}else{
// parameter prüfen und mail senden
$from = 'From: '.trim(strip_tags($_POST['from']));
$subject = trim(strip_tags($_POST['subject']));
$message = trim(strip_tags($_POST['message']));
if($from != '' && $subject != '' && $message != ''){
if(check_mail_address($from)){
if(mail($to,$subject,$message,$from)){
$text .= '<h2>Die folgende Mail wurde verschickt:</h2>'."\n";
$text .= form($from,$subject,$message,true);
$_SESSION['state'] = 0;
}else{
$text .= '<h2>Die Mail konnte nicht verschickt werden!</h2>'."\n";
$_SESSION['state'] = 0;
};
}else{
// error
$_SESSION['start_time'] = time();
$text .= '<h2>Ungültige Mailadresse!</h2>'."\n";
$text .= form($from,$subject,$message,false);
$_SESSION['number_of_unfilled_form_attempts']++;
};
}else{
//mail unvollständig
$_SESSION['start_time'] = time();
$text .= '<h2>Das Formular ist nicht vollständig ausgefüllt!</h2>'."\n";
$text .= form($from,$subject,$message,false);
$_SESSION['number_of_unfilled_form_attempts']++;
};
};
};
$_SESSION['number_of_attempts']++;
};
$text .= mail_box_close();
echo $text;
function mail_box_open($stylesheet){
$text = '<html>'."\n";
$text .= '<head>'."\n";
$text .= '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'" />'."\n";
$text .= '<title>Mail Box</title>'."\n";
$text .= '</head>'."\n";
$text .= '<body id="page">'."\n";
$text .= '<div class="static">'."\n";
$text .= '<div class="box">'."\n";
$text .= '<div class="box_head">Mail an Webmaster</div>'."\n";
$text .= '<div class="box_content">'."\n";
return($text);
}
function mail_box_close(){
$text = '</div>'."\n";
$text .= '</div>'."\n";
$text .= '</div>'."\n";
$text .= '</body>'."\n";
$text .= '</html>'."\n";
return($text);
}
function form($from,$subject,$message,$ro){
if($ro){$ro='readonly="readonly"';}else{$ro='';};
$text = '<form action="'.$_SERVER['REQUEST_URI'].'" method="post">'."\n";
$text .= '<p>Ihre E-Mail-Adresse:<br><input type="text" size="32" maxlength="64" name="from" value="'.$from.'" '.$ro.'></p>'."\n";
$text .= '<p>Betreff:<br><input type="text" size="32" maxlength="64" name="subject" value="'.$subject.'" '.$ro.'></p>'."\n";
$text .= '<p>Nachricht:<br><textarea cols="64" rows="24" name="message" '.$ro.'>'.$message.'</textarea></p>'."\n";
$text .= '<p><input type="submit" name="submit" value="Absenden" '.$ro.'>'."\n";
$text .= '<input type="reset" name="reset" value="Abbrechen" '.$ro.'></p>'."\n";
$text .= '</form>'."\n";
return($text);
}
function check_mail_address($from){
$f = false;
$temp = explode('@',$from);
if(count($temp) === 2){
if(count(explode('.',$temp[1])) === 2){
$f = true;
};
};
return($f);
}
?>