This question already has an answer here:
- Sending multiple attachment in an email using PHP 2 answers
- Send attachments with PHP Mail()? 13 answers
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>