<?php require("PHPMailer/PHPMailerAutoload.php");
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $to = $_POST['to'];
    $subject = $_POST['subject'];
    $body = $_POST['body'];
    $attachment = isset($_FILES['attachment']) ? $_FILES['attachment']['tmp_name'] : null;

    $headers = "From: sender@example.com";

    if ($attachment) {
        $file_content = file_get_contents($attachment);
        $file_name = $_FILES['attachment']['name'];
        $file_type = $_FILES['attachment']['type'];
        $boundary = md5(time());

        $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"$boundary\"";
        $body .= "\r\n--$boundary\r\n";
        $body .= "Content-Type: $file_type; name=\"$file_name\"\r\n";
        $body .= "Content-Transfer-Encoding: base64\r\n";
        $body .= "Content-Disposition: attachment; filename=\"$file_name\"\r\n\r\n";
        $body .= chunk_split(base64_encode($file_content));
        $body .= "\r\n--$boundary--";
    }

    if (mail($to, $subject, $body, $headers)) {
        echo 'Email sent successfully';
    } else {
        echo 'Failed to send email';
    }
} else {
    echo 'Invalid request method';
}
?>