0

I have created two scripts for this HTML and a corresponding PHP.

HTML markup:

<html>
  <body>
    <form action="http://senindiaonline.in/admar/non-series-numbers-db.php" method="post" target="_blank" name="non_series_numbers_db" enctype="multipart/form-data">
      <label for="emp-id">Employee ID: </label>
      <input type="text" name="emp-id" id="emp-id" maxlength="15" style='text-transform:uppercase'/>
      <br/>
      <label for="no-csv">If you have a mobile number database in CSV file format upload that here: </label>
      <input name="no-csv" id="no-csv" type="file" accept=".csv"/>
      <br/>
      <label for="no-xls">If you have a mobile number database in XLS (excel) file format upload that here: </label>
      <input name="no-xls" id="no-xls" type="file" accept=".xls"/>
      <br/>
      <label for="no-xlsx">If you have a mobile number database in XLSX (excel) file format upload that here: </label>
      <input name="no-xlsx" id="no-xlsx" type="file" accept=".xlsx"/>
      <br/>
      <input name="send" value="Send" type="submit" id="send"/>
    </form>
  </body>
</html>

PHP script:

<?php
error_reporting(0);
$email_to = 'senindiagroup@gmail.com';
$email_subject = "Non-Seriesed Mobile Number Database from Ad&Marketing Employee Panel of Sen India Online inc.";    
$emp_id = $_POST['emp-id'];
$sub="Employee ID: $emp_id";
$bound_text = "jimmyPI23";
$bound = "--" .$bound_text. "\r\n";
$bound_last = "--" .$bound_text. "--\r\n";
$email_message .= "If you can see this MIME than your  client doesn't accept MIME types! \r\n" .$bound;
$email_message .= "Content-Type: text/html; Charset=\"iso-8859-1\"\r\n" ."Content-Transfer-Encoding: 7bit\r\n\r\n" ."$sub\r\n" .$bound;

//getting temporary file location with name
$a=file_get_contents($_FILES['no-csv']['tmp_name']);
$b=file_get_contents($_FILES['no-xls']['tmp_name']);
$c=file_get_contents($_FILES['no-xlsx']['tmp_name']);

//getting original name of the file you uploaded
$name=$_FILES["no-csv"]["name"];
$name1=$_FILES["no-xls"]["name"];
$name2=$_FILES["no-xlsx"]["name"];

$email_message .= "Content-Type: {\"application/octet-stream\"}; \n" . "name=\"$name\ "\n" . "Content-Transfer-Encoding: base64\r\n" ."Content-Disposition: attachment; \n" . " filename=\"$name\ "\n"."\r\n" .chunk_split(base64_encode($a)).$bound;

mail($email_to, $email_subject, $email_message, $headers);
?>

The error is:

syntax error showing on line 31, attach file with mail command.

And the mail is not send. I solve this error with DreamWeaver suggestion. Then the mail is send, but the attachment is not included in the mail.

zx485
  • 28,498
  • 28
  • 50
  • 59
  • 2
    I just googled and got this link: http://stackoverflow.com/questions/12301358/send-attachments-with-php-mail – Duy Hoang Apr 14 '16 at 10:11
  • Possible duplicate of [sending an mail with attachment using php](http://stackoverflow.com/questions/6175229/sending-an-mail-with-attachment-using-php) – Mark van Herpen Apr 14 '16 at 10:12
  • I replaced all you 'lebel' tags with the correct HTML element tag 'label'. – zx485 Apr 14 '16 at 10:38
  • Seriously... stop trying to use `mail()` to send attachments. It can be done, but you'll need to write a mountain of code. Instead, just download phpMailer or Swiftmailer, and use one of them. They've already done all that work for you, so you just need to include them and call them. They also resolve a lot of the security issues with sending email, and have additional features over the simple `mail()` function such as the ability to send via an external smtp server instead. – Simba Apr 14 '16 at 10:44

1 Answers1

0

This is the example of sending multiple attachments in mail

<?php

    for($i=0; $i < count($_FILES['csv_file']['name']); $i++){

        $ftype[]       = $_FILES['csv_file']['type'][$i];
        $fname[]       = $_FILES['csv_file']['name'][$i];

    }


    // array with filenames to be sent as attachment
    $files = $fname;

    // email fields: to, from, subject, and so on
    $to = "example@gmail.com";
    $from = "example@gmail.com"; 
    $subject ="My subject"; 
    $message = "My message";
    $headers = "From: $from";

    // boundary 
    $semi_rand = md5(time()); 
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

    // headers for attachment 
    $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 

    // multipart 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";

    // preparing attachments
    for($x=0;$x<count($files);$x++){
        $file = fopen($files[$x],"rb");
        $data = fread($file,filesize($files[$x]));
        fclose($file);
        $data = chunk_split(base64_encode($data));
        $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . 
        "Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" . 
        "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
        $message .= "--{$mime_boundary}\n";
    }

    // send

    $ok = @mail($to, $subject, $message, $headers); 
    if ($ok) { 
        echo "<p>mail sent to $to!</p>"; 
    } else { 
        echo "<p>mail could not be sent!</p>"; 
    } 


?>

if you want simple solution use phpmailer library, that would be easy to use,easy to configure.

Naisa purushotham
  • 905
  • 10
  • 18