How to accept attachment using form and mail in php

To accept an attachment using a form and mail in PHP, you can use the following steps:

Step 1: Create a form with an input field for the attachment

In your HTML form, add an input field with the type attribute set to file. This will allow users to select a file to upload.

<form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file" name="attachment" />
    <input type="submit" value="Send" />
</form>

Step 2: Create a PHP script to handle the form submission

Create a PHP script (e.g., upload.php) that will handle the form submission. This script will receive the uploaded file and send it via email using the mail() function.

<?php
// Check if the form has been submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get the uploaded file
    $attachment = $_FILES["attachment"];

    // Check if the file was uploaded successfully
    if ($attachment["error"] == 0) {
        // Get the file name and extension
        $filename = $attachment["name"];
        $filetype = $attachment["type"];
        $filesize = $attachment["size"];

        // Check the file type and size
        if (in_array($filetype, array("application/pdf", "application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"))) {
            if ($filesize <= 1024 * 1024 * 5) { // 5MB limit
                // Send the file via email
                $to = "[email protected]";
                $subject = "File Attachment";
                $message = "File attached: ". $filename;

                // Use the mail() function to send the email
                $headers = "From: [email protected]\r\n";
                $headers.= "Content-Type: text/plain; charset=UTF-8\r\n";
                $headers.= "Content-Disposition: attachment; filename=\"". $filename. "\"\r\n";
                $headers.= "Content-Transfer-Encoding: base64\r\n";

                $mail = mail($to, $subject, $message, $headers, $attachment["tmp_name"]);

                if ($mail) {
                    echo "File sent successfully!";
                } else {
                    echo "Error sending file.";
                }
            } else {
                echo "File size exceeds the limit.";
            }
        } else {
            echo "Invalid file type.";
        }
    } else {
        echo "Error uploading file.";
    }
}
?>

Step 3: Send the email using the mail() function

In the PHP script, use the mail() function to send the email with the attached file. The mail() function takes the following parameters:

In this example, we're using the mail() function to send a plain text email with the attached file. We're setting the Content-Type header to text/plain and the Content-Disposition header to attachment to indicate that the file is an attachment.

Note: Make sure to replace the [email protected] and [email protected] email addresses with your actual email addresses.

That's it! With these steps, you should be able to accept an attachment using a form and mail it using PHP.