附件不适用于邮件功能wordpress

时间:2016-10-20 05:26:22

标签: php wordpress email

文本邮件工作正常,但我也想附加文件。 附件不起作用。 我的代码是:

 <form method="post" enctype="multipart/form-data">
     <input name="filepc" type="file" id="filepc" class="listEm" />
 </form>


if (isset($_POST['submit'])) {
  $attachments = '';
    if (!empty($_FILES['filepc']['tmp_name'])) {
        $attachments = array($_FILES['filepc']['name']);

    }

headers = "From:" . $your_email . " < " . $email . ">" . "\r\n";
        $headers .= "Reply - To:" . $ct_email . "\r\n";
        $headers .= "Content-Disposition: attachment; filename=\"".$attachments."\"\r\n\r\n";
        //$headers = "Bcc: someone@domain . com" . "\r\n";
        $headers = "X - Mailer: PHP / " . phpversion();
        mail($to , $subject , $msg , $headers,$attachments);
$successMsg = '<h5 style="color:red;">Sent successfully</h5>';
//        }

}

以上代码有效但文件未附加。请帮我找到解决这个问题的方法。

1 个答案:

答案 0 :(得分:1)

if(!empty($_FILES['resume']['name'])) {
    $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
    $tmp_name = $_FILES['resume']['tmp_name'];
    $type = $_FILES['resume']['type'];
    $file_name = $_FILES['resume']['name'];
    $size = $_FILES['resume']['size'];

    // Check to make sure that it is an uploaded file and not a system file
    if(is_uploaded_file($tmp_name)){
        // Now Open the file for a binary read
        $file = fopen($tmp_name,'rb');
        // Now read the file content into a variable
        $data = fread($file,filesize($tmp_name));
        // close the file
        fclose($file);
        // Now we need to encode it and split it into acceptable length lines
        $data = chunk_split(base64_encode($data));
    }
    $mybody = "This is a multi-part message in MIME format.\n\n" .
         "--{$mime_boundary}\n" .
         "Content-Type: text/html; charset=\"iso-8859-1\"\n" .
         "Content-Transfer-Encoding: 7bit\n\n" .
    $mybody . "\n\n";

    $headers = "From: $from\r\n" .
         "MIME-Version: 1.0\r\n" .
         "Content-Type: multipart/mixed;\r\n" .
         " boundary=\"{$mime_boundary}\"";

    $mybody .= "--{$mime_boundary}\n" .
         "Content-Type: {$type};\n" .
         " name=\"{$file_name}\"\n" .
         //"Content-Disposition: attachment;\n" .
         //" filename=\"{$fileatt_name}\"\n" .
         "Content-Transfer-Encoding: base64\n\n" .
        $data . "\n\n" .
        "--{$mime_boundary}--\n";

    $bodys .= "$mybody <br>";
    $subject = "Attachment";
    $body = $body . $bodys;
    mail($to, $subject, $body, $headers);
}