开发者

What is the problem with this script?

开发者 https://www.devze.com 2023-03-24 22:16 出处:网络
This is the fourth time I\'m asking this query. I have a contact form, that I got from the Internet, with file attachment. It\'s showing no error but when I tried to send mail with a file attached it

This is the fourth time I'm asking this query.

I have a contact form, that I got from the Internet, with file attachment. It's showing no error but when I tried to send mail with a file attached it didn't work. Can you tell me what is the problem or can you suggest a good simple contact form with file attachment? I already tried most contact forms with file attachment on the Internet but the file attachment part in most does not work.

Below is a small part of the HTML code that is part of my script. My site is online and because of this I'm facing problems.

 <form action="" method="post" name="form1" enctype="multipart/form-data">  
 <input name="txtTo" type="text" id="txtTo">
 <input name="txtSubject" type="text" id="txtSubject">
 <textarea name="txtDescription" cols="30" rows="4" id="txtDescription">
 <input name="txtFormName" type="text">
  <input name="txtFormEmail" type="text">
  <input name="fileAttach" type="file">
  <input type="submit" name="Submit" value="Send">
  </form>  

php script

 <?php 
 if(isset($_POST["submit"])){ 
 $strTo = $_POST["txtTo"];  
 $strSubject = $_POST["txtSubject"];  
 $strMessage = nl2br($_POST["txtDescription"]);  

 //*** Uniqid Session ***//  
 $strSid = md5(uniqid(time()));  

 $strHeader = "";  
 $strHeader .= "From: ".$_POST["txtFormName"]."<".$_POST["txtFormEmail"].">\nReply-To:   ".$_POST["txtFormEmail"]."";  

 $strHeader .= "MIME-Version: 1.0\n";  
 $strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";  
 $strHeader .= "This is a multi-part message in MIME format.\n";  

 $strHeader .= "--".$strSid."\n";  
 $strHeader .= "Content-type: text/html; charset=utf-8\n";  
  $strHeader .= "Content-Transfer-Encoding: 7bit\n\n";  
   $strHeader .= $strMessage."\n\n";  

 //*** Attachment ***//  
  if($_FILES["fileAttach"]["name"] != "")  
  {  
  $strFilesName = $_FILES["fileAttach"]["name"];  
  $strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]  ["tmp_name"])));  
  $strHeader .= "--".$strSid."\n";  
  $strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n";  
  $strHeader .= "Content-Transfer-Encoding: base64\n";  
  $strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";  
   $strHeader .= $strContent."\n\n";  
 }  

 $flgSend = @mail($strTo,$strSubject,null,$strHeader);  // @ = No Show Error //  

 if($flgSend)  
 {  
  echo "Mail send completed.";  
 }  
  else  
 {  
 echo "Cannot send mail.";  
 }开发者_Go百科 
 } 
?>


This is my humble answer. I formatted your code a little and created a stand-alone version of it (with some simple styling^^).

Live demo: http://kopli.pri.ee/stackoverflow/6935517.php
(Please don't abuse my little mail-sending service)

In a nutshell it seems, that the $_POST["submit"] was the main issue. However, it is possible, that I fixed some other critical aspect and forgot to note it out.

NOTE: Maybe your script worked in some ways, but your e-mails providers anti-spam systems marked it as spam?! Also, if your page was not correctly encoded, then there might have been conflicts with the UTF-8 format of the email...

I would wish to give some pointers to you:

  • There was a critical problem with $_POST["submit"], no such input in the form.. meaning its not a valid trigger
  • Your <textarea> was not closed and was causing problems.
  • I'm using xhtml in my example, so <input>'s need to be ended with /
  • In <input> for PHP, you don't need have id="txtSubject" (id's are useful when dealing with JS)
  • There is no point of having name="" in <form>
  • There were some weird spaces in your PHP code. Example: $_FILES["fileAttach"] ["tmp_name"]. That's not very correct code!
  • Adding . "" at the end of a string is very much pointless

Full standalone code:

<?php

