0

I try to send 2 or 3 PDF files generated by FPDF class as attachments within the same email using PHP mail function but every time only one attachment sent. I tried to edit my code many times but it is still not working. I want to know what's the wrong with my code if it is working properly with the first attachment and it is not working with the other attachments?

// attachment
// $files is an array
$separator = md5(time());
$eol = PHP_EOL;
            for ($i = 0; $i < count($files); $i++)
                {
                $filename = $files[$i].".pdf";
                $filepath = 'https://www.mywebsite.com/pdf/index.php?tcode='.$tcode[$i];    

               // to specify the attachment file path to send */
                $content = file_get_contents($filepath);
                $attachment = chunk_split(base64_encode($content));
                $reply_message .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
                $reply_message .= "Content-Transfer-Encoding: base64".$eol;
                $reply_message .= "Content-Disposition: attachment".$eol.$eol;
                $reply_message .= $attachment.$eol;
                $reply_message .= "--".$separator."--";
                }
            mail($email_reply, $reply_subject, $reply_message, $headers1);
Mido
  • 133
  • 1
  • 10
  • Answered here https://stackoverflow.com/questions/22245082/how-to-attach-two-or-multiple-files-and-send-mail-in-php and here https://stackoverflow.com/questions/27757772/sending-multiple-attachment-in-an-email-using-php – Elman Huseynov Apr 09 '20 at 20:05
  • I have seen this question but I found it is not still working with me. I use file_get_contents – Mido Apr 09 '20 at 20:07
  • Many thanks I found the right answer and it is working with me. I've posted it here below – Mido Apr 09 '20 at 20:24

1 Answers1

0

The updated answer

The problem was that I have to Insert the dividing delimiters at the beginning of (inside) the loop, and add a close delimiter after (one outside) the loop.

// attachment
// $files is an array
$separator = md5(time());
$eol = PHP_EOL;
        for ($i = 0; $i < count($files); $i++)
            {
            $filename = $files[$i].".pdf";
            $filepath = 'https://www.mywebsite.com/pdf/index.php?tcode='.$tcode[$i];    

           // to specify the attachment file path to send */
            $content = file_get_contents($filepath);
            $attachment = chunk_split(base64_encode($content));
            $reply_message .= "--".$separator.$eol;                
            $reply_message .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
            $reply_message .= "Content-Transfer-Encoding: base64".$eol;
            $reply_message .= "Content-Disposition: attachment".$eol.$eol;
            $reply_message .= $attachment.$eol;
            }
        $reply_message .= "--".$separator."--";
        mail($email_reply, $reply_subject, $reply_message, $headers1);
Mido
  • 133
  • 1
  • 10