Пусто (0KB) Вложения в PHP для почтовой программы - PullRequest
0 голосов
/ 18 октября 2018

Я пытаюсь заставить работать простой почтовый PHP, но не могу понять, почему я получаю пустые вложения при отправке электронного письма.Надеясь, что кто-то увидит, что мне не хватает.

Я сократил это до основ, чтобы помочь сократить этот пост, вот 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> 

И 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>"; 
    } 



}
?>

Любая помощь высоко ценится.

1 Ответ

0 голосов
/ 18 октября 2018

Ваши ссылки на массив отключены.Когда у вас есть

<input type=file name=csv_file[]>

Вам понадобится ссылка

$_FILES['csv_file'][0]

Чтобы получить первый файл

Так что

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

Будет работатьлучше для вас ....

Хорошо, согласно вашему комментарию, вы, по крайней мере, получаете данные.

Вот что работает для меня для моего скрипта form2mail, который обрабатывает вложения (илине ...)

// 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);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...