PHP Mail() 中的 Base 64 附件不工作

Base 64 Attachment in PHP Mail() not working

我有一个脚本可以在函数运行时自动发送电子邮件。我希望能够发送 HTML 电子邮件和 PDF 附件。我知道我需要将文件编码为 Base64,但是我只是将 base64 代码附加到我的电子邮件底部。我认为这与 mime 相关。有人看到这个问题了吗?

    $to = 'example@example.com';

    $subject = 'test!';

    $file = file_get_contents("files/CAPS-Standing-Order.pdf");
    $encoded_file = chunk_split(base64_encode($file));

    // message
    $boundary = md5("sanwebe");

    $message = 'Hello';

    // To send HTML mail, the Content-type header must be set
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    // Additional headers
    $headers .= 'From: CAPS Consortium <contact@capsconsortium.com>' . "\r\n";

    $message .= "--$boundary\r\n";
    $message .="Content-Type: pdf; name=\"CAPS-Standing-Order.pdf\"\r\n";
    $message .="Content-Disposition: attachment; filename=\"CAPS-Standing-Order.pdf\"\r\n";
    $message .="Content-Transfer-Encoding: base64\r\n";
    $message .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
    $message .= $encoded_file; 

    // Mail it
    mail($to, $subject, $message, $headers);
<?php

$file = file_get_contents("files/CAPS-Standing-Order.pdf");
$encoded_file = chunk_split(base64_encode($file));

$attachments[] = array(
    'name' => 'CAPS-Standing-Order.pdf', // Set File Name
    'data' => $encoded_file, // File Data
    'type' => 'application/pdf', // Type
    'encoding' => 'base64' // Content-Transfer-Encoding
);

$this->sendMail("example@example.com", "Hello", "test!", $attachments); 
// Send the actual mail and include the attachments

我做的发送更干净邮件的功能

<?php
function sendMail($email = "", $text = "", $subject = "", $attachments = array()) {
    if(!$email || !$text) {
        return false;
    }

    $headers   = array();
    $headers[] = "To: {$email}";
    $headers[] = "From: CAPS Consortium <contact@capsconsortium.com>";
    $headers[] = "Reply-To: CAPS Consortium <contact@capsconsortium.com>";
    $headers[] = "Subject: {$subject}";
    $headers[] = "X-Mailer: PHP/".phpversion();

    $headers[] = "MIME-Version: 1.0";

    if(!empty($attachments)) {
        $boundary = md5(time());
        $headers[] = "Content-type: multipart/mixed;boundary=\"".$boundary."\"";
        // Have attachment, different content type and boundary required.
    } else {
        $headers[] = "Content-type: text/html; charset=UTF-8";
    }

    $html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
            <title>CAPS Consortium</title>
            <style>table { border-collapse:collapse; mso-table-lspace:0pt; mso-table-rspace:0pt; }</style>
        </head>
        <body style="font-family: arial;" width="100%">
            [text]
        </body>
    </html>';

    $generated = date('jS M Y H:i:s');
    $subject = ($subject ? $subject : 'Default Subject');
    $message = $html;

    $message = str_replace("[text]", $text, $message);

    if(!empty($attachments)) {
        $output   = array();
        $output[] = "--".$boundary;
        $output[] = "Content-type: text/html; charset=\"utf-8\"";
        $output[] = "Content-Transfer-Encoding: 8bit";
        $output[] = "";
        $output[] = $message;
        $output[] = "";
        foreach($attachments as $attachment) {
            $output[] = "--".$boundary;
            $output[] = "Content-Type: ".$attachment['type']."; name=\"".$attachment['name']."\";";
            if(isset($attachment['encoding'])) {
                $output[] = "Content-Transfer-Encoding: " . $attachment['encoding'];
            }
            $output[] = "Content-Disposition: attachment;";
            $output[] = "";
            $output[] = $attachment['data'];
            $output[] = "";
        }
        return mail($email, $subject, implode("\r\n", $output), implode("\r\n", $headers));
    } else {
        return mail($email, $subject, $message, implode("\r\n", $headers));
    }

}

希望这对您有所帮助。不需要太多解释,因为它几乎就是您所拥有的,只是更干净、更易于维护。