I am making a web application, which should send notification emails with attachments. It is a school project and I'm not allowed to use a class like PHPMailer.
This is my code:
<?php
$imagefiles = array();
$imagefiles[] = "https://upload.wikimedia.org/wikipedia/commons/thumb/b/b3/Wikipedia-logo-v2-en.svg/2000px-Wikipedia-logo-v2-en.svg.png";
$imagefiles[] = "http://www.gizmoids.com/wp-content/uploads/2015/11/new-google-logo1.jpg";
$message = "<h3>Hi, how are you today?</h3>";
$attachments = array();
foreach ($imagefiles AS $imagefile) {
$name = basename($imagefile);
$size = filesize($imagefile);
$data = file_get_contents($imagefile);
$type = mime_content_type($imagefile);
$attachments[] = array(
"name" => $name,
"size" => $size,
"type" => $type,
"data" => $data
);
}
mail_att("me@example.com", "Mister Example", "you@example.com", "Test-Mail", $message, $attachments);
function mail_att($to, $sendername, $sendermail, $subject, $message, $attachments)
{
$mime_boundary = "-----=" . md5(uniqid(mt_rand(), 1));
$header = "From:" . $sendername . "<" . $sendermail . ">\n";
$header .= "Reply-To: " . $sendermail . "\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: multipart/mixed;\r\n";
$header .= " boundary=\"" . $mime_boundary . "\"\r\n";
$content = "This is a multi-part message in MIME format.\r\n\r\n";
$content .= "--" . $mime_boundary . "\r\n";
$content .= "Content-Type: text/html charset=UTF-8\r\n";
$content .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
$content .= $message . "\r\n";
if (is_array($attachments) AND is_array(current($attachments))) {
foreach ($attachments AS $dat) {
$data = chunk_split(base64_encode($dat['data']));
$content .= "--" . $mime_boundary . "\r\n";
$content .= "Content-Disposition: attachment;\r\n";
$content .= "\tfilename=\"" . $dat['name'] . "\";\r\n";
$content .= "Content-Length: ." . $dat['size'] . ";\r\n";
$content .= "Content-Type: " . $dat['type'] . "; name=\"" . $dat['name'] . "\"\r\n";
$content .= "Content-Transfer-Encoding: base64\r\n\r\n";
$content .= $data . "\r\n";
}
$content .= "--" . $mime_boundary . "--";
} else {
$data = chunk_split(base64_encode($attachments['data']));
$content .= "--" . $mime_boundary . "\r\n";
$content .= "Content-Disposition: attachment;\r\n";
$content .= "\tfilename=\"" . $attachments['name'] . "\";\r\n";
$content .= "Content-Length: ." . $attachments['size'] . ";\r\n";
$content .= "Content-Type: " . $attachments['type'] . "; name=\"" . $attachments['name'] . "\"\r\n";
$content .= "Content-Transfer-Encoding: base64\r\n\r\n";
$content .= $data . "\r\n";
}
if (@mail($to, $subject, $content, $header))
return true;
else
return false;
}
?>
The script sends a mail with three attachments. The first and second are the two images and the third is a file named noname.html
. My HTML message is inside the third file and there is no HTML mail content.
How can I send my message in the body of the email and not as an attachment?