Guides › mPDF › Email a PDF

Email a PDF

Written for mPDF 8.2 on PHP 8.1+. Updated 2 November 2026.

There’s no need to save a PDF to disk before emailing it. Ask mPDF for the bytes and hand them straight to your mailer.

$mpdf = new \Mpdf\Mpdf(['tempDir' => sys_get_temp_dir() . '/mpdf']);
$mpdf->WriteHTML($html);
$pdf = $mpdf->Output('', \Mpdf\Output\Destination::STRING_RETURN);

$pdf now holds the complete file as a string.

With PHPMailer

use PHPMailer\PHPMailer\PHPMailer;

$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = getenv('SMTP_USER');
$mail->Password = getenv('SMTP_PASSWORD');
$mail->Port = 587;

$mail->setFrom('billing@example.com', 'Northwind Studio');
$mail->addAddress($customerEmail);
$mail->Subject = 'Your invoice INV-2026-041';
$mail->Body = "Hello,\n\nYour invoice is attached.\n\nThanks,\nNorthwind Studio";
$mail->addStringAttachment($pdf, 'INV-2026-041.pdf', PHPMailer::ENCODING_BASE64, 'application/pdf');
$mail->send();

With Symfony Mailer (and Laravel)

use Symfony\Component\Mime\Email;

$email = (new Email())
    ->from('billing@example.com')
    ->to($customerEmail)
    ->subject('Your invoice INV-2026-041')
    ->text("Hello,\n\nYour invoice is attached.")
    ->attach($pdf, 'INV-2026-041.pdf', 'application/pdf');

$mailer->send($email);

In Laravel, a Mailable can do the same with Attachment::fromData(fn () => $pdf, 'INV-2026-041.pdf')->withMime('application/pdf').

Practical advice

  • Generate in a queue job, not during the web request, when you send more than a few. mPDF can take a second or more for a long document, and mail servers are slow too.
  • Mind the size. Many mail servers reject messages over 10–25 MB, and base64 encoding adds about a third. Keep images small (see images), or link to a download instead.
  • Don’t put secrets in the PDF’s metadata, and think twice before emailing sensitive documents. If you must, protect the PDF with a password sent separately (see password protection).
  • Name the attachment usefully. INV-2026-041.pdf beats document.pdf in someone’s downloads folder.