File attachment with PHPMailer

File attachment with PHPMailer

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

File attachment with PHPMailer

When building a contact form that accepts file uploads, the most common stumbling block occurs when trying to send that uploaded file as an email attachment using PHPMailer. The logic seems sound: capture the file in $_FILES, move it to a temporary directory, and then pass its path to the addAttachment() method. However, simply having the file on disk isn't enough; the data flow between the form submission and the mailer instantiation often breaks down before the email is even sent.

The provided source code demonstrates exactly this scenario. The HTML form correctly uses enctype="multipart/form-data" and names the input field image. On the server side, the script validates the file extension and size, then moves the file to an uploads/ folder. At this specific point, the variable $file_name holds the original filename from the client (e.g., photo.jpg).

The critical failure in the provided snippet lies in how PHPMailer is configured relative to that validation block. Specifically, look at these lines:

$mail->addAttachment("uploads/.$file_name");
$mail->From = $email;

There are two distinct issues here. First, the addAttachment() method expects an absolute path or a correct relative path. If the script runs from a directory other than where the contact.php file resides, "uploads/" might not resolve correctly. A safer approach is to use __DIR__ . '/uploads/' . $file_name to ensure the path is resolved relative to the current script's location, regardless of the server's document root.

Second, and more fundamentally, there is a logic error in how the email sender address is set: $mail->From = $email;. The variable $email contains the visitor's input (e.g., user@example.com). In PHPMailer, setting the From header to the recipient's input field creates a massive security vulnerability known as spoofing. If a user enters badactor@evil.com, your script will send an email from that address, potentially bypassing spam filters or appearing to come from a legitimate source when it is not. The From header must be hardcoded to a verified domain you control (e.g., noreply@yourdomain.com).

Furthermore, the code relies on $_REQUEST to retrieve form data ($name, $phone, etc.). While convenient, $_REQUEST merges $_GET, $_POST, and COOKIE. If an attacker manipulates headers to send GET parameters alongside a POST request with a file, they could potentially bypass your validation logic or inject unintended data. It is strictly better to use $_POST for form submissions involving files.

Finally, the validation logic checks $file_size > 2097152. The comment says "excactly 2 MB", but the comparison uses a greater-than operator. This means files exactly 2MB will pass, while files slightly larger will fail. If the requirement is to strictly reject anything over 2MB, the logic holds. However, if the intent was to enforce a maximum limit of 2MB, the condition should remain as is, but ensure the error message reflects the actual behavior (rejecting > 2MB, not just failing at exactly 2MB).

To fix this implementation, refactor the PHPMailer configuration to use a hardcoded From address and ensure file paths are absolute. Additionally, always set $mail->isHTML(true) when using <br> tags in your email body; otherwise, the line breaks will be stripped by most mail clients. This ensures the "Comments" section renders correctly on Gmail or Outlook.

For developers looking to streamline form handling and security practices further, platforms like https://laravelcompany.com offer insights into building robust applications where separation of concerns prevents these kinds of logic errors from occurring in the first place.

Our network