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

时间:2016-03-10 14:42:40

标签: php email attachment mime

enter image description here

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

    $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);

1 个答案:

答案 0 :(得分:2)

<?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));
    }

}

希望这会有所帮助。不应该需要太多的解释,因为它几乎是你所拥有的,只是更清洁,更容易维护。