Please check my PHP mail attachment program. its only sending the attachment not the mail body.

I have tried to open the attachment but the attached files is corrupted.

Full Code with JQuery http://pastebin.com/T0WtnxbT

PHP mail Attachment Script

<?php 
error_reporting(E_ALL ^ E_NOTICE); // hide all basic notices from PHP

//If the form is submitted
if(isset($_POST['submitted'])) {

    // require a name from user
    if(trim($_POST['contactName']) === '') {
        $nameError =  'Forgot your name!'; 
        $hasError = true;
    } else {
        $name = trim($_POST['contactName']);
    }

    // need valid email
    if(trim($_POST['email']) === '')  {
        $emailError = 'Forgot to enter in your e-mail address.';
        $hasError = true;
    } else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email']))) {
        $emailError = 'You entered an invalid email address.';
        $hasError = true;
    } else {
        $email = trim($_POST['email']);
    }

    // we need at least some content
    if(trim($_POST['comments']) === '') {
        $commentError = 'You forgot to enter a message!';
        $hasError = true;
    } else {
        if(function_exists('stripslashes')) {
            $comments = stripslashes(trim($_POST['comments']));
        } else {
            $comments = trim($_POST['comments']);
        }
    }

    // upon no failure errors let's email now!
    if(!isset($hasError)) {

            $email_from = $_POST['email']; // required
            $telephone = $_POST['senderPhone']; // not required
            $company = $_POST['senderCompany'];
            if(($selected_key==0))

            echo "Please enter your title";
            function clean_string($string) {
                    $bad = array("content-type","bcc:","to:","cc:","href");
                    return str_replace($bad,"",$string);
            }

            $email_message = "";
            $email_message .= "Name: ".$name."\n";
            $email_message .= "Company: ".$name."\n";
            $email_message .= "Email: ".$email."\n";
            $email_message .= "Telephone: ".clean_string($telephone)."\n";
            $email_message .= "Comments: ".$comments."\n";

            $email_to = "sashi@gmx.com"; // The email you are sending to (example)
            //$email_from = "sendfrom@email.com"; // The email you are sending from (example)
            $email_subject = 'Submitted message from '.$name; // The Subject of the email
            //$email_txt = "text body of message"; // Message that the email has in it

            $destination=$_FILES["file"]["name"];
            move_uploaded_file($_FILES["file"]["name"], $destination);
            $fileatt = fopen($_FILES['file']['name'],'r'); // Path to the file (example)
            $fileatt_type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; // File Type
            $fileatt_name = "Details.docx"; // Filename that will be used for the file as the attachment
            $file = fopen($fileatt,'r');
            $data = fread($file,filesize($fileatt));
            fclose($file);

            $semi_rand = md5(time());
            $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
            $headers="From: $email_from"; // Who the email is from (example)
            $headers .= "\nMIME-Version: 1.0\n" .
            "Content-Type: multipart/mixed;\n" .
            " boundary=\"{$mime_boundary}\"";
            $email_message .= "This is a multi-part message in MIME format.\n\n" .
            "--{$mime_boundary}\n" .
            "Content-Type:text/html; charset=\"iso-8859-1\"\n" .
            "Content-Transfer-Encoding: 7bit\n\n" . $email_txt;
            $email_message .= "\n\n";
            $data = chunk_split(base64_encode($data));
            $email_message .= "--{$mime_boundary}\n" .
            "Content-Type: {$fileatt_type};\n" .
            "name=\"{$fileatt_name}\"\n" .
            "Content-Transfer-Encoding: base64\n\n" .
            $data . "\n\n" .
            "--{$mime_boundary}--\n";

            mail($email_to,$email_subject,$email_message,$headers); 
            $emailSent = true;
}
}
?>

This example may give you some clue

<?php

$to = "abc@abc.com";
$subject = "test message";
$email = "xyz@xyz.com" ;
$message = " Hello this is a test message ";
$headers = "From:$email";

$sent = mail($to, $subject, $message, $headers) ;
if($sent)
{echo "Your mail was sent successfully delivered "; }
else
{echo "Encountered an error sending automatic mail p"; }


?>

This is not the Answer

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.