完成上传附件之前发送的PHP邮件

I am making a PHP form which allow users to upload attachment and send it to my e-mail. I have been searching for quite a long time to make it. And, finally, I found this. http://www.shotdev.com/php/php-mail/php-send-email-upload-form-attachment-file/. It works fine. However, when I modify it myself (change the fields), something doesn't go well.

<?php

$location=$_POST['location'];
$name_ha=$_POST['name_ha'];
$name_person=$_POST['name_person'];
$email=$_POST['email'];
$date_sent=$_POST['date_sent'];
$date_completed=$_POST['date_completed'];
$date_received=$_POST['date_received'];

$to="admin@admin.com" . "$email";

$message="to";

//*** Uniqid Session ***//
$sid = md5(uniqid(time()));

$header = "";
$header .= "From: ".$_POST["name_ha"]."<".$_POST["email"].">
Reply-To: ".$_POST["email"]."";

$header .= "MIME-Version: 1.0
";
$header .= "Content-Type: multipart/mixed; boundary=\"".$sid."\"

";
$header .= "This is a multi-part message in MIME format.
";

$header .= "--".$sid."
";
$header .= "Content-type: text/html; charset=utf-8
";
$header .= "Content-Transfer-Encoding: 7bit

";
$header .= $message."

";

//*** Attachment ***//
if($_FILES["fileAttach"]["name"] != "")
{
    $FilesName = $_FILES["fileAttach"]["name"];
    $Content = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"]))); 
    $header .= "--".$sid."
";
    $header .= "Content-Type: application/octet-eam; name=\"".$FilesName."\"
"; 
    $header .= "Content-Transfer-Encoding: base64
";
    $header .= "Content-Disposition: attachment; filename=\"".$FilesName."\"

";
    $header .= $Content."

";
}

$flgSend = @mail($to,"A new file for you!",null,$header);  // @ = No Show Error //

if ($flgSend)
{
    echo "Mail sent.";
}

?>

The files I downloaded from shotdev.com and the one I modified are being hosted on the same server and under the same folder. But, for the one I have modified, the email is sent before the attachment is uploaded (~ 45% of the uploading process) if the file size is greater than 1MB. The email I received, there is no attachment and no information of sender ($name_ha). On the other hand, for the files downloaded on shotdev.com, the email will only be sent after the attachment is uploaded completely no matter how big it is.

Is there any error of the script, or something is missing, causing such an occurrence? Your time and help is much appreciated.

First, in code that you showed as example you have error on this line:

$to="admin@admin.com" . "$email";

Change it to:

$to="admin@admin.com, " . "$email";
  • Notice comma that you are missing... It needed to be there for every new receivers email you want to add on that particular way...

Also I have tested code on my server, everything works well.
I have 100 mbps upload and tried file that is 4.5 MB and it works.

So maybe there is a problem with your upload speed and allowed execution time of php script that you are calling to send this email.

Try to add the following just after <?php in php file that you setted in HTML form as action="something.php":

set_time_limit(0);

That means that there is no liimt in execution time of script, change 0 to desired number of seconds...

Also remove @ before @mail() function if there are errors you need to see them, hiding them will not do any good for you.

EDIT:

I have altered your code so that it does check if, firstly if there is file, if not sends mail without attachment. And if there is file it checks if it is encoded to base64 and splited and in that case it send the mail with attachment...

<?php

$location=$_POST['location'];
$name_ha=$_POST['name_ha'];
$name_person=$_POST['name_person'];
$email=$_POST['email'];
$date_sent=$_POST['date_sent'];
$date_completed=$_POST['date_completed'];
$date_received=$_POST['date_received'];
$FilesName = $_FILES["fileAttach"]["name"];

$to = "admin@mail.com," . "$email";

$message = "to";

$sid = md5(uniqid(time()));

$header = "";
$header .= "From: ".$_POST["name_ha"]."<".$_POST["email"].">
Reply-To: ".$_POST["email"]."";

$header .= "MIME-Version: 1.0
";
$header .= "Content-Type: multipart/mixed; boundary=\"".$sid."\"

";
$header .= "This is a multi-part message in MIME format.
";

$header .= "--".$sid."
";
$header .= "Content-type: text/html; charset=utf-8
";
$header .= "Content-Transfer-Encoding: 7bit

";
$header .= $message."

";


if($_FILES["fileAttach"]["name"] != "") {


    $Content = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"]))); 
    $header .= "--".$sid."
";
    $header .= "Content-Type: application/octet-stream; name=\"".$FilesName."\"
"; 
    $header .= "Content-Transfer-Encoding: base64
";
    $header .= "Content-Disposition: attachment; filename=\"".$FilesName."\"

";
    $header .= $Content."

";

}

if (strlen($FilesName) > 0) {

    if ($Content) {

        $flgSend = mail($to,"Here is that file",null,$header);

    }
    else {

        echo "problem with file...";

    }

}
else {

    $flgSend = mail($to,"Here is that file",null,$header);

}

if ($flgSend) {

    echo "Mail sent.";

}

?>

HERE IS HTML FOR IT:

<html>
<head>
</head>
<body>
<form method="post" action="sender.php" enctype="multipart/form-data" >
    location: <input type="text" name="location" />
    <br />
    name_ha: <input type="text" name="name_ha" />
    <br />
    name_person: <input type="text" name="name_person" />
    <br />
    email: <input type="text" name="email" />
    <br />
    date_sent: <input type="text" name="date_sent" />
    <br />
    date_completed: <input type="text" name="date_completed" />
    <br />
    date_received: <input type="text" name="date_received" />
    <br />
    file: <input type="file" name="fileAttach" />
    <br />
    <input type="submit" value="submit" />
</form>
</body>
</html>