PHP 将图像附加到电子邮件

标签 php email attachment

有没有办法将图像附加到用 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/

相关文章:

PHP PDO - 动态绑定(bind)变量数

vba - 如何获取发件人的电子邮件地址?

php - PHP 的 mail() 函数附件支持在 Windows 上是否有点损坏?

php - 如何压缩文件夹并使用php下载它?

php - 使用 Ajax 获得一组连续的 DB 派生单选按钮

javascript - PHP 中的 HTTPPost 到 JSON

perl - 在 perl 中发送多部分邮件

python - 我如何在 Django 中使用 rapportive.py

vba - 用于在 Gmail 中撰写带有附件的邮件的 URL

php - 尝试更改上传目录时出现空白页