这个脚本有什么问题?
这是我第四次问这个问题。
我有一份从互联网上获得的联系表,其中包含文件附件。它没有显示任何错误,但是当我尝试发送附加文件的邮件时,它不起作用。您能告诉我问题是什么吗?或者您能建议一个带有文件附件的简单联系表格吗?我已经尝试过互联网上大多数带有文件附件的联系表单,但大多数文件附件部分不起作用。
下面是我的脚本中一小部分 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我谦虚的回答。我对您的代码进行了一些格式化,并创建了它的独立版本(带有一些简单的样式^^)。
现场演示:http://kopli.pri.ee/stackoverflow/6935517.php
(请不要滥用我的小邮件发送服务)
简而言之,
$_POST["submit"]
似乎是主要问题。但是,有可能我修复了其他一些关键方面,但忘记将其记下来。注意:也许您的脚本在某些方面有效,但您的电子邮件提供商的反垃圾邮件系统将其标记为垃圾邮件?!另外,如果您的页面编码不正确,则可能与电子邮件的 UTF-8 格式存在冲突...
我希望给您一些提示:
需要以 / 结尾。
中,您不需要
id="txtSubject"
(id 在处理 JS 时很有用)$_FILES["fileAttach"] ["tmp_name"]
。这不是非常正确的代码!完整的独立代码:
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:
<textarea>
was not closed and was causing problems.<input>
's need to be ended with /<input>
for PHP, you don't need haveid="txtSubject"
(id's are useful when dealing with JS)name=""
in<form>
$_FILES["fileAttach"] ["tmp_name"]
. That's not very correct code!. ""
at the end of a string is very much pointlessFull standalone code: