Home > Backend Development > PHP Tutorial > How to Send File Attachments via Email Using PHP and PHPMailer?

How to Send File Attachments via Email Using PHP and PHPMailer?

Barbara Streisand
Release: 2024-12-07 18:49:12
Original
874 people have browsed it

How to Send File Attachments via Email Using PHP and PHPMailer?

Sending File Attachments from a Form Using PHP and PHPMailer

In your process.php file, you can attach the uploaded file to the email using the following steps:

  1. Retrieve the uploaded file:
if (isset($_FILES['uploaded_file']) && $_FILES['uploaded_file']['error'] == UPLOAD_ERR_OK) {
    $uploadInfo = $_FILES['uploaded_file'];
}
Copy after login
  1. Attach the file to the email:
if (isset($uploadInfo)) {
    $mail->addAttachment($uploadInfo['tmp_name'], $uploadInfo['name']);
}
Copy after login

This code checks if the file was uploaded successfully, then attaches the file to the email using the addAttachment method of PHPMailer. The arguments to addAttachment are the temporary file name of the uploaded file and the original filename, respectively.

Additional Notes:

  • Make sure your form has the enctype="multipart/form-data" attribute set to allow file uploads.
  • The MAX_FILE_SIZE input hidden field sets the maximum file size allowed for uploads.
  • You can pass additional parameters to addAttachment to specify the file encoding, MIME type, and disposition.
  • The file will be deleted from the temporary directory after the email is sent.

The above is the detailed content of How to Send File Attachments via Email Using PHP and PHPMailer?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template