Im trying to send mail from my server with file attachment, my problem is that the mail arrives with everything except the content of the attached file, i tried pdf,docx,jpeg.... no matter what format i attach, ill get blank/empty file.
im running wamp server with sendmail(), here is the php script:
<?php
$email = $_GET['page'];
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
//$attachment = chunk_split(base64_encode(file_get_contents('./payDoc.pdf', FILE_USE_INCLUDE_PATH)));
$uid = md5(uniqid(time()));
$header = "From: ".$from_name." <".$from_mail.">
";
$header .= "Reply-To: ".$replyto."
";
$header .= "MIME-Version: 1.0
";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"
";
$header .= "This is a multi-part message in MIME format.
";
$header .= "--".$uid."
";
$header .= "Content-type:text/plain; charset=iso-8859-1
";
$header .= "Content-Transfer-Encoding: 7bit
";
$header .= $message."
";
$header .= "--".$uid."
";
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"
";
$header .= "Content-Transfer-Encoding: base64
";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"
";
//$header .= $attachment."
";
$header .= "--".$uid."--";
// Messages for testing only, nobody will see them unless this script URL is visited manually
if (mail($mailto, $subject, "", $header)) {
echo "Message sent!";
} else {
echo "ERROR sending message.";
}
}
//File attach
$my_file = "payDoc.pdf";
$my_path = $_SERVER['DOCUMENT_ROOT']."/connect_android/include/";
//email is FROM
$my_name = "Android Store";
$my_mail = "demo@gmail.com";
$my_replyto = "demo@gmail.com";
//email is going TO
$to_email = $email;
// Subject of email
$my_subject = "Order details has arrived ";
$message = "Please fill the file attached,
some text to add
goes here";
// send email
mail_attachment($my_file, $my_path, $to_email, $my_mail, $my_name, $my_replyto, $my_subject, $message);
have you tried using PHPmailer? https://github.com/PHPMailer/PHPMailer
Literally had this same problem 5mins ago, turned out i missed adding this to my opening form tag.
enctype="multipart/form-data"