Avatar billede proviben Novice
13. juli 2004 - 17:48 Der er 7 kommentarer og
1 løsning

formmail-script, mime-typerne: gif jpg png

Hvorledes kan følgende formmail-script ændres, således at det udelukkende vil acceptere mime-typerne: gif jpg png ?

/Henrik

<?php
// Your e-mail adress:
$mailto = "name@domain.tld";

# Maximum size of attachment in bytes:
$max_attach_size = 500000;

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Formmailer</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
<!--
body                { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; color: #000000; background: #ffff3; margin: 20px; padding: 0px; border: 0px; }
.caution            { color: red; font-weight: bold; }
-->
</style>
</head>
<body><?php
/*if (empty($_POST['form_submitted']))
{
  ?><p>Please fill out the form:</p><?php
}
*/
if (isset($_POST["form_submitted"]))
{
  $name = $_POST['name'];
  $email = $_POST['email'];
  $subject = $_POST['subject'];
  $text = $_POST['text'];

  unset($errors);
  if ($email != "" and !preg_match("/^[^@]+@.+\.\D{2,5}$/", $email)) $errors[] = "e-mail address seems to be invalod";
  if ($text == "") $errors[] = "no message entered";
  if ($_FILES['probe']['size'] > $max_attach_size) $errors[] = "attachment too large (".number_format($_FILES['probe']['size']/1000,0,",","")." KB) - maximum size: ".number_format($max_attach_size/1000,0,",","")." KB";

  if (empty($errors))
  {
    $text = stripslashes($text);
    $subject = stripslashes($subject);
    if ($name != "") $mail_name=$name; else $mail_name="Unknown";
    if ($subject != "") $mail_subject = $subject; else $mail_subject = "No subject";
    if ($email != "") $mail_email = $email; else $mail_email = "email@unknown.xyz";
    $ip = $_SERVER["REMOTE_ADDR"];

    // if attachment, MIME-Mail:
    if (isset($_FILES['probe']['name']) && trim($_FILES['probe']['name']) != "")
    {
      // read and encode file:
      $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:
      $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;
      // End:
      $mail_header .= "\n--".$boundary."--";
      // Sende E-Mail und gebe Fehler bzw. Bestaetigung aus
      if (@mail($mailto,$mail_subject,"",$mail_header)) $sent = true; else $errors[] = "no connection to the mailserver - please try again later";
    }
    // no attachment, normal 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,$mail_header)) $sent = true; else $errors[] = "no connection to the mailserver - please try again later";
    }

    // copy to sender:
    if (isset($sent) && isset($email) && $email != "" && isset($_POST['copy']))
    {
      if (isset($_FILES['probe']['name']) && trim($_FILES['probe']['name']) != "") $copy_mail_text = "Copy of the e-mail:\n\n".$text."\n\nAttachment: ".$_FILES['probe']['name']; else $copy_mail_text = "Copy of the 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);
    }
  }
}

if (empty($sent))
{
  if(isset($errors))
  {
    ?><p class="caution">Error:</p><ul><?php foreach($errors as $f) { ?><li><?php echo $f; ?></li><?php } ?></ul><br /><?php
  }

  ?><form method="post" action="<?php echo basename($_SERVER["PHP_SELF"]); ?>" enctype="multipart/form-data"><div>
  <p><b>Name:</b><br /><input type="text" name="name" value="<?php if (isset($name)) echo htmlentities(stripslashes($name)); else echo ""; ?>" size="35" /></p>
  <p><b>E-mail:</b><br /><input type="text" name="email" value="<?php if (isset($email)) echo htmlentities(stripslashes($email)); else echo ""; ?>" size="35" /></p>
  <p><b>Subject:</b><br /><input type="text" name="subject" value="<?php if (isset($subject)) echo htmlentities(stripslashes($subject)); else echo ""; ?>" size="35" /></p>
  <p><b>Message:</b><br /><textarea name="text" cols="55" rows="12"><?php if (isset($text)) echo htmlentities(stripslashes($text)); else echo ""; ?></textarea></p>
  <b>Attachment:</b><br /><input type="file" name="probe" value="<?php if (isset($_POST['probe'])) echo htmlentities(stripslashes($_POST['probe'])); else echo ""; ?>" size="20"/></p>
  <br /><br />
  <p><input type="submit" name="form_submitted" value="OK - Submit" /> <input type="checkbox" name="copy" value="true" /> Copy to sender</p>
  </div></form><?php
}
else
{
  if (empty($email)) { ?><p><b>Thank you!</b><br />The message has been sent successfully but you didn't specify your e-mail address so I can't reply.</p><?php }
  else { ?><p><b>Thank you!</b><br />The message has been sent successfully.</p><?php }
}

// If you want to remove the Link please donate some Euros:
// http://www.mylittlehomepage.net/donation.html

?><p style="margin-top: 25px; font-size: 11px;">Script by <a class="sln" href="http://www.mylittlehomepage.net/">Alex</a></p>
</body>
</html>
Avatar billede jaw Nybegynder
13. juli 2004 - 17:51 #1
Har ikke læst hele din kode 100% igennem, hvis jeg ikke tager fejl, og at det er: $_FILES['probe']['type'] der kun må være de 3 typer, så kan du:

$filetyper = array("image/gif", "image/jpeg", "image/pjpeg", "image/png");
if (in_array($_FILES['probe']['type'], $filetyper)) {
  // godkendt
} else {
  // ikke godkendt
}
Avatar billede proviben Novice
13. juli 2004 - 18:02 #2
Hvor flettes linierne ind i scriptet?
Avatar billede jaw Nybegynder
13. juli 2004 - 18:07 #3
Det vil jeg nok smide ind sådan:

