无法在PHP中使用PEAR邮件发送附件

时间:2013-11-08 13:16:34

标签: php email attachment pear

我有一个HTML表单,用于发送带有附件的邮件(在localhost上)到工作邮件 表单填写后发送$ 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");

1 个答案:

答案 0 :(得分:0)

通过删除重现错误所不需要的所有内容来最小化脚本。

想法:

  • 请勿使用已上传的文件,而是发送已在服务器上的文件
  • 根本不要使用表单,而是使用简单的小表单,只有一个任务就是发送带有硬编码测试文本的邮件
  • 确保阅读可能会返回的错误信息
相关问题