How can a PDF file generated using FPDF be attached to an email?

To attach a PDF file generated using FPDF to an email, you can save the PDF file to a temporary location on the server and then use PHP's `mail()` function to send an email with the PDF file attached as an attachment.

// Generate PDF using FPDF
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output('temp.pdf','F');

// Send email with PDF attachment
$to = 'recipient@example.com';
$subject = 'PDF Attachment';
$message = 'Please find the attached PDF file.';
$from = 'sender@example.com';

$filename = 'temp.pdf';
$file = fopen($filename, 'rb');
$attachment = fread($file, filesize($filename));
fclose($file);

$boundary = md5(time());
$headers = "From: $from\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";

$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($message));
$body .= "--$boundary\r\n";
$body .= "Content-Type: application/pdf; name=$filename\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "Content-Disposition: attachment; filename=$filename\r\n\r\n";
$body .= chunk_split(base64_encode($attachment));
$body .= "--$boundary--";

if (mail($to, $subject, $body, $headers)) {
    echo 'Email sent with PDF attachment.';
} else {
    echo 'Failed to send email.';
}