Problem
There are several errors in the code:
- The email header must be completed
CRLF. You only have LF. - A B64 encoded file and message are added to the headers. They should be sent as the body of the message.
CRLF.- ,
CRLF.
, RFC 2046. CRLF. ( BNF, .)
multipart-body := [preamble CRLF]
dash-boundary CRLF
body-part *encapsulation
close-delimiter
[CRLF epilogue]
dash-boundary := "--" boundary
body-part := MIME-part-headers [CRLF *OCTET]
encapsulation := delimiter
CRLF body-part
delimiter := CRLF dash-boundary
close-delimiter := delimiter "--"
:
$uid = md5(uniqid(time()));
$headers = "From: ".$from_name." <".$mailto.">\r\n";
$headers .= "Reply-To: ".$from_mail."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n";
$body = "This is a multi-part message in MIME format.\n";
$body .= "--".$uid."\r\n";
$body .= "Content-type:text/plain\r\n";
$body .= "Content-Transfer-Encoding: 8bit\r\n";
$body .= "\r\n";
$body .= $message;
foreach ($_FILES as $file) {
$name = $file['name'];
$handle = fopen($file['tmp_name'], "r");
$content = fread($handle, $file['size']);
fclose($handle);
$content = chunk_split(base64_encode($content));
$body .= "\r\n--{$uid}\r\n";
$body .= "Content-Type: {$file['type']}; name=\"{$name}\"\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "Content-Disposition: attachment; filename=\"{$name}\"\r\n";
$body .= "\r\n";
$body .= $content;
}
$body .= "\r\n--{$uid}--";
$mailto = 'myemail@email.example';
return mail($mailto, $subject, $body, $headers, '-f' . $mailto);