if (isset($_POST["submit_trigger"])) {
    $strTo = $_POST["txtTo"];
    $strSubject = $_POST["txtSubject"];
    $strMessage = nl2br($_POST["txtDescription"]);

    //*** Uniqid Session ***//
    $strSid = md5(uniqid(time()));

    $strHeader = "";
    $strHeader .= "From: " . $_POST["txtFormName"] . "<" . $_POST["txtFormEmail"] . ">\nReply-To: " . $_POST["txtFormEmail"];

    $strHeader .= "MIME-Version: 1.0\n";
    $strHeader .= "Content-Type: multipart/mixed; boundary=\"" . $strSid . "\"\n\n";
    $strHeader .= "This is a multi-part message in MIME format.\n";

    $strHeader .= "--" . $strSid . "\n";
    $strHeader .= "Content-type: text/html; charset=utf-8\n";
    $strHeader .= "Content-Transfer-Encoding: 7bit\n\n";
    $strHeader .= $strMessage . "\n\n";

    //*** Attachment ***//
    if ($_FILES["fileAttach"]["name"] != "") {
        $strFilesName = $_FILES["fileAttach"]["name"];
        $strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"])));
        $strHeader .= "--" . $strSid . "\n";
        $strHeader .= "Content-Type: application/octet-stream; name=\"" . $strFilesName . "\"\n";
        $strHeader .= "Content-Transfer-Encoding: base64\n";
        $strHeader .= "Content-Disposition: attachment; filename=\"" . $strFilesName . "\"\n\n";
        $strHeader .= $strContent."\n\n";
    }

    // @ = No Show Error //
    $flgSend = @mail($strTo, $strSubject, null, $strHeader);

    if ($flgSend) {
        $posting_message = '<div class="success_message">Mail send completed :)</div>';
    } else {
        $posting_message = '<div class="error_message">Cannot send mail :(</div>';
    }
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title>Can you tell me what is the problem with this script - Kalle H. Väravas</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <style>
        html, body {margin: 0px; padding: 0px; background: #B3D9FF;}
        label {font-weight: bold; width: 140px; display: inline-block; padding: 10px;}
        .success_message,
        .error_message {display: inline-block; padding: 2px 5px; font-weight: bold; margin-bottom: 5px;}
        .success_message {background: #A9F5AB;}
        .error_message {background: #FF8080;}
        #main_container {width: 500px; -moz-border-radius: 5px; background: #FFFFFF; margin: 20px auto; padding: 20px;}
    </style>
</head>
<body>
    <div id="main_container">
        <?php echo $posting_message; ?>
        <form action="" method="post" enctype="multipart/form-data">
            <input name="submit_trigger" value="1" type="hidden" />
            <label>To:</label><input name="txtTo" type="text" /><br />
            <label>Subject:</label><input name="txtSubject" type="text" /><br />
            <label>Message:</label><textarea name="txtDescription" cols="30" rows="4"></textarea><br />
            <label>From name:</label><input name="txtFormName" type="text" /><br />
            <label>From email</label><input name="txtFormEmail" type="text" /><br />
            <label>Attachment:</label><input name="fileAttach" type="file" /><br />
            <input type="submit" name="Submit" value="Send" /><br />
        </form>
    </div>
</body>
</html>


file name = "php_sendmail_upload1"
        <form action="#" method="post" name="form1" class="blocks" enctype="multipart/form-data" class="blocks">
        <p>
            <label>Name</label>
            <input name="txtFormName" class="text" type="text">
        </p>
        <p>
            <label>Email</label>
            <input name="txtFormEmail" type="text" class="text">
        </p>
        <p>
            <label>Position Applying For</label>
            <input type="text" name="txtDescription" id="txtDescription" class="text">
        </p>
        <p class="area">
            <label>Upload CV</label>
            <input name="fileAttach" type="file" >
        </p>
        <p>
            <label>&nbsp;</label>
            <input type="submit" class="submit" name="Submit" value="SEND" />

        </p>
    </form>



    <?
$strTo = "info@mysticsadvertising.com";
$strSubject = $_POST["txtSubject"];
$strMessage = nl2br($_POST["txtDescription"]);

//*** Uniqid Session ***//
$strSid = md5(uniqid(time()));

$strHeader = "";
$strHeader .= "From: ".$_POST["txtFormName"]."<".$_POST["txtFormEmail"].">\nReply-To: ".$_POST["txtFormEmail"]."";

$strHeader .= "MIME-Version: 1.0\n";
$strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";
$strHeader .= "This is a multi-part message in MIME format.\n";

$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-type: text/html; charset=utf-8\n";
$strHeader .= "Content-Transfer-Encoding: 7bit\n\n";
$strHeader .= $strMessage."\n\n";

//*** Attachment ***//
if($_FILES["fileAttach"]["name"] != "")
{
    $strFilesName = $_FILES["fileAttach"]["name"];
    $strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"]))); 
    $strHeader .= "--".$strSid."\n";
    $strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n"; 
    $strHeader .= "Content-Transfer-Encoding: base64\n";
    $strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";
    $strHeader .= $strContent."\n\n";
}


$flgSend = @mail($strTo,$strSubject,null,$strHeader);  // @ = No Show Error //

if($flgSend)
{
    echo "";
}
else
{
    echo "Cannot send mail.";
}
?>  



file name="php_sendmail_upload2"


    <?
$strTo = "info@xxxxxx.com";
$strSubject = $_POST["txtSubject"];
$strMessage = nl2br($_POST["txtDescription"]);

//*** Uniqid Session ***//
$strSid = md5(uniqid(time()));

$strHeader = "";
$strHeader .= "From: ".$_POST["txtFormName"]."<".$_POST["txtFormEmail"].">\nReply-To: ".$_POST["txtFormEmail"]."";

$strHeader .= "MIME-Version: 1.0\n";
$strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";
$strHeader .= "This is a multi-part message in MIME format.\n";

$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-type: text/html; charset=utf-8\n";
$strHeader .= "Content-Transfer-Encoding: 7bit\n\n";
$strHeader .= $strMessage."\n\n";

//*** Attachment ***//
if($_FILES["fileAttach"]["name"] != "")
{
    $strFilesName = $_FILES["fileAttach"]["name"];
    $strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"]))); 
    $strHeader .= "--".$strSid."\n";
    $strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n"; 
    $strHeader .= "Content-Transfer-Encoding: base64\n";
    $strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";
    $strHeader .= $strContent."\n\n";
}


$flgSend = @mail($strTo,$strSubject,null,$strHeader);  // @ = No Show Error //

if($flgSend)
{
    echo "Mail send completed.";
}
else
{
    echo "Cannot send mail.";
}
?>          
0

精彩评论

暂无评论...
验证码 换一张
取 消