Attaching Form Uploads to PHPMailer Emails
When you submit a form with enctype="multipart/form-data", the server doesn't just receive the HTML fields; it receives a structured payload containing file data in memory. The critical detail is that PHP stores this uploaded file information in the $_FILES superglobal array. It does not automatically load the file content into a variable you can pass directly to PHPMailer.
To attach the file, you must explicitly instruct PHPMailer to read from that specific source. The AddAttachment() method accepts a path to the file on your server's filesystem. Therefore, your workflow involves two distinct steps: validating the upload and then passing the physical file path to the mailer.
Retrieving the File Path
Before attempting to attach anything, you must verify that the upload actually occurred. If you try to access $_FILES['uploaded_file']['tmp_name'] without checking if a file exists, PHPMailer will fail or send an empty attachment.
Add this validation block at the very top of your process.php, immediately after initializing the script but before creating the PHPMailer instance:
if (!isset($_FILES['uploaded_file'])) {
die("No file uploaded");
}
// Validate that the upload was successful and not empty
if ($_FILES['uploaded_file']['error'] === UPLOAD_ERR_OK) {
// $_FILES['uploaded_file']['tmp_name'] contains the temporary path
$filePath = $_FILES['uploaded_file']['tmp_name'];
// Optional: Verify file extension or size limits here before proceeding
} else {
die("File upload error occurred");
}
This code extracts the temporary filename assigned by PHP during the upload process. This path is unique to that request and will reside in a system-specific temporary directory (usually /tmp on Linux). You do not need to move or rename the file; you simply pass this string directly to the mailer.
Configuring PHPMailer to Attach the File
Once the $filePath variable is populated, you can configure PHPMailer as usual. The placement of AddAttachment() relative to setting the email body does not strictly matter functionally, but logically it often makes sense to define attachments after you have finalized the message content or before sending.
Here is how your full process.php logic should look:
require("phpmailer.php");
$mail = new PHPMailer();
// Set standard headers
$mail->From = 'me@example.com';
$mail->FromName = 'My name';
$mail->AddAddress('me@example.com', 'John Doe');
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "Contact Form Submitted";
$mail->Body = "This is the body of the message.";
// Attach the file retrieved from $_FILES
if (isset($filePath)) {
$mail->AddAttachment($filePath, 'uploaded_file');
}
Important Considerations for Temporary Files
The most significant pitfall in this architecture is the lifecycle of the temporary file. PHPMailer attaches files by reading them from their current location on the disk and streaming the content into the email headers (MIME boundaries). It does not save a copy to your public_html folder or any permanent directory; it reads what is there at that exact moment.
Because $_FILES['uploaded_file']['tmp_name'] points to a temporary location, the file will be automatically deleted by the operating system once the script finishes execution (end of request). Since you explicitly stated that you do not care about saving the file after sending, this behavior is acceptable and actually preferable for security. It prevents sensitive user data from persisting longer than necessary on your server.
However, if you encounter issues where the attachment appears empty or missing, check your PHP upload_max_filesize and post_max_size directives in php.ini. If the file exceeds these limits, PHP will move it to a temporary directory but set the error flag, which you must catch before attempting to attach it.
For complex application structures where you might want to persist files or handle multiple uploads dynamically, frameworks like Laravel provide robust abstractions for this process. You can find more details on handling file uploads in modern stacks at https://laravelcompany.com.
