0

How can I dynamically send multiple FPDF's via mail in PHP ? I am a new user. The scripts work fine individually. mail.php also works when I attach one pdf. But I need to attach 2 of them to the same mail. Could someone please help me find a way around? Thank you.

I have atttached one of the created pdf's. There's another one with the same code snippets. I have also attached the script I used to send the email.

createpdf.php

<?php
$email_id = $_GET['e'];
$connect=mysqli_connect("localhost", "user", 
"password","db") or die("Connection failed1:".mysqli_connect_error());
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',12);
$email_id = $email_id;
$query = $connect->query("SELECT first_name FROM `db` WHERE email_id = '$email_id'");
$array = Array();
while($result = $query->fetch_assoc()){
    $pdf->SetFont('Arial','B',12);  
$pdf->Cell(40,10,'Dear ' .$result['first_name']. ', This is the 1st doc.');
}

$pdf->Output();

?>

mail.php

    <?php

    require('createpdf.php');
    $to = $_GET['e']; 
    $subject = "test"; 
    $message = "<p>Please see the attachment.</p>";

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

    // carriage return type (we use a PHP end of line constant)
    $eol = PHP_EOL;

    // attachment name
    $filename = "test.pdf";

    // encode data (puts attachment in proper format)
    $pdfdoc = $pdf->Output("", "S");
    $attachment = chunk_split(base64_encode($pdfdoc));

     // main header
    $headers  = "From: X < noreply@test.com >\n";
    $headers .= "MIME-Version: 1.0".$eol; 
    $headers .= "Content-Type: multipart/mixed; 
    boundary=\"".$separator."\"";

    // no more headers after this, we start the body! //

    $body = "--".$separator.$eol;
    $body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
    $body .= "This is a MIME encoded message.".$eol;

     // message
    $body .= "--".$separator.$eol;
    $body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
    $body .= "Content-Transfer-Encoding: 8bit".$eol.$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.$eol;
    $body .= $attachment.$eol;
    $body .= "--".$separator."--";

    // send message
    mail($to, $subject, $body, $headers);

    ?>

0 Answers0