附件问题使用php mail()函数

时间:2016-05-20 06:28:19

标签: php wordpress

我正在使用此代码发送混合内容(带有html +附件的文本):

function multi_attach_mail($to, $subject, $message1, $senderMail, $senderName, $files){

    $from = $senderName." <".$senderMail.">"; 
    $headers = "From: $from";

    $semi_rand = md5(time()); 
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

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

    $message1 = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
    "Content-Transfer-Encoding: 7bit\n\n" . $message1 . "\n\n"; 


    $message1 .= "--{$mime_boundary}\n";
    $fp =    @fopen($files,"rb");
    $data =  @fread($fp,filesize($files));
    @fclose($fp);
    $data = chunk_split(base64_encode($data));
    $message1 .= "Content-Type: application/octet-stream; name=\"".basename($files)."\"\n" . 
    "Content-Description: ".basename($files)."\n" .
    "Content-Disposition: attachment;\n" . " filename=\"".basename($files)."\"; size=".filesize($files).";\n" . 
    "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
    $message1 .= "--{$mime_boundary}--";
    $returnpath = "-f" . $senderMail;

    $mail = mail($to, $subject, $message1, $headers, $returnpath); 

    if($mail){ return TRUE; } else { return FALSE; }
}

它成功发送电子邮件,附加文件,但附件内容不可用,一旦下载,就说文件可能已损坏。我想我的标题有问题,但无法准确理解问题是什么。

我正在使用wordpress,此功能位于主题文件夹的functions.php文件中。

0 个答案:

没有答案