I try to add send email attachment using php,file attachment working fine but attachment file open blank document.How to solve this issue.Below mentioned my code.
$from_email = 'sender_mail@example.com'; //sender email
$recipient_email = 'manosk24@gmail.com'; //recipient email
$subject = 'Test mail'; //subject of email
$message = 'This is body of the message'; //message body
$filename = "file1.pdf";
$path = $_SERVER['DOCUMENT_ROOT'] . "/mail-function/upload/";
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "rb");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$boundary = md5(uniqid(time()));
//header
$headers = "MIME-Version: 1.0
";
$headers .= "From:" . $from_email . "
";
$headers .= "Reply-To: " . $user_email . "" . "
";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary
";
//plain text
$body = "--$boundary
";
$body .= "Content-Type: text/plain; charset=ISO-8859-1
";
$body .= "Content-Transfer-Encoding: base64
";
$body .= chunk_split(base64_encode($message));
//attachment
$body .= "--$boundary
";
$body .="Content-Type: application/pdf; name=" . $filename . "
";
$body .="Content-Disposition: attachment; filename=" . $filename . "
";
$body .="Content-Transfer-Encoding: base64
";
$body .="X-Attachment-Id: " . rand(1000, 99999) . "
";
$body .= $encoded_content;
$sentMail = @mail($recipient_email, $subject, $body, $headers);
if ($sentMail) { //output success or failure messages
die('Thank you for your email');
} else {
die('Could not send mail! Please check your PHP mail configuration.');
}
Sorry for my spelling mistake..