Base 64 Attachment in PHP Mail() not working

孤者浪人 提交于 2021-01-28 21:25:44

问题


I got a script which sends out an automated email when a function runs. I want to be able to send the HTML email along with a PDF attachment. I know I need to encode the file in to Base64 however I am just getting base64 code attached to the bottom of my email. I assume it's something to do with the mime stuff. Anyone see the issue?

    $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:


<?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

The function I made to send cleaner mail

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

}

Hopefully this helps. Shouldn't require too much explanation as it's pretty much what you have, just cleaner and easier to maintain.



来源:https://stackoverflow.com/questions/35919576/base-64-attachment-in-php-mail-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!