这个脚本有什么问题?

发布于 2024-11-27 17:04:49 字数 2265 浏览 0 评论 0原文

这是我第四次问这个问题。

我有一份从互联网上获得的联系表,其中包含文件附件。它没有显示任何错误,但是当我尝试发送附加文件的邮件时,它不起作用。您能告诉我问题是什么吗?或者您能建议一个带有文件附件的简单联系表格吗?我已经尝试过互联网上大多数带有文件附件的联系表单,但大多数文件附件部分不起作用。

下面是我的脚本中一小部分 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.";  
 } 
 } 
?>

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可爱咩 2024-12-04 17:04:49

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

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

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

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

我希望给您一些提示:

  • 存在一个严重问题使用 $_POST["submit"],表单中没有此类输入..这意味着它不是有效的触发器
  • 您的
  • 我在示例中使用的是 xhtml,因此 需要以 / 结尾。
  • 在 PHP 的 中,您不需要id="txtSubject" (id 在处理 JS 时很有用)

  • 中使用 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>

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>
帥小哥 2024-12-04 17:04:49
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> </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.";
}
?>          
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> </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.";
}
?>          
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文