2011-08-04 161 views
-6

這是我第四次問這個查詢。這個腳本有什麼問題?

我有一個聯繫表格,我從互聯網上獲得的文件附件。它沒有顯示任何錯誤,但是當我嘗試使用附加的文件發送郵件時,它不起作用。你能告訴我什麼是問題,或者你可以建議一個好的簡單聯繫表格與文件附件?我已經在互聯網上嘗試了大多數帶有文件附件的聯繫表格,但大多數文件附件都不起作用。

以下是屬於腳本的HTML代碼的一小部分。我的網站在線,因此我面臨問題。

<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腳本

<?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."; 
} 
} 
?> 
+8

爲什麼你不乾脆僱人來爲你做的? – zerkms

+1

你錯過了一些關鍵的步驟。你需要做更多的研究。你快到了。看看 http://www.tizag.com/phpT/fileupload。php和 http://php.net/move_uploaded_file – micha

+0

感謝micha爲你的出色答覆 – test

回答

1

這是我謙虛的回答。我將你的代碼格式化了一些,並創建了它的獨立版本(有一些簡單的樣式^^)。

現場演示:http://kopli.pri.ee/stackoverflow/6935517.php
(請不要濫用我的小郵件發送服務)

在這似乎是一個簡單地說,該$_POST["submit"]是主要問題。但是,我可能會修正其他一些關鍵方面,並忘記記下它。

注意:也許您的腳本在某些方面有效,但您的電子郵件提供商反垃圾郵件系統將其標記爲垃圾郵件?此外,如果你的頁面中的編碼不正確,則可能已經與電子郵件的UTF-8格式的衝突......

我想給一些指點您:

  • 有一個與$ _ POST一個關鍵問題的形式[「提交」],沒有這樣的輸入..這意味着它不是一個有效的觸發
  • <textarea>沒有關閉,並造成問題。
  • 我使用XHTML在我的例子,所以<input>的需要與/
  • 結束在<input>爲PHP,你不需要有id="txtSubject"
  • 有(ID與JS打交道時非常有用)沒有在<form>
  • name=""有在你的PHP代碼中的一些怪異的空間點。例如:$_FILES["fileAttach"] ["tmp_name"]。這不是很正確的代碼!
  • 在字符串的結尾添加. ""是非常沒有意義的

完全獨立的代碼:

<?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> 
0
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 = "[email protected]"; 
$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 = "[email protected]"; 
$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

你能解釋你的改變嗎? – Korcholis