【问题标题】:php html mail with attachment emptyphp html邮件,附件为空
【发布时间】:2016-02-20 12:38:00
【问题描述】:

我的 PHP 邮件功能有这个问题: 我正在尝试发送带有 PDF 文件的 html 电子邮件附件,该文件存储在 mywebsite 的文件夹中,并使用 mpdf 从我创建,但是当我发送它时,收到的邮件带有附件尺寸为 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。 但是当我在我的邮箱中收到电子邮件时,它都是正确的,而不是存在但为空的附件。 我尝试了两种提取代码中插入的文件内容的替代方法,但结果是一样的。 有人可以帮助我吗?

【问题讨论】:

    标签: php


    【解决方案1】:

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

    <?
    $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";
    }
    ?>
    

    【讨论】:

      【解决方案2】:

      看到我没有收到答案,我只找到了一种没有问题的方法,这种方法是使用 PHPMailar 类。 感谢讨论: Send attachments with PHP Mail()?

      【讨论】:

        猜你喜欢
        • 2011-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-20
        • 2019-05-11
        • 2012-06-08
        • 1970-01-01
        相关资源
        最近更新 更多