<?php
// Your e-mail adress:
$mailto = "name@domain.tld";

# Maximum size of attachment in bytes:
$max_attach_size = 500000;
$filetyper = array("image/gif", "image/jpeg", "image/pjpeg", "image/png");

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Formmailer</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
<!--
body                { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; color: #000000; background: #ffff3; margin: 20px; padding: 0px; border: 0px; }
.caution            { color: red; font-weight: bold; }
-->
</style>
</head>
<body><?php
/*if (empty($_POST['form_submitted']))
{
  ?><p>Please fill out the form:</p><?php
}
*/
if (isset($_POST["form_submitted"]))
{
  $name = $_POST['name'];
  $email = $_POST['email'];
  $subject = $_POST['subject'];
  $text = $_POST['text'];

  unset($errors);
  if ($email != "" and !preg_match("/^[^@]+@.+\.\D{2,5}$/", $email)) $errors[] = "e-mail address seems to be invalod";
  if ($text == "") $errors[] = "no message entered";
  if ($_FILES['probe']['size'] > $max_attach_size) $errors[] = "attachment too large (".number_format($_FILES['probe']['size']/1000,0,",","")." KB) - maximum size: ".number_format($max_attach_size/1000,0,",","")." KB";
  if (!in_array($_FILES['probe']['type'], $filetyper)) $errors[] = "filetype not allowed.";
  if (empty($errors))
  {
    $text = stripslashes($text);
    $subject = stripslashes($subject);
    if ($name != "") $mail_name=$name; else $mail_name="Unknown";
    if ($subject != "") $mail_subject = $subject; else $mail_subject = "No subject";
    if ($email != "") $mail_email = $email; else $mail_email = "email@unknown.xyz";
    $ip = $_SERVER["REMOTE_ADDR"];

    // if attachment, MIME-Mail:
    if (isset($_FILES['probe']['name']) && trim($_FILES['probe']['name']) != "")
    {
      // read and encode file:
      $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:
      $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;
      // End:
      $mail_header .= "\n--".$boundary."--";
      // Sende E-Mail und gebe Fehler bzw. Bestaetigung aus
      if (@mail($mailto,$mail_subject,"",$mail_header)) $sent = true; else $errors[] = "no connection to the mailserver - please try again later";
    }
    // no attachment, normal 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,$mail_header)) $sent = true; else $errors[] = "no connection to the mailserver - please try again later";
    }

    // copy to sender:
    if (isset($sent) && isset($email) && $email != "" && isset($_POST['copy']))
    {
      if (isset($_FILES['probe']['name']) && trim($_FILES['probe']['name']) != "") $copy_mail_text = "Copy of the e-mail:\n\n".$text."\n\nAttachment: ".$_FILES['probe']['name']; else $copy_mail_text = "Copy of the 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);
    }
  }
}

if (empty($sent))
{
  if(isset($errors))
  {
    ?><p class="caution">Error:</p><ul><?php foreach($errors as $f) { ?><li><?php echo $f; ?></li><?php } ?></ul><br /><?php
  }

  ?><form method="post" action="<?php echo basename($_SERVER["PHP_SELF"]); ?>" enctype="multipart/form-data"><div>
  <p><b>Name:</b><br /><input type="text" name="name" value="<?php if (isset($name)) echo htmlentities(stripslashes($name)); else echo ""; ?>" size="35" /></p>
  <p><b>E-mail:</b><br /><input type="text" name="email" value="<?php if (isset($email)) echo htmlentities(stripslashes($email)); else echo ""; ?>" size="35" /></p>
  <p><b>Subject:</b><br /><input type="text" name="subject" value="<?php if (isset($subject)) echo htmlentities(stripslashes($subject)); else echo ""; ?>" size="35" /></p>
  <p><b>Message:</b><br /><textarea name="text" cols="55" rows="12"><?php if (isset($text)) echo htmlentities(stripslashes($text)); else echo ""; ?></textarea></p>
  <b>Attachment:</b><br /><input type="file" name="probe" value="<?php if (isset($_POST['probe'])) echo htmlentities(stripslashes($_POST['probe'])); else echo ""; ?>" size="20"/></p>
  <br /><br />
  <p><input type="submit" name="form_submitted" value="OK - Submit" /> <input type="checkbox" name="copy" value="true" /> Copy to sender</p>
  </div></form><?php
}
else
{
  if (empty($email)) { ?><p><b>Thank you!</b><br />The message has been sent successfully but you didn't specify your e-mail address so I can't reply.</p><?php }
  else { ?><p><b>Thank you!</b><br />The message has been sent successfully.</p><?php }
}

// If you want to remove the Link please donate some Euros:
// http://www.mylittlehomepage.net/donation.html

?><p style="margin-top: 25px; font-size: 11px;">Script by <a class="sln" href="http://www.mylittlehomepage.net/">Alex</a></p>
</body>
</html>

Men husk nu at det er på eget ansvar, så en backup er på sin plads først :)
Avatar billede proviben Novice
13. juli 2004 - 18:23 #4
Mange tak jaw, du var hurtig. 100 point er prompte på vej.
Avatar billede jaw Nybegynder
13. juli 2004 - 18:32 #5
Så siger jeg mange tak da :) Virkede det?
Avatar billede proviben Novice
13. juli 2004 - 18:37 #6
Ja, det virker helt efter hensigten.
Avatar billede proviben Novice
13. juli 2004 - 18:42 #7
Er de 100 point i hus?
Avatar billede jaw Nybegynder
13. juli 2004 - 18:47 #8
Jeps, så siger jeg tak for point :D
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Vi tilbyder markedets bedste kurser inden for webudvikling

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester