我正在使用下面的代码将图片附加并嵌入到电子邮件中。这适用于Gmail,我原本认为这将是最大的障碍。出于某种原因,当我通过Apple Mail和Thunderbird(同一帐户)访问电子邮件时,图像无法加载但分别显示以下内容:
我做错了什么?我无法正常托管和链接这些图像,因为它们是即时生成的数据驱动图形。多部分附件样式嵌入似乎是我唯一的选择。
$to = "...";
$body = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html><body><img src="cid:image"/></body></html>';
$eol = PHP_EOL;
$separator = md5(time());
// headers
$headers = "From: ..." . $eol;
$headers .= "Reply-To: ..." . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
// message
$message = "--" . $separator . $eol;
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"" . $eol;
$message .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;
$message .= $body . $eol;
// attachment
$filename = "image.png";
$content = chunk_split(base64_encode(file_get_contents($filename)));
$message .= "--" . $separator . $eol;
$message .= "Content-Type: application/octet-stream; name=\"image.png\"" . $eol;
$message .= "Content-ID: <image>" . $eol;
$message .= "Content-Transfer-Encoding: base64" . $eol;
$message .= "Content-Disposition: attachment; filename=\"image.png\"" . $eol;
$message .= $content . $eol;
$message .= "--" . $separator . "--";
mail($to, "test", $message, $headers);