2

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?

Nisse Engström
  • 4,555
  • 22
  • 24
  • 38
  • I noticed that you are suppressing the `mail` function at the end. Did you see any warning messages when you sent it? – The Unknown Dev May 07 '16 at 13:56
  • 1
    [Here's a fairly definitive answer](http://stackoverflow.com/a/23853079/3585500) in java. But it's all about content-types and encoding. – ourmandave May 07 '16 at 15:56
  • I sent with this function a test mail to my gmail account and gmail had not display any warning messages – Merlin Woff May 08 '16 at 08:01
  • Should I change the content type from text/html to related/html? – Merlin Woff May 08 '16 at 08:03
  • Try `Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable` [as seen here](http://stackoverflow.com/a/10891003/3585500). But also [see this related comment](http://stackoverflow.com/a/10899430/3585500) below it. – ourmandave May 08 '16 at 12:19
  • Okay, I have tried now to replace the "Content-Transfer-Encoding: 8-Bit" with "Content-Transfer-Encoding: quoted-printable" and the 'Content-Type: text/html charset=UTF-8" with "Content-Type: text/html charset="iso-8859-1"' and still it isnt working. I checked the link you sent me, but I dont see any end boundary in my code – Merlin Woff May 08 '16 at 15:28

0 Answers0