这个脚本有什么问题?

时间:2011-08-04 02:25:08

标签: php forms file attachment sendmail

这是我第四次询问此查询。

我有一个联系表格,我从互联网上获得了文件附件。它显示没有错误,但当我尝试发送附带文件的邮件时,它无法正常工作。你能告诉我这是什么问题,或者你能建议一个简单的文件附件联系表吗?我已经在互联网上尝试了大多数带有文件附件的联系表格,但大多数文件附件部分都不起作用。

以下是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.";  
 } 
 } 
?>

2 个答案:

答案 0 :(得分:1)

这是我的谦虚回答。我稍微格式化了你的代码并创建了它的独立版本(带有一些简单的样式^^)。

现场演示:http://kopli.pri.ee/stackoverflow/6935517.php
(请不要滥用我的小邮件发送服务)

简而言之,似乎$_POST["submit"]是主要问题。但是,有可能,我修复了一些其他关键方面而忘记注意它。

注意:也许您的脚本在某些方面有效,但您的电子邮件提供商反垃圾邮件系统将其标记为垃圾邮件?!此外,如果您的页面编码不正确,则可能与电子邮件的UTF-8格式存在冲突......

我希望能给你一些指示:

  • $ _POST [“submit”]存在严重问题,表单中没有此类输入..意味着它不是有效的触发器
  • 您的<textarea>未关闭,导致问题。
  • 我在我的示例中使用了xhtml,因此<input>需要以/
  • 结束
  • <input> for PHP中,您不需要id="txtSubject"(在处理JS时id非常有用)
  • name=""
  • 中没有<form>
  • 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>

答案 1 :(得分: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 = "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.";
}
?>