I have a wordpress site where I generate an invoice pdf by means of FPDF. I want to attach it to an email but I don’t know how to do it in WordPress. When the pdf is generated, it is returned as a string but it is not possible to attach it using the wp_mail function.
This is my current code, which attaches a noname file (a corrupt one).
// Generating the invoice PDF on the fly
...
$pdf = new PDF();
...
$document = $pdf->Output('', 'S'); // return the document as a string
// Attaching the PDF to an email
$msg = 'My message';
$semirandom = md5(time());
$header = 'Content-Type: multipart/mixed; boundary=' . $semirandom . 'rn';
$message = '--'.$semirandom.'rn';
$message .= 'Content-type: text/html; charset=iso-8859-1' . 'rn';
$message .= "rn".$msg."rnrn";
$message .= "--".$semirandom."rn";
$message .= "Content-Type: application/octet-stream; name="invoice.pdf"rn";
$message .= "Content-Disposition: attachment; filename="invoice.pdf"rn";
$message .= "Content-Transfer-Encoding: base64rnrn";
$message .= chunk_split(base64_encode($document));
$message .= "--".$semirandom."--";
@wp_mail($email, 'My subject', $message, $header);