1

I'm trying to send more than one pdf and I didn't found any that works for me. I tried this How to attach two or multiple files and send mail in PHP

My problem is that only one pdf is attached. Here is my code.

public function sendMultiple($from,$to,$subject,$files){
    $eol = PHP_EOL;
    $separator = md5(time());

    $headers  = "From: \"".$from."\" <".$this->replyTo.">\r\n";
    $headers .= "MIME-Version: 1.0".$eol;
    $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;

    $body = "Content-Transfer-Encoding: 7bit".$eol;
    $body .= "This is a MIME encoded message.".$eol;
    $body .= "--".$separator.$eol;
    $body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
    $body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
    $body .= $this->getMessage().$eol;
    foreach ($files as $file ) {
        $body .= "--" . $separator . $eol;
        $body .= "Content-Type: application/octet-stream; name=\"" . $file['name'] . "\"" . $eol;
        $body .= "Content-Transfer-Encoding: base64" . $eol;
        $body .= "Content-Disposition: attachment" . $eol . $eol;
        $body .= $file['file'] . $eol;
        $body .= "--" . $separator . "--";
    }
    $mail = mail($to,$subject,$body,$headers);
    if($mail){
        return true;
    }
    return null;
}

//$files is an array of this
$file1 = array("name"=>"name".$name1.".pdf","file"=>chunk_split(base64_encode($pdf)));
$file2 = array("name"=>"name".$name2.".pdf","file"=>chunk_split(base64_encode($pdf2)));
array_push($files, $file1);
array_push($files, $file2);
0m3r
  • 12,286
  • 15
  • 35
  • 71
kibusan
  • 31
  • 4

1 Answers1

1

I solved the problem, here is the code:

public function sendMultiple($from,$to,$subject,$files){
    $eol = PHP_EOL;
    $separator = md5(time());

    $headers  = "From: \"".$from."\" <".$this->replyTo.">\r\n";
    $headers .= "MIME-Version: 1.0".$eol;
    $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;

    $body = "Content-Transfer-Encoding: 7bit".$eol;
    $body .= "This is a MIME encoded message.".$eol;
    $body .= "--".$separator.$eol;
    $body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
    $body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
    $body .= $this->getMessage().$eol;
    foreach ($files as $file ) {
        $body .= "--" . $separator . $eol;
        $body .= "Content-Type: application/octet-stream; name=\"" . $file['name'] . "\"" . $eol;
        $body .= "Content-Transfer-Encoding: base64" . $eol;
        $body .= "Content-Disposition: attachment" . $eol . $eol;
        $body .= $file['file'] . $eol;
        //This line is not needed, solved the issue
        //$body .= "--" . $separator . "--";
    }
    $mail = mail($to,$subject,$body,$headers);
    if($mail){
        return true;
    }
    return null;
}

//$files is an array of this
$file1 = array("name"=>"name".$name1.".pdf","file"=>chunk_split(base64_encode($pdf)));
$file2 = array("name"=>"name".$name2.".pdf","file"=>chunk_split(base64_encode($pdf2)));
array_push($files, $file1);
array_push($files, $file2);
kibusan
  • 31
  • 4