我尝试发送包含嵌入图片的电子邮件,但图片作为文字附件发送,并附有图片的原始邮件内容。如何正确地将图像嵌入电子邮件?请指教。
$to = 'example@yahoo.com';
$subject = 'test';
$inline = base64_encode(file_get_contents('d.jpg'));
$sep = 'd.jpg';
$headers = "From: youremail@host.com\r\n";
$headers .= "MIME-Version: 1.0\r\n" .
"Content-Type: multipart/related;boundary=\"d0f4ad49cc20d19bf96d4adf9322d567\""."\r\n" ;
$message = "--d0f4ad49cc20d19bf96d4adf9322d567
Content-type: text/html; charset=utf-8
Content-transfer-encoding: 8bit\r\n
<html>
<head>
<style> p {color:green} </style>
</head>
<body>
A line above
<br>
<img src='cid:PHP-CID-d'>".
'<br>
a line below
</body>
</html>'."\n\n";
$message .= "--d0f4ad49cc20d19bf96d4adf9322d567\r\n
Content-Type: image/jpeg;\r\nname=\"$sep\"\r\n"
."Content-Transfer-Encoding: base64\r\n"
."Content-ID: PHP-CID-d\r\n
Content-Disposition: inline;\r\n
filename=$sep; size=" .filesize('d.jpg'). ";\r\n
$inline";
mail($to, $subject, $message, $headers) ;