我有一个 HTML 表单,可以将带有附件的邮件从站点(在本地主机上)发送到工作邮件。
填写并发送表格后,$work_mail 只接收没有附件的文本部分。而且我没有看到任何错误报告。我究竟做错了什么?
HTML 表单:
<form action="parts/sendmail.php" method="post" enctype="multipart/form-data">
<table cellspacing="2" cellpadding="2" width="80%" border="0" class="table" align="center">
<tr><td class="tq" colspan="2">
Send mail from site
<?php
if(isset($_GET["i"])) {
echo "<br /><span style=\"font-weight: 900; color: #a00;\">";
switch($_GET["i"]) {
case 1:
echo "(Fill all fields please)";
break;
case 2:
echo "(Mail sent successfully)";
break;
case 3:
echo "(Send mail error)";
break;
}
echo "</span>";
}
?>
</td></tr>
<tr><td width="180" class="tq">Your name:</td><td class="ta"><input type="text" name="who" /></td></tr>
<tr><td width="180" class="tq">Contact E-mail:</td><td class="ta"><input type="text" name="mail" /></td></tr>
<tr><td width="180" class="tq">Text:</td><td class="ta"><textarea style="height: 130px;" name="txt"></textarea></td></tr>
<tr><td width="180" class="tq">Attach file:</td><td class="ta"><input type="file" name="att" /></td></tr>
<tr><td class="tq" colspan="2" style="text-align: right;">
<button>Send</button>
</td></tr>
</table>
</form>
PHP 脚本:
function mail_to($mail,$sbj,$body) {
global $server,$work_mail,$acc,$pass;
@include_once 'Mail.php';
@include_once 'Mail/mime.php';
$headers['From'] = $mail;
$headers['To'] = $work_mail;
$headers['Subject'] = '[Mail from site] from '.$sbj;
$headers['Content-type'] = "text/html; charset=windows-1251";
$headers['MIME-Version'] = "1.0";
$smtpinfo["host"] = $server;
$smtpinfo["port"] = "25";
$smtpinfo["auth"] = true;
$smtpinfo["username"] = $acc;
$smtpinfo["password"] = $pass;
// Create the mail object using the Mail::factory method
$msg=new Mail_mime("\r\n");
$msg->setTXTBody($body);
$body=$msg->get(array('html_charset'=>'windows-1251','text_charset'=>'windows-1251','head_charset'=>'windows-1251'));
$headers=$msg->headers($headers);
if(isset($_FILES["att"])) {
move_uploaded_file($_FILES["att"]["tmp_name"],"../tmp/".$_FILES["att"]["name"]);
$msg->addAttachment("../tmp/".$_FILES["att"]["name"],'application/octet-stream');
}
@$mail_object =& Mail::factory("smtp", $smtpinfo);
@$send=$mail_object->send($headers['To'], $headers, $body);
if (PEAR::isError($send)) return false;//{
// echo("<p>" . $send->getMessage() . "</p>");
// } else {
// echo("Error message sent!");
// }
return true;
}
$lng=(isset($_GET["lng"]))?$_GET["lng"]:false;
$loc="location: ../cont.php?".($lng?"lng=$lng&":"");
if(isset($_POST["who"])&&isset($_POST["mail"])&&isset($_POST["txt"])) {
if(mail_to($_POST["mail"],$_POST["who"],$_POST["txt"])) header($loc."i=2");
else header($loc."i=3");
}
else header($loc."i=1");