Send File Attachment from Form Using PHPMailer and PHP
To attach files uploaded from a form using PHPMailer and PHP, follow these steps:
Retrieving the File
At the beginning of your process.php file, include the following code to retrieve the uploaded file:
if (isset($_FILES['uploaded_file']) && $_FILES['uploaded_file']['error'] == UPLOAD_ERR_OK) { $file = $_FILES['uploaded_file']; }
Adding the Attachment to PHPMailer
After you have retrieved the file, add it as an attachment to PHPMailer using the addAttachment() function:
if (isset($file)) { $mail->addAttachment($file['tmp_name'], $file['name']); }
Where:
Example Usage
Putting everything together, your modified code could look like this:
require("phpmailer.php"); $mail = new PHPMailer(); $mail->From = [email protected]; $mail->FromName = My name; $mail->AddAddress([email protected],"John Doe"); $mail->WordWrap = 50; $mail->IsHTML(true); $mail->Subject = "Contact Form Submitted"; $mail->Body = "This is the body of the message."; if (isset($_FILES['uploaded_file']) && $_FILES['uploaded_file']['error'] == UPLOAD_ERR_OK) { $file = $_FILES['uploaded_file']; } if (isset($file)) { $mail->addAttachment($file['tmp_name'], $file['name']); } if (!$mail->Send()) { echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; } else { echo 'Message has been sent.'; }
By following these steps, you can send email attachments using PHPMailer and handle file uploads from a PHP form.
The above is the detailed content of How to Send File Attachments from a PHP Form Using PHPMailer?. For more information, please follow other related articles on the PHP Chinese website!