2

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 . '\r\n';
$message = '--'.$semirandom.'\r\n';
$message .= 'Content-type: text/html; charset=iso-8859-1' . '\r\n';
$message .= "\r\n".$msg."\r\n\r\n";
$message .= "--".$semirandom."\r\n";
$message .= "Content-Type: application/octet-stream; name=\"invoice.pdf\"\r\n";
$message .= "Content-Disposition: attachment; filename=\"invoice.pdf\"\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n\r\n";
$message .= chunk_split(base64_encode($document));
$message .= "--".$semirandom."--";

@wp_mail($email, 'My subject', $message, $header);
igogra
  • 415
  • 1
  • 5
  • 18
  • possible duplicate of [Send attachments with PHP Mail()?](http://stackoverflow.com/questions/12301358/send-attachments-with-php-mail) – Gerald Schneider Jul 13 '15 at 08:47

0 Answers0