What are some common methods for allowing website visitors to upload files and send them via email using PHP?

One common method for allowing website visitors to upload files and send them via email using PHP is to use a form on the website where users can upload their files. The PHP script can then process the uploaded file, attach it to an email, and send it to a specified email address.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $file = $_FILES['file']['tmp_name'];
    $file_name = $_FILES['file']['name'];
    $to_email = "recipient@example.com";
    $subject = "File Upload";
    $message = "Please find the attached file.";

    $headers = "From: sender@example.com\r\n";
    $headers .= "Reply-To: sender@example.com\r\n";
    $headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Disposition: attachment; filename=\"" . $file_name . "\"\r\n";

    if (file_exists($file)) {
        $content = file_get_contents($file);
        $success = mail($to_email, $subject, $message, $headers, $content);
        if ($success) {
            echo "File sent successfully.";
        } else {
            echo "Failed to send file.";
        }
    } else {
        echo "File not found.";
    }
}
?>

<form method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit" value="Upload and Send">
</form>