download2014711 2019-01-28 15:08
浏览 31

来自PHP邮件功能的PDF文件无法打开[重复]

This question already has an answer here:

Note: Before posting this question, I had reviewed lot of questions which are related to my issue, but none of them helped me and so am posting the question here.

I want to send multiple pdf files in an email using PHP mail function

I used the code from below SO answers
How to attach two or multiple files and send mail in PHP

The user can upload the pdf files and can send the email, but the PDF files which are received cannot be opened. I think that the content of the pdf files are not transfered and only the file names are transferred and so that size of the pd files are in bytes.

 <form action="#" 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

 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 = "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 .= "
MIME-Version: 1.0
" . "Content-Type: multipart/mixed;
" . " boundary=\"{$mime_boundary}\""; 

// multipart boundary 
$message = "This is a multi-part message in MIME format.

" . "--{$mime_boundary}
" . "Content-Type: text/plain; charset=\"iso-8859-1\"
" . "Content-Transfer-Encoding: 7bit

" . $message . "

"; 
$message .= "--{$mime_boundary}
";

// 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\"};
" . " name=\"$files[$x]\"
" . 
    "Content-Disposition: attachment;
" . " filename=\"$files[$x]\"
" . 
    "Content-Transfer-Encoding: base64

" . $data . "

";
    $message .= "--{$mime_boundary}
";
}

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



}

?>

Can anyone please guide me here to send proper pdf files without truncating

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 下图接收小电路,谁知道原理
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探