php html邮件附件为空

时间:2016-02-20 12:38:01

标签: php

我有PHP邮件功能的这个问题: 我试图发送带有附件PDF文件的 html电子邮件,该文件存储在mywebsite的文件夹中,并使用mpdf从我创建,但是当我发送它时,收到的邮件有附件 size 0b

这是代码:

<?
$attachment = "path_to_file_pdf.file.pdf";
if( file_exists($attachment)){
    // File Exists
    $size = filesize($attachment);
    if( $size > 0 ){
        //Alternative 1
            $file = fopen($attachment,'rb');
            $content = fread($file, $size);
            fclose($file);
            $content = chunk_split(base64_encode($content));
        //Alternative 1
            //$content = chunk_split(base64_encode(file_get_contents($attachment)));
        $mailto = "example@maito.com";
        $from_name = "MyDomainName";
        $from_mail = "example@mydomain.com";
        $replyto = "example@mydomain.com";
        $uid = md5(uniqid(time()));
        $subject = "e-mail subject here";
        $message = "HTML MESSAGE HERE" ;
        $filename = "file.pdf";
        $header = "From: ".$from_name." <".$from_mail.">\r\n";
        $header .= "Reply-To: ".$replyto."\r\n";
        $header .= "Content-Type: multipart/mixed; boundary=\"PHP-alt-".$uid."\"\r\n\r\n";
        $header .= "This is a multi-part message in MIME format.\r\n";
        $header .= "--PHP-alt-".$uid."\r\n";
        $header .= "Content-Type: text/html; charset=UTF-8\r\n";
        $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
        $header .= $message."\r\n\r\n";
        $header .= "--PHP-alt-".$uid."\r\n";
        $header .= "Content-Type: application/pdf; name=\"".$filename."\"\r\n";
        $header .= "Content-Transfer-Encoding: base64\r\n";
        $header .= "Content-Disposition: attachment; \r\n\r\n";
        $header .= $content."\r\n\r\n";
        $header .= "--PHP-alt-".$uid."--";
        if( @mail($mailto, $subject, "", $header) ){
            echo "Mail SENT";
        }else{
            echo "ERROR - Mail error";
        }
    }else{
        echo "ERROR- File size = 0";
    }
}else{
    echo "ERROR - File doesn't exist";
}
?>

邮件发送正确,因此文件存在且其大小大于0b。 但是当我在我的电子邮箱中收到电子邮件时,它都是正确的,而不是存在的附件但是空的。 我尝试了两种提取代码中插入的文件内容的替代方法,但结果是一样的。 有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

我更改了一些标题配置。试试这个:

<?
$attachment = "path_to_file_pdf.file.pdf";
if( file_exists($attachment)){
    // File Exists
    $size = filesize($attachment);
    if( $size > 0 ){
        //Alternative 1
            $file = fopen($attachment,'rb');
            $content = fread($file, $size);
            fclose($file);
            $content = chunk_split(base64_encode($content));
        //Alternative 1
            //$content = chunk_split(base64_encode(file_get_contents($attachment)));
        $mailto = "example@maito.com";
        $from_name = "MyDomainName";
        $from_mail = "example@mydomain.com";
        $replyto = "example@mydomain.com";
        $uid = md5(uniqid(time()));
        $subject = "e-mail subject here";
        $message = "HTML MESSAGE HERE" ;
        $filename = "file.pdf";
        $header = "From: ".$from_name." <".$from_mail.">\r\n";
        $header .= "Reply-To: ".$replyto."\r\n";
        $header .= "Content-Type: multipart/mixed; boundary=\"PHP-alt-".$uid."\"\r\n\r\n";
        $header .= "This is a multi-part message in MIME format.\r\n";
        $header .= "--PHP-alt-".$uid."\r\n";
        $header .= "Content-Type: text/html; charset=UTF-8\r\n";
        $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
        $header .= $message."\r\n\r\n";
        $header .= "--PHP-alt-".$uid."\r\n";
        $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
        $header .= "Content-Transfer-Encoding: base64\r\n";
        $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
        $header .= $content."\r\n\r\n";
        $header .= "--PHP-alt-".$uid."--";
        if( @mail($mailto, $subject, "", $header) ){
            echo "Mail SENT";
        }else{
            echo "ERROR - Mail error";
        }
    }else{
        echo "ERROR- File size = 0";
    }
}else{
    echo "ERROR - File doesn't exist";
}
?>