简体   繁体   中英

Can't send attachments with PEAR mail in PHP

I have an HTML form to send mail with attachment from site (at localhost yet) to work mail.
After form was filled and sent $work_mail receives only text part without attachment. And I see no errors reporting. What am I doing wrong?

HTML form:

<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 script:

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");

Minimize your script by removing everything that is not needed to reproduce the error.

Ideas for that:

  • Don't use an uploaded file but send a file that's already on the server
  • Don't use a form at all but a simple small that only has the one task of sending a mail with a hard-coded test text
  • Make sure to read the error information that might get returned

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM