有没有办法将图像附加到用 PHP 创建的 html 格式的电子邮件消息中?
我们需要确保发送给在阅读电子邮件时可能无法访问互联网的客户的电子邮件上有公司 Logo (他们显然会通过互联网下载文件)。
最佳答案
试试 PEAR Mail_Mime包,可以embed images for you .
您需要使用 addHTMLImage()方法并传递一个内容 ID (cid),这是一个唯一的文本字符串,您还将在 img 的 src 属性中用作 cid:
URL。例如:
include('Mail.php');
include "Mail/mime.php";
$crlf = "\r\n";
$hdrs = array(
'From' => 'foo@bar.org',
'Subject' => 'Mail_mime test message'
);
$mime = new Mail_mime($crlf);
//attach our image with a unique content id
$cid="mycidstring";
$mime->addHTMLImage("/path/to/myimage.gif", "image/gif", "", true, $cid);
//now we can use the content id in our message
$html = '<html><body><img src="cid:'.$cid.'"></body></html>';
$text = 'Plain text version of email';
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$body = $mime->get();
$hdrs = $mime->headers($hdrs);
$mail =& Mail::factory('mail');
$mail->send('person@somewhere.org', $hdrs, $body);
关于PHP 将图像附加到电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/536838/