PHP 将图像附加到电子邮件 [英] PHP Attaching an image to an email

查看:36
本文介绍了PHP 将图像附加到电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将图像附加到用 PHP 创建的 html 格式的电子邮件?

Is there a way to attach an image to an html formatted email message created in PHP?

我们需要确保发送给客户的电子邮件中包含公司徽标,而这些客户在阅读电子邮件时可能无法访问互联网(他们显然可以下载文件).

We need to ensure that a corporate logo is on emails sent to clients who may not have access to the internet whilst reading their email (They will obviously have it to download the files).

推荐答案

试试 PEAR Mail_Mime 包,它可以为您嵌入图片.

Try the PEAR Mail_Mime package, which can embed images for you.

您需要使用 addHTMLImage() 方法并传递内容 ID (cid),这是一个唯一的文本字符串,您还将在 img 的 src 属性中将其用作 cid: URL.例如:

You need to use the addHTMLImage() method and pass a content id (cid), which is a unique string of text you will also use in your img's src attribute as a cid: URL. For example:

include('Mail.php');
include "Mail/mime.php";


$crlf = "
";
$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 将图像附加到电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