I am trying to generate multi pages on fpdf for each email in the array, but it keeps generating a long page with data for each email and sends that long page to everyone. UPDATE This is my whole code... the first part generates PDF and the second emails them.
<?php
while (ob_get_level())
ob_end_clean();
header("Content-Encoding: None", true);
//Requires here
require('../fpdf/fpdf.php');
require_once '../connect.php';
$db = new PDO('mysql:host=localhost;dbname=mcle','user','pass');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// CREATE AND SAVE THE PDF DOCUMENT
class pdf extends FPDF
{
function header()
{
//$this->image('img/lsnc_logo.jpg',2,2);
$this->SetTextColor(0,0,255);
$this->SetFont('Helvetica','B',16);
$this->SetFillColor('230,230,230');
$this->SetXY(35,15);
$this->Cell(140,10,'Certificate Of Attendance for California
MCLE',1,0,'C', TRUE);
$this->Ln(20);
}
function pdf_body($db)
{
$emails = $_POST['email'];
foreach ($emails as $email)
{
$stmt = $db->query("SELECT *
FROM sessions JOIN attendance
ON sessions.event_id = attendance.event_id JOIN staff
ON attendance.staff_bar_no = staff.staff_bar_no
WHERE staff.email = '$email'");
for($i = 0; $i < count($emails); $i++)
{
while($data = $stmt->fetch(PDO::FETCH_OBJ))
{
$this->Ln(10);
$this->SetX(20);
$this->SetFont('Arial','B',11);
$this->Cell(45,6,'Provider Name:',0,0,'R',0);
$this->SetFont('Times','',11);
$this->Cell(150,6,'Legal Services Of Northern California',0,0,'L');
$this->Ln();
$this->SetX(20);
$this->SetFont('Arial','B',11);
$this->Cell(45,6,'Provider Number:',0,0,'R',0);
$this->SetFont('Times','',11);
$this->Cell(150,6,'2221',0,0,'L');
$this->Ln();
$this->SetX(20);
$this->SetFont('Arial','B',11);
$this->Cell(45,6,'Title Of Activity:',0,0,'R',0);
$this->SetFont('Times','',11);
$this->Cell(150,6,$data->pro_title,0,0,'L');
$this->Ln();
$this->SetX(20);
$this->SetFont('Arial','B',11);
$this->Cell(45,6,'Date Of Activity:',0,0,'R',0);
$this->SetFont('Times','',11);
$this->Cell(65,6,$data->date,0,0,'L');
$this->Ln();
$this->SetX(20);
$this->SetFont('Arial','B',11);
$this->Cell(45,6,'Time of Activity:',0,0,'R',0);
$this->SetFont('Times','',11);
$this->Cell(65,6,$data->time,0,0,'L');
$this->Ln();
$this->SetX(20);
$this->SetFont('Arial','B',11);
$this->Cell(45,6,'Location (City, State):',0,0,'R',0);
$this->SetFont('Times','',11);
$this->Cell(150,6,$data->location,0,0,'L');
$this->Ln();
$this->Ln(10);
$this->SetFont('Arial','B',12);
$this->Cell(65,6,'Eligible California MCLE Credit','',0);
$this->Ln(10);
$this->SetFont('Times','',12);
$this->Cell(109,6,'Total California MCLE Credit Hours for the above
activity are:',0,0,'R',0);
$this->SetFont('Times','B',12);
$this->Cell(4,6,$data->length,0,0,'R');
$this->SetFont('Times','',12);
$this->Cell(80,6,', including the following sub-field
credits:',0,0,'L',0);
$this->Ln(5);
$this->SetFont('Arial','',12);
$this->Cell(45,6,'* Legal Ethics: ',0,0,'R',0);
$this->SetFont('Arial','B',12);
$this->Cell(45,6,$data->length,0,0,'L');
$this->Ln();
$this->SetFont('Arial','',12);
$this->Cell(57,6,'* Elimination of Bias: ',0,0,'R',0);
$this->SetFont('Arial','B',12);
$this->Cell(45,6,$data->bias,0,0,'L');
$this->Ln();
$this->SetFont('Arial','',12);
$this->Cell(174,6,'* Substance Abuse / Mental or Physical Issues Affecting
Professional Competence: ',0,0,'R',0);
$this->SetFont('Arial','B',12);
$this->Cell(10,6,$data->sampiapc,0,0,'L');
$this->SetFont('Arial','',12);
$this->Ln(15);
$this->SetFont('Times','B',11);
$this->MultiCell(174,6,'THE BOTTOM PORTION OF THIS FORM IS TO BE COMPLETED
BY THE ATTORNEY AFTER PARTICIPATION IN THE ABOVE-REFERENCED ACTIVITY');
$this->Ln(12);
// Split Here
$this->SetFont('Times','I',12);
$this->SetX(20);
$this->MultiCell(170, 6, '' . str_repeat('By signing below, I certify that
I participated in all or some* of the activity described above and am
therefore entitled to the following MCLE credit hours -', 1),'LRT');
$this->SetFont('Times','',12);
$this->SetX(20);
$this->Cell(170,6,'Total California MCLE Credit Hours ______, including
the following sub-field credits:','LR');
$this->Ln();
$this->SetX(20);
$this->SetFont('Times','',12);
$this->Cell(170,6,' Legal Ethics:______','LR');
$this->Ln();
$this->SetX(20);
$this->SetFont('Times','',12);
$this->Cell(170,6,' Elimination of Bias:______','LR');
$this->Ln();
$this->SetX(20);
$this->SetFont('Times','',12);
$this->Cell(170,6,' Substance Abuse / Mental or Physical Issues
Affecting Professional Competence:____ ','LR');
$this->Ln();
$this->SetFont('Times','I',12);
$this->SetX(20);
$this->Cell(170,6,' ','LR');
$this->Ln();
$this->SetX(20);
$this->MultiCell(170,6,'*Partial participation hours must be pro-rated.
You may not claim credit for sub-fields unless the Provider is granting
credit in those areas and you participated in those portions of the
activity ','LR');
$this->SetX(20);
$this->Cell(170,6,' ','LR');
$this->Ln();
//
$this->SetX(20);
$this->SetFont('Times','',12);
$this->Cell(33,6,'Print Your Name:',0,'L');
$this->SetFont('Arial','B',12);
$this->Cell(80,6,$data->fname." ".$data->lname ,0,0,'L');
$this->SetFont('Arial','',12);
$this->Cell(28,6,'State Bar No: ',0,'R');
$this->SetFont('Arial','B',12);
$this->Cell(29,6,$data->staff_bar_no,0,0,'L');
$this->SetX(20);
$this->Cell(170,10,' ','LR');
//
$this->Ln();
$this->SetX(20);
$this->SetFont('Times','',12);
$this->Cell(170,6,'Your Signature:_____________________________ ','LR');
$this->SetX(20);
$this->Cell(170,6,' ','LR');
$this->Ln();
$this->SetX(20);
$this->Cell(170,6,' ','LRB');
$this->Ln(19);
$this->SetFont('Times','B',10);
$this->SetX(20);
$this->Cell(25,4,'Reminders: ',0,0,'L',0);
$this->Ln();
$this->SetX(20);
$this->SetFont('Times','',10);
$this->MultiCell(170,4,'1- Keep this record of attendance for 4 years. In
the event that you are audited by the State Bar, you may be requested to
submit this record of attendance. Send this to the State Bar only if you
are audited');
$this->SetX(20);
$this->MultiCell(170,4,'2- You must personally sign-in on the Official
Record of Attendance for California MCLE or complete a contemporaneous
registered login online for web events maintained by this provider in
order to qualify for California MCLE credits.');
}
}
}
}
function footer()
{
}
////////////////////////// Make sure we don't detete this below
}
//////////////////////////
$pdf = new pdf();
$pdf->AddPage('P','A4',0);
$pdf->pdf_body($db);
//$pdf->Output();
//Mailing Parts here
$emails = $_POST['email']; //This is mainly to declare $email
foreach ($emails as $email)
{
//
}
// email stuff (change data below)
$to = $email;
$from = "noreply@domain";
$subject = "Your LSNC MCLE Attendance Certificate";
$message = "<p>Hello,</p> <p>Here is your LSNC MCLE Attendance
Certificate. Please Confirm . Thank you !</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 = "MCLE.pdf";
// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output('', 'S');
$attachment = chunk_split(base64_encode($pdfdoc));
// main header
$headers = "From: ".$from.$eol;
$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."--";
mail($to, $subject, $body, $headers) ;
unset($pdf); // THIS will now ONLY send the first email with other
//generated pages...
?>
<script>
alert('Emails Successfully Sent !');
history.back();
</script>
<?php
?>
I hope to find a solution for this, I know I am missing on the first loop, I couldn't loop through elements of the array (I think). once I get that right, the email part will work fine as I have tested it on individual records... Many Thanks @Dave.
You have a foreach
going through $_POST['multi1']
and a similar looking foreach
going through $_POST['email']
so it isn't clear which of those variables is actually coming from your form (which isn't shown).
I'm going to assume, for this answer, that $_POST['multi1']
is the correct variable to be using. If it isn't adjust the code as needed. You'll notice that the PDF gets built for each e-mail address, sent and then the PDF object is destroyed before going on to the next e-mail address.
// this stuff only needs to be set once
$subject = 'Your LSNC MCLE Attendance Certificate';
$message = '<p>Hello,</p><p>Here is your LSNC MCLE Attendance Certificate. Please Confirm . Thank you!</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 = "MCLE.pdf";
if (isset($_POST['submit'])) {
foreach($_POST['multi1'] as $email) {
$to = $email;
$stmt = $db->query("SELECT * FROM sessions
JOIN attendance ON sessions.event_id = attendance.event_id
JOIN staff ON attendance.staff_bar_no = staff.staff_bar_no
WHERE staff.email = '$email'");
while ($data = $stmt->fetch(PDO::FETCH_OBJ)) {
//building pdf here
}
$pdf = new pdf();
$pdf->AddPage('P','A4',0);
$pdf->pdf_body($db);
// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));
// main header
$headers = 'From: noreply@emailaddress.domain' . $eol;
$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="MCLE.pdf"' . $eol;
$body .= 'Content-Transfer-Encoding: base64' . $eol;
$body .= 'Content-Disposition: attachment' . $eol . $eol;
$body .= $attachment . $eol;
$body .= '--' . $separator . '--';
mail($to, $subject, $body, $headers) ;
unset($pdf); // destroy PDF object
} // end of foreach through $_POST['multi1']
} // end of isset($_POST['submit'])