PHP Mail()中的Base 64附件不起作用 [英] Base 64 Attachment in PHP Mail() not working

查看:61
本文介绍了PHP Mail()中的Base 64附件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

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

}

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

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

这篇关于PHP Mail()中的Base 64附件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