<?php
session_start();
?>
<?php
# Adresse, an die die Formulardaten gesendet werden sollen:
$mailto = "simon.joss@gmail.com";
# Maximale Größe des Attachments in Bytes:
$max_attach_size = 1500000;
?>






<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<STYLE>
h1 { color: #036;  font-family: Verdana; font-size: 20; }
BODY 		  { font-family: Verdana,Arial,Helvetica,Geneva,Sans-serif,sans-serif; font-size: 10pt; color: rgb(51,51,51); font-weight: normal; font-style: normal; background-color: rgb(255,255,255);}
.caution      { color:red; font-weight:bold; }
.input01 	  { font-family:Arial; color:#000000; font-size:12px; border: 1px solid #036; height:22px; width:300px; padding:3px; }
.input02	  { font-family:Arial; color:#000000; font-size:12px; border: 1px solid #036; height:100px; width:300px; padding:3px; }
.input03	  { font-family:Arial; color:#000000; font-size:16px; border: 1px solid #036; height:27px; padding:3px; }
.input04 	  { font-family: Verdana,Arial,Helvetica,Geneva,Sans-serif,sans-serif; font-size: 10pt; color: rgb(51,51,51); font-weight: normal; font-style: normal; border: 1px solid #036; height:22px; width:300px; padding:2px; }
img.captcha   { margin:0px; padding:0px; border:1px solid #036; }
input.captcha { margin:0px; padding:0px; width:100px; height:23px; font-size:16px; }
td			  { font-family: Verdana,Arial,Helvetica,Geneva,Sans-serif,sans-serif; font-size: 10pt; color: rgb(51,51,51); font-weight: normal; font-style: normal; background-color: rgb(255,255,255); }
</STYLE>

</HEAD>
<BODY>
<h1>Nachricht an Michelle</h1>




<?php
if (isset($_POST["form_submitted"]))
 {
// übergebene Variablen ermitteln:
  $name = $_POST['name'];
  $email = $_POST['email'];
  $subject = $_POST['subject'];
  $text = $_POST['text'];
  $telefon = $_POST['telefon'];
  $rassehund = $_POST['rassehund'];
  $namehund = $_POST['namehund'];

  // Überprüfungen der Daten:
  # Hier werden die zu erfüllenden Bedingungen überprüft. Nur wernn das Array $errors keinen Eintrag hat, wird die Mail versendet
  unset($errors);
  if(!isset($_SESSION['captcha'])) $errors[] = 'Session error';
  if ($email != "" and !preg_match("/^[^@]+@.+\.\D{2,5}$/", $email)) $errors[] = "Die E-Mail-Adresse sieht nicht richtig aus.";
  if ($email == "") $errors[] = "Es wurde keine E-Mail Adresse eingegeben.";
  if ($name == "") $errors[] = "Es wurde kein Name eingegeben.";
  if ($telefon == "") $errors[] = "Es wurde keine Telefonnummer eingegeben.";
  if ($nachricht == "") $errors[] = "Es wurde keine Nachricht eingegeben.";
  if ($_FILES['probe']['size'] > $max_attach_size) $errors[] = "Attachment zu groß (".number_format($_FILES['probe']['size']/1000,0,",","")." KB) - Maximalgröße: ".number_format($max_attach_size/1000,0,",","")." KB";
  if($_POST['captcha_word'] != $_SESSION['captcha']) $errors[] = 'Sicherheitscode ist falsch.';

  if (empty($errors))
   {
    $text = stripslashes($text);
    $subject = stripslashes($subject);
	$email = stripslashes($email);
	$telefon = stripslashes($telefon);
	$namehund = stripslashes($namehund);
	$rassehund = stripslashes($rassehund);
	$nachricht = stripslashes($nachricht);
    if ($name != "") $mail_name=$name; else $mail_name="unbekannt";
    if ($subject != "") $mail_subject = $subject; else $mail_subject = "kein Betreff";
    if ($email != "") $mail_email = $email; else $mail_email = "email@unknown.xyz";
	if ($name != "") $text = "Name: ".$name."\n"; else $text = "";
	if ($email != "") $text = "Mail: ".$email."\n"; else $text = "";
    if ($telefon != "") $text = "Tel.: ".$telefon."\n\n"; else $text = "";
	if ($namehund != "") $text = "Name des Hundes: ".$namehund."\n"; else $text = "Name des Hundes: ".$namehund."\n";
	if ($rassehund != "") $text = "Hunderasse: ".$rassehund."\n\n"; else $text = "Hunderasse: ".$rassehund."\n\n";
	if ($nachricht != "") $text = "Nachricht: ".$nachricht."\n"; else $text = "Nachricht: ".$nachricht."\n";


    $ip = $_SERVER["REMOTE_ADDR"];

    // Wenn Attachment, dann MIME-Mail erstellen:
    if (isset($_FILES['probe']['name']) && trim($_FILES['probe']['name']) != "")
     {
      // Datei einlesen und codieren:
      $datei_content = fread(fopen($_FILES['probe']['tmp_name'],"r"),filesize($_FILES['probe']['tmp_name']));
      $datei_content = chunk_split(base64_encode($datei_content),76,"\n");

      // Boundary festlegen:
      $boundary = md5(uniqid(rand()));

      // Mail-Header:
      $mail_header = "From: ".$mail_name." <".$mail_email.">\n";
      $mail_header .= "X-Sender-IP: ".$ip."\n";
      $mail_header .= "MIME-Version: 1.0\n";
      $mail_header .= "Content-Type: multipart/mixed; boundary=\"".$boundary."\"\n";
      $mail_header .= "This is a multi-part message in MIME format.\n";
      // Mail-Text:
      $mail_header .= "--".$boundary;
      $mail_header .= "\nContent-Type: text/plain";
      $mail_header .= "\nContent-Transfer-Encoding: 8bit";
      $mail_header .= "\n\n".$text;
      // Attachment:
      $mail_header .= "\n--".$boundary;
      $mail_header .= "\nContent-Type: ".$_FILES['probe']['type']."; name=\"".$_FILES['probe']['name']."\"";
      $mail_header .= "\nContent-Transfer-Encoding: base64";
      $mail_header .= "\nContent-Disposition: attachment; filename=\"".$_FILES['probe']['name']."\"";
      $mail_header .= "\n\n".$datei_content;

      // Ende:
      $mail_header .= "\n--".$boundary."--";
      // Sende E-Mail und gebe Fehler bzw. Bestaetigung aus
      if (@mail($mailto,$mail_subject,"",$mail_header)) $sent = true; else $errors[] = "Keine Verbindung zum Mailserver - bitte nochmal versuchen.";
     }
    // kein Attachment, normale E-Mail:
    else
     {
      $mail_header = "From: ".$mail_name." <".$mail_email.">\n";
      $mail_header .= "X-Sender-IP: $ip\n";
      $mail_header .= "Content-Type: text/plain";
      if (@mail($mailto,$mail_subject, $text,$telefon,$rassehund,$mail_header)) $sent = true; else $errors[] = "Keine Verbindung zum Mailserver - bitte nochmal versuchen.";
     }

    // Kopie an Absender:
    if (isset($sent) && isset($email) && $email != "" && isset($_POST['copy']))
     {
      if (isset($_FILES['probe']['name']) && trim($_FILES['probe']['name']) != "") $copy_mail_text = "Kopie der versendeten E-Mail:\n\n".$text."\n\nAttachment: ".$_FILES['probe']['name']; else $copy_mail_text = "Kopie der versendeten E-Mail:\n\n".$text;
      $header= "From: ".$mailto."\n";
      $header .= "X-Sender-IP: ".$ip."\n";
      $header .= "Content-Type: text/plain";
      @mail($email, $mail_subject, $copy_mail_text, $header);
     }
 #   session_destroy();
   }
 }

if (empty($sent))
 {
  if(isset($errors))
   {
    ?><table><tr><td valign="top" width="150"><p class="caution">Fehler:</p></td><td colspan="3"><?php foreach($errors as $f) { ?>- <?php echo $f; ?><br><?php } ?></td></tr></table><br><br><?php
   }
   

   $letters="abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ123456789";
   mt_srand((double)microtime()*1000000);
   $word='';
   for($i=0;$i<5;$i++)
    {
     $word.=substr($letters,mt_rand(0,strlen($letters)-1),1);
    }
   $_SESSION['captcha'] = $word;

?>
<form method="post" action="<?php echo basename($_SERVER["PHP_SELF"]); ?>" enctype="multipart/form-data"><div>



<table>
<tr><td width="150"><p>Name:</td><td colspan="3"><input class="input01" type="text" name="name" value="<?php if (isset($name)) echo htmlentities(stripslashes($name)); else echo ""; ?>" size="35" /></p></td></tr>
<tr><td><p>E-Mail:</td><td colspan="3"><input class="input01" type="text" name="email" value="<?php if (isset($email)) echo htmlentities(stripslashes($email)); else echo ""; ?>" size="35" /></p></td></tr>
<tr><td><p>Telefon:</td><td colspan="3"><input class="input01" type="text" name="telefon" value="<?php if (isset($telefon)) echo htmlentities(stripslashes($telefon)); else echo ""; ?>" size="35" /></p></td></tr>
<tr><td height="15"></td><td colspan="3"></td></tr>
<tr><td><p>Name des Hundes:</td><td colspan="3"><input class="input01" type="text" name="namehund" value="<?php if (isset($namehund)) echo htmlentities(stripslashes($namehund)); else echo ""; ?>" size="35" /></p></td></tr>
<tr><td><p>Rasse:</td><td colspan="3"><input class="input01" type="text" name="rassehund" value="<?php if (isset($rassehund)) echo htmlentities(stripslashes($rassehund)); else echo ""; ?>" size="35" /></p></td></tr>
<tr><td height="15"></td><td colspan="3"></td></tr>
<tr><td><p>Betreff:</td><td colspan="3"><input class="input01" type="text" name="subject" value="<?php if (isset($subject)) echo htmlentities(stripslashes($subject)); else echo ""; ?>" size="35" /></p></td></tr>
<tr><td><p>Nachricht:</td><td colspan="3"><textarea class="input02" name="nachricht" cols="44" rows="6"><?php if (isset($nachricht)) echo htmlentities(stripslashes($nachricht)); else echo ""; ?></textarea></p></td></tr>
<tr><td height="15"></td><td colspan="3"></td></tr>
<tr><td><p>Sicherheitscode:</td><td><img border="1" class="captcha" src="formmail_img/image.php" ></td><td><center>Code:</center></td><td align="right"><input class="input03" type="text" name="captcha_word" value="" size="10" /></p></td></tr>
<tr><td height="15"></td><td colspan="3"></td></tr>
<tr><td><p>E-Mail senden:</p></td><td colspan="3"><input class="input04" type="submit" name="form_submitted" value="Abschicken" /></td></tr>
</table>
<br>
<p>

  
  </div></form><?php
 }
else
 {
  if (empty($email)) { ?><p><b>Danke!</b><br />Nachricht erfolgreich versendet.</p><?php }
  else { ?><p><b>Danke!</b><br />Nachricht erfolgreich versendet.</p><?php }
 }
?>




</BODY>
</HTML>
 