I'm using the following php code to try to attach .html files to email using php but it's not attaching the file:
$recipient_email="webmaster@domain.com";
$subject = "Test email";
$mail_headers= "From: Me <me@mydomain.com>\r\n";
$mail_boundary = md5(uniqid(time()));
$mail_headers .= "MIME-Version: 1.0\r\n";
$mail_headers .= "Content-type: multipart/mixed;boundary=\"$mail_boundary \"";
$mail_headers .= "\r\n\r\n";
$mail_headers .= "This is a multi-part message in MIME format.";
$mail_headers .= "\r\n\r\n";
$userfile = "/home/account_name/public_html/test.html";
$fp = fopen($userfile, "r");
$file = fread($fp, filesize($userfile));
$file = chunk_split(base64_encode($file));
$mail_body = "--$mail_boundary\n";
$mail_body .= "Content-type: text/plain; charset=euc-kr\r\n";
$mail_body .= "Content-transfer-encoding: 8bit\r\n\r\n";
$mail_body .= " Here goes the project file.\r\n";
$mail_body .= "--$mail_boundary\r\n";
$filename = basename($userfile);
$mail_body .= "Content-type: text/html; name=$filename\r\n";
$mail_body .= "Content-transfer-encoding:base64\r\n\r\n";
$mail_body .= $file. "\r\n\r\n";
$mail_body .= " --$mail_boundary--";
$send_check2 = mail($recipient_email,$subject,$mail_body,$mail_headers);
if ($send_check2) {
echo "<p>The email has been sent.</p>";
}
The email is being sent out but there is no attachment. What am I doing wrong?
Also, it shows this output in the email message:
This is a multi-part message in MIME format.
--435a053fc841a34f1f24d497c55ef92b
Content-type: text/plain; charset=euc-kr
Content-transfer-encoding: 8bit
Here goes the project file.
--435a053fc841a34f1f24d497c55ef92b
Content-type: text/html; name=test.html
Content-transfer-encoding:base64
--435a053fc841a34f1f24d497c55ef92b--