Gmail显示noname.html文件附件而不是邮件 - PHP Mail - 没有库

时间:2016-05-07 13:53:48

标签: php html email html-email email-attachments

我正在制作一个网络应用程序,它应该发送包含附件的通知电子邮件。这是一个学校项目,我不允许使用像PHPMailer这样的类。

这是我的代码:

<?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;
}
?>

脚本发送带有三个附件的邮件。第一个和第二个是两个图像,第三个是名为noname.html的文件。我的HTML邮件在第三个文件中,没有HTML邮件内容。

如何在邮件正文中发送邮件而不是附件?

0 个答案:

没有答案
相关问题