Send Attachments and Inline Images with PHPMailer

Last updated: August 28, 2026.

PHPMailer can attach files from disk and embed images referenced by cid URLs. Resolve paths on the server instead of accepting arbitrary filenames from the browser.

Attach a PDF and embed a logo

<?php
require __DIR__ . '/vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
$mail = new PHPMailer(true);
// Configure SMTP, sender, and recipient first.
$mail->addAttachment(__DIR__ . '/exports/invoice-1042.pdf', 'invoice-1042.pdf');
$mail->addEmbeddedImage(__DIR__ . '/assets/logo.png', 'company-logo', 'logo.png');
$mail->isHTML(true);
$mail->Subject = 'Invoice 1042';
$mail->Body = '<p><img src="cid:company-logo" alt="Example Company"></p><p>Your invoice is attached.</p>';
$mail->AltBody = 'Your invoice is attached.';
$mail->send();

Control attachment cost

  • Check file size before attaching.
  • Prefer protected links for large or sensitive files.
  • Use server-resolved paths only.
  • Delete generated temporary files after sending.

Reference: PHPMailer file upload example.

admin

admin