-1

i am trying this time sending email with attachment code seems correct without sending email with attachment. but i got error as PHP Parse error: syntax error, unexpected end of file on line 77 what is missing ???

    <?php
           if(isset($_FILES) && (bool) $_FILES) {
        $AllowedExtensions = ["pdf","doc","docx","gif","jpeg","jpg","png","rtf","txt"];
        $files = [];
        $server_file = [];
        foreach($_FILES as $name => $file) {
            $file_name = $file["name"];
            $file_temp = $file["tmp_name"];
            foreach($file_name as $key) {
                $path_parts = pathinfo($key);
                $extension = strtolower($path_parts["extension"]);
                if(!in_array($extension, $AllowedExtensions)) { die("Extension not allowed"); }
                $server_file[] = "uploads/{$path_parts["basename"]}";
            }
            for($i = 0; $i<count($file_temp); $i++) { move_uploaded_file($file_temp[$i], $server_file[$i]); }
        }
        $headers = "From: $from";
        $semi_rand = md5(time());
        $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
        $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
        $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
        $message .= "--{$mime_boundary}\n";
        $FfilenameCount = 0;
        for($i = 0; $i<count($server_file); $i++) {
            $afile = fopen($server_file[$i],"rb");
            $data = fread($afile,filesize($server_file[$i]));
            fclose($afile);
            $data = chunk_split(base64_encode($data));
            $name = $file_name[$i];
            $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" .
                "Content-Disposition: attachment;\n" . " filename=\"$name\"\n" .
                "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
            $message .= "--{$mime_boundary}\n";
        }

if($_POST['submit'] !='')
{
$name = htmlspecialchars($_REQUEST['name']);

$email = htmlspecialchars($_REQUEST['email']);

$mobile = htmlspecialchars($_REQUEST['mobile']);

$company = htmlspecialchars($_REQUEST['company']);

$qty = htmlspecialchars($_REQUEST['qty']);

//$upload = htmlspecialchars($_REQUEST['upload']);

$msg = htmlspecialchars($_REQUEST['msg']);

}

$to="example@gmail.com";


$subject = "Order Information";

$message .= "Name: " . $name . "\n";

$message .= "Email: " . $email . "\n";

$message .= "ContactNo: " . $mobile . "\n";

$message .= "Company: " . $company . "\n";

$message .= "Quantity: " . $qty . "\n";

//$message .= "Upoload: " . $upload . "\n";

$message .= "Message: " . $msg . "\n";


if(mail($to, $subject, $message, $headers))
{   echo 'thank you';   }
else{ echo 'error';}
?>
GOVINDA MAHAJAN
  • 43
  • 2
  • 12

3 Answers3

1

You are getting Syntax error, unexpected end because you missed the ending bracket for this line:

if(isset($_FILES) && (bool) $_FILES) {

Also note that, when you fix this parse error, you will the few Undefined Indexes Notices because you defined $name, $email etc inside the if($_POST['submit'] !='') and using outside the check.

Full Modified Code:

<?php
if(isset($_FILES) && (bool) $_FILES) {
    $AllowedExtensions = ["pdf","doc","docx","gif","jpeg","jpg","png","rtf","txt"];
    $files = [];
    $server_file = [];
    foreach($_FILES as $name => $file) {
        $file_name = $file["name"];
        $file_temp = $file["tmp_name"];
        foreach($file_name as $key) {
            $path_parts = pathinfo($key);
            $extension = strtolower($path_parts["extension"]);
            if(!in_array($extension, $AllowedExtensions)) { die("Extension not allowed"); }
            $server_file[] = "uploads/{$path_parts["basename"]}";
        }
        for($i = 0; $i<count($file_temp); $i++) { move_uploaded_file($file_temp[$i], $server_file[$i]); }
    }
    $headers = "From: $from";
    $semi_rand = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
    $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
    $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n";
    $message .= "--{$mime_boundary}\n";
    $FfilenameCount = 0;
    for($i = 0; $i<count($server_file); $i++) {
        $afile = fopen($server_file[$i],"rb");
        $data = fread($afile,filesize($server_file[$i]));
        fclose($afile);
        $data = chunk_split(base64_encode($data));
        $name = $file_name[$i];
        $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" .
            "Content-Disposition: attachment;\n" . " filename=\"$name\"\n" .
            "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
        $message .= "--{$mime_boundary}\n";
    }
}

/** Your submit block **/
if(isset($_POST['submit']))
{
    $name = htmlspecialchars($_REQUEST['name']);
    $email = htmlspecialchars($_REQUEST['email']);
    $mobile = htmlspecialchars($_REQUEST['mobile']);
    $company = htmlspecialchars($_REQUEST['company']);
    $qty = htmlspecialchars($_REQUEST['qty']);
    $msg = htmlspecialchars($_REQUEST['msg']);
    $to="example@gmail.com";
    $subject = "Order Information";
    $message .= "Name: " . $name . "\n";
    $message .= "Email: " . $email . "\n";
    $message .= "ContactNo: " . $mobile . "\n";
    $message .= "Company: " . $company . "\n";
    $message .= "Quantity: " . $qty . "\n";
    $message .= "Message: " . $msg . "\n";
    if(mail($to, $subject, $message, $headers)) {   
        echo 'thank you';   
    }
    else { 
        echo 'error';
    }
}

?>

UPDATE 1:

I don't know why are you using $message at end of this line:

$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";

It will also give you Undefined Variable Notice, just remove it.

devpro
  • 16,184
  • 3
  • 27
  • 38
  • i have added it to line no 77 after mail function ends... – GOVINDA MAHAJAN Feb 04 '16 at 07:44
  • after this fix, you will get the undefined index notices.. @GOVINDAMAHAJAN – devpro Feb 04 '16 at 07:48
  • got this messages ... – GOVINDA MAHAJAN Feb 04 '16 at 07:53
  • Warning:move_uploaded_file(uploads/order.jpg):failed to open stream:No such file or directory on line 15 Warning:move_uploaded_file():Unable to move '/tmp/phpoJARVm' to'uploads/order.jpg' on line 15 Notice:Undefined variable:on line17 Notice:Undefined variable: message on line 21 Warning:fopen(uploads/order.jpg):failed to open stream: No such file or directory on line 25 Warning:filesize():stat failed for uploads/order.jpg on line 26 Warning:fread() expects parameter 1 to be resource, boolean given on line 26 Warning:fclose() expects parameter 1 to be resource,boolean given on line 27 – GOVINDA MAHAJAN Feb 04 '16 at 08:15
  • @GOVINDAMAHAJAN: check your file path, this is another issue.. not related to this question. – devpro Feb 04 '16 at 08:25
  • thank you sir, i have added this code as it is in my mail.php this works but now this only shows attachment not all form filed values.. what can i do for this ??? – GOVINDA MAHAJAN Feb 04 '16 at 08:43
  • i suggest u to create a separate question u will get more exposure. @GOVINDAMAHAJAN – devpro Feb 04 '16 at 08:45
  • PHP Notice: Undefined variable: from on line 17 PHP Notice: Undefined variable: message on line 21 – GOVINDA MAHAJAN Feb 04 '16 at 08:47
  • @GOVINDAMAHAJAN: which one is line 21? – devpro Feb 04 '16 at 08:48
  • line 17 $headers = "From: $from"; line 21 $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; – GOVINDA MAHAJAN Feb 04 '16 at 08:48
  • see friend, "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n" here $message is not defined.. @GOVINDAMAHAJAN – devpro Feb 04 '16 at 08:49
  • just use this: $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n"; @GOVINDAMAHAJAN – devpro Feb 04 '16 at 08:49
  • @GOVINDAMAHAJAN: i have also updated in answer. – devpro Feb 04 '16 at 08:52
  • what about line 17 $headers = "From: $from"; PHP Notice: Undefined variable: from on line 17 – GOVINDA MAHAJAN Feb 04 '16 at 08:57
  • @GOVINDAMAHAJAN: $from = "youremail@test.com"; $headers = "From: $from"; – devpro Feb 04 '16 at 08:59
  • i have updated there are no errors but i got only attachment not all input values... – GOVINDA MAHAJAN Feb 04 '16 at 09:04
  • @GOVINDAMAHAJAN: this is another, issue, attachment and html in email body, i suggest u to create a new question... like HOW to get HTML and Attachment in email together ... – devpro Feb 04 '16 at 09:37
  • thank you for your kind and deep suggestions... – GOVINDA MAHAJAN Feb 04 '16 at 09:41
0

Try this code:

<?php
    if(isset($_FILES) && (bool) $_FILES) {
        $AllowedExtensions = ["pdf","doc","docx","gif","jpeg","jpg","png","rtf","txt"];
        $files = [];
        $server_file = [];
        foreach($_FILES as $name => $file) {
            $file_name = $file["name"];
            $file_temp = $file["tmp_name"];
            foreach($file_name as $key) {
                $path_parts = pathinfo($key);
                $extension = strtolower($path_parts["extension"]);
                if(!in_array($extension, $AllowedExtensions)) { die("Extension not allowed"); }
                $server_file[] = "uploads/{$path_parts["basename"]}";
            }
            for($i = 0; $i<count($file_temp); $i++) { move_uploaded_file($file_temp[$i], $server_file[$i]); }
        }
        $headers = "From: $from";
        $semi_rand = md5(time());
        $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
        $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
        $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
        $message .= "--{$mime_boundary}\n";
        $FfilenameCount = 0;
        for($i = 0; $i<count($server_file); $i++) {
            $afile = fopen($server_file[$i],"rb");
            $data = fread($afile,filesize($server_file[$i]));
            fclose($afile);
            $data = chunk_split(base64_encode($data));
            $name = $file_name[$i];
            $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" .
                "Content-Disposition: attachment;\n" . " filename=\"$name\"\n" .
                "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
            $message .= "--{$mime_boundary}\n";
        }

if($_POST['submit'] !='')
{
$name = htmlspecialchars($_REQUEST['name']);

$email = htmlspecialchars($_REQUEST['email']);

$mobile = htmlspecialchars($_REQUEST['mobile']);

$company = htmlspecialchars($_REQUEST['company']);

$qty = htmlspecialchars($_REQUEST['qty']);

//$upload = htmlspecialchars($_REQUEST['upload']);

$msg = htmlspecialchars($_REQUEST['msg']);

}

$to="amar.ghodke30@gmail.com";


$subject = "Order Information";

$message .= "Name: " . $name . "\n";

$message .= "Email: " . $email . "\n";

$message .= "ContactNo: " . $mobile . "\n";

$message .= "Company: " . $company . "\n";

$message .= "Quantity: " . $qty . "\n";

//$message .= "Upoload: " . $upload . "\n";

$message .= "Message: " . $msg . "\n";


if(mail($to, $subject, $message, $headers))
{   echo 'thank you';   }
else{ echo 'error';}
    } // This one is missing :)
?>

You had a missing "}" at the end of the file.

0

Missing one closing bracket before this line...

if($_POST['submit'] !='')

You have not closed your first if condition block.

Gagan Upadhyay
  • 255
  • 2
  • 11