Can';t在PHP中使用PEAR邮件发送附件
我有一个HTML表单,可以将带有附件的邮件从站点(本地主机)发送到工作邮件。Can';t在PHP中使用PEAR邮件发送附件,php,email,attachment,pear,Php,Email,Attachment,Pear,我有一个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
填写并发送$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");
功能邮件发送至($mail,$sbj,$body){
全局$server、$work\u-mail、$acc、$pass;
@包括_once'Mail.php';
@包括“Mail/mime.php”;
$headers['From']=$mail;
$headers['To']=$work\u mail;
$headers['Subject']='.$sbj中的“[来自站点的邮件];
$headers['Content-type']=“text/html;charset=windows-1251”;
$headers['MIME-Version']=“1.0”;
$smtpinfo[“主机”]=$server;
$smtpinfo[“端口”]=“25”;
$smtpinfo[“auth”]=true;
$smtpinfo[“用户名”]=$acc;
$smtpinfo[“密码”]=$pass;
//使用mail::factory方法创建邮件对象
$msg=新邮件\u mime(“\r\n”);
$msg->setTXTBody($body);
$body=$msg->get(数组('html\u charset'=>'windows-1251','text\u charset'=>'windows-1251','head\u charset'=>'windows-1251');
$headers=$msg->headers($headers);
如果(isset($_文件[“att”])){
移动上传的文件($\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\;
$msg->addAttachment(../tmp/“$\u文件[“att”][“name”],“应用程序/八位字节流”);
}
@$mail_object=&mail::factory(“smtp”、$smtpinfo);
@$send=$mail\u object->send($headers['To'],$headers,$body);
if(PEAR::isError($send))返回false//{
//echo(“”$send->getMessage()””;
//}其他{
//回显(“发送错误消息!”);
// }
返回true;
}
$lng=(isset($\u GET[“lng”])?$\u GET[“lng”]:false;
$loc=“位置:../cont.php?”($lng?”lng=$lng&“:”);
如果(isset($_POST[“who”])和&isset($_POST[“mail”])和&isset($_POST[“txt”])){
if(mail_to($_POST[“mail”],$_POST[“who”],$_POST[“txt”])标题($loc.“i=2”);
else标题($loc.“i=3”);
}
else标题($loc.“i=1”);
通过删除重现错误不需要的所有内容来最小化脚本
这方面的想法:
- 不要使用上载的文件,而是发送服务器上已有的文件
- 不要使用表单,只使用一个简单的小表单,它只有一个任务,就是发送带有硬编码测试文本的邮件
- 确保读取可能返回的错误信息
var\u dump(文件存在(“../tmp/”$\u文件[“att”][“name”])
的输出是什么(在$msg->addAttachment(…)
行之后添加此内容),然后重试。是的,文件实际上已上载:die(var\u dump(文件存在(“../tmp/”$\u文件[“att”][“name”)))代码>显示bool(true)
,我在tmp
目录中看到它。哦。。。在我的脚本中有一个非常愚蠢的错误(为什么我有一个月没有看到它?),这是在代码缩减过程中根据您的建议发现的。在附加文件之前,我正在使用$msg->get()
。在移动if(isset($\u FILES[“att”]){}
子句之后,$body=$msg->get()
一切正常。谢谢