通过电子邮件发送多个附件PHP

时间:2018-12-18 09:06:17

标签: php html

我想通过电子邮件发送多个附件。下面是我的代码

$file = 'C:/Users/pdf/Testing.pdf';

$mailto = 'mail@mail.com';
$subject = 'Subject';
$message = 'My message';

$content = file_get_contents($file);
$content = chunk_split(base64_encode($content));

///我只能将$ content分配给一个文件,而在这里我必须提供多个pdf文件

// a random hash will be necessary to send mixed content
$separator = md5(time());

// carriage return type (RFC)
$eol = PHP_EOL;

// main header (multipart mandatory)
$headers = "From: name <test@test.com>" . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$headers .= "This is a MIME encoded message." . $eol;

// message
$body = "--" . $separator . $eol;
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol;
$body .= $message . $eol;

// attachment
$body .= "--" . $separator . $eol;
$body .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol;
$body .= "Content-Disposition: attachment" . $eol;
$body .= $content . $eol;
$body .= "--" . $separator . "--";

//SEND Mail
if (mail($mailto, $subject, $body, $headers)) {
    echo "mail send ... OK"; // or use booleans here
} else {
    echo "mail send ... ERROR!";
    print_r( error_get_last() );
}

此代码仅向我发送一个文件作为附件,我也必须发送另一文件。

$file2 = 'C:/Users/pdf/sample1.pdf'; // The path for 2nd pdf file

1 个答案:

答案 0 :(得分:2)

请尝试以下代码。您可以创建

之类的附件数组
$attachement = array();
$attachement['data'][0] = 'pdfdata' // Pass PDF content with  base64_encode
$attachement['data'][1] = 'tpPdfdata';

$attachement['name'][0] = 'sample1.pdf';
$attachement['name'][1] = 'sample2.pdf';
enter code here

<?php
    public function send($to, $from, $subject, $message, 
        $cc, $attachement='') {        

        $mail_header = "From: $from\n";
        if (isset($cc)) {
            $mail_header .= "Cc:$cc\n";
        }        

        $mail_header.= "Reply-To: noreply@demo.com\n";
        $mail_header .= "MIME-Version: 1.0";

        // boundary 
        $semi_rand = md5(time());
        $boundary = "==Multipart_Boundary_x{$semi_rand}x";

        // headers for attachment 
        $mail_header .= "\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$boundary}\"";

        // multipart boundary 
        $message = "--{$boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
            "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";

        // preparing attachments
        if (count($attachement) > 0) {
            for ($i = 0; $i < count($attachement); $i++) {
                $message .= "--{$boundary}\n";
                $data = $attachement['data'][$i];
                $message .= "Content-Type: application/octet-stream; name=\"" . $attachement['name'][$i] . "\"\n" .
                    //"Content-Description: ".basename($files[$i])."\n" .
                    "Content-Disposition: attachment;\n" . " filename=\"" . $attachement['name'][$i] . "\"; size=" . filesize($attachement['name'][$i]) . ";\n" .
                    "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
            }
        }
        $message .= "--{$boundary}--";
        return mail($to, $subject, $message, $mail_header);
    }
?>

如果您使用普通的邮件功能,则可以使用上述代码。您可以传递to,from,subject,attachment等参数,请尝试。谢谢