3

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"].">\nReply-To: ".$_POST["email"]."";

$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$sid."\"\n\n";
$header .= "This is a multi-part message in MIME format.\n";

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

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

$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.

Sorx
  • 33
  • 2
  • I need to test this, first impression is that you have some problem with headers, I don't like how is that part of code done. – Develoger Oct 08 '12 at 09:57
  • I would isolate the issue first by retracing what you changed. – Ja͢ck Oct 08 '12 at 10:40
  • seriously, don't even try to use PHP's built-in `mail()` function if you're trying to work with attachements. You'll spend your life trying to make it work properly. You'd be much better off using the [phpMailer class](http://code.google.com/a/apache-extras.org/p/phpmailer/). See also my answer here: http://stackoverflow.com/questions/12301358/send-attachments-with-php-mail/12302354#12302354 – SDC Oct 08 '12 at 10:44

1 Answers1

1

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"].">\nReply-To: ".$_POST["email"]."";

$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$sid."\"\n\n";
$header .= "This is a multi-part message in MIME format.\n";

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


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


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

}

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>
Develoger
  • 3,950
  • 2
  • 24
  • 38
  • Your help is much appreciated. Unfortunately, the same problem still exists. Would the size of the form and the script of sender.php contribute to it? Or it's all about the connection problem? Currently, I am just using a free web host. I will try it later using localhost on my PC. Thanks again for everyone's help. – Sorx Oct 09 '12 at 02:18
  • 1
    Uh, an "free web host" is problem 99% sure! They are usually limiting available resources... It is not surprise that it works perfectly on my server and fails on "free web host" that you use. – Develoger Oct 09 '12 at 06:52