简体   繁体   中英

Send multiple attachments with an email PHP

I want to send multiple attachments with an email. The below is my code

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

// I can give $content to only one file and I have to give multiple pdf files here

// 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() );
}

This code send me only one files as an attachment and I have to send the other file as well.

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

Please try below code. You can create attachment array like

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

If you using normal mail function you can achieve using above code.You can pass argument like to, from, subject,attachment etc... Please try. Thank you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM