简体   繁体   中英

Empty (0KB) Attachments in PHP for mailer

I'm trying to get a simple PHP mailer working but can't figure out why I'm getting empty attachments when the email is sent. Hoping someone can see what I'm missing.

I've stripped it back to basics to help shorten this post, here is the HTML-

 <form action="http://www.example.co.uk/send_form_ybk.php" method="POST" enctype="multipart/form-data" > <input type="file" name="csv_file[]" /> <br/> <input type="file" name="csv_file[]" /> <br/> <input type="file" name="csv_file[]" /> <br/> <input type="submit" name="upload" value="Upload" /> <br/> </form> 

And the PHP

 <?php if($_POST) { 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 = "ben@example.co.uk"; $from = "ben@example.co.uk"; $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\\r\\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\\n" . " filename=\\"$files[$x]\\"\\n" . "Content-Transfer-Encoding: base64\\n\\n" . $data . "\\r\\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>"; } } ?> 

Any help much appreciated.

Your array refs are off. When you have

<input type=file name=csv_file[]>

You'll need to reference

$_FILES['csv_file'][0]

To get the first file

So

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

Will work better for you....

Ok, per your comment you are now at least getting data to come through.

Here's what is working for me for my form2mail script that handles attachments (or not...)

// build headers
$separator=md5(time());

$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: $from_name <$from_address>\r\n";
$headers .= "Date: " . date("Ymd H:i:s") . "\r\n";
$headers .= "Reply-To: $from_name <$from_address>\r\n";            
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: High\r\n";
$headers .= "X-Mailer: ".$_SERVER['PHP_SELF']. "?id=". $_SERVER['UNIQUE_ID']. "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"\r\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n";
$headers .= "This is a MIME encoded message.\r\n";
// headers complete


// build message body
// text message as normal for form2mail
$messageBody="--".$separator."\r\n";
$messageBody.="Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$messageBody.="Content-Transfer-Encoding: 8bit\r\n";
$messageBody.="\r\n".$message_body."\r\n\r\n";
// add attachments
for($i=0;$i<count($attachments);$i++){
    $attachcontent=chunk_split(base64_encode(file_get_contents($attachments[$i]['tmp_name'])));
    $messageBody.="--".$separator."\r\n";
    $messageBody.="Content-Type: application/octet-stream; name=\"".$attachments[$i]['name']."\"\r\n";
    $messageBody.="Content-Transfer-Encoding: base64\r\n";
    $messageBody.="Content-Disposition: attachment; filename=\"".$attachments[$i]['name']."\"\r\n";
    $messageBody.="\r\n".$attachcontent."\r\n";
}
$messageBody.="--" . $separator . "--";
mail($to_address, $subject, $messageBody, $headers);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM