首页 / 邮件 / 嵌入图像以使用PHP在电子邮件中使用?
嵌入图像以使用PHP在电子邮件中使用?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了嵌入图像以使用PHP在电子邮件中使用?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2695字,纯文字阅读大概需要4分钟。
内容图文
![嵌入图像以使用PHP在电子邮件中使用?](/upload/InfoBanner/zyjiaocheng/696/80046baf0b9f435f8a34ab0c9b2cab95.jpg)
我知道如何将图像作为附件嵌入到电子邮件中,但这并不是我真正想做的.我要做的是将图像嵌入到邮件中,并在电子邮件本身的正文中使用它.是否可以这样做或以某种方式引用邮件中要使用的附件?
我还要为此担心吗?通过链接到网络上的映像来加载它们是否更有益?我当时在想这会减轻服务器的负担,因此不必在打开的每封电子邮件中都直接从我的站点下载它,从而稍微减少了带宽.
注意:我不是在寻找任何电子邮件框架或库等,只是为了一种简单的方法来完成此任务.
解决方法:
人们会告诉您使用SwiftMailer或其他一些库.但这并不难.这真的很挑剔,但并不难.以下是我使用的功能.我已经将其匿名化了,希望没有破坏任何内容.在html消息(我从另一个函数获得)中,以这种方式链接到您附加的图像:
$output .= '<p><img src="cid:' . $linkID . '" alt="graph" /></p>';
其中$linkID是以下函数中使用的相同值.
public function sendEmail($emailAddress)
{
$random_hash = md5(date('r', time()));
$htmlString = $this->getHTML($random_hash);
$message = "--mixed-$random_hash\n";
$message .= 'Content-Type: multipart/alternative; boundary="alt-' . $random_hash . '"' . "\n\n";
$message .= 'MIME-Version: 1.0' . "\n";
$message .= '--alt-' . $random_hash . "\n";
$message .= 'Content-Type: text/plain; charset="iso-8859-1"' . "\n";
$message .= 'Content-Transfer-Encoding: 7bit' . "\n\n";
// text message
$message .= strip_tags($htmlString) . "\n\n";
$message .= '--alt-' . $random_hash . "\n";
$message .= 'Content-Type: text/html; charset="iso-8859-1"' . "\n";
// $message .= 'MIME-Version: 1.0' . "\n";
$message .= 'Content-Transfer-Encoding: 7bit' . "\n\n";
// html message
$message .= $htmlString . "\n\n";
$message .= '--alt-' . $random_hash . '--' . "\n";
// graph image
if($this->selectedGraph != 0)
{
$graphString = $this->getGraph();
$graphString = chunk_split(base64_encode($graphString));
$linkID = 'graph-' . $random_hash . '-image';
$message .= '--mixed-' . $random_hash . "\n";
$message .= 'MIME-Version: 1.0' . "\n";
$message .= 'Content-Transfer-Encoding: base64' . "\n";
$message .= 'Content-ID: ' . $linkID . "\n";
$message .= 'Content-Type: image/gif; name="graph.gif"' . "\n";
$message .= 'Content-Disposition: attachment' . "\n\n";
$message .= $graphString;
$message .= '--mixed-' . $random_hash . '--' . "\n";
}
else
{
$message .= '--mixed-' . $random_hash . '--' . "\n";
}
$headers = 'From: ' . $this->from. "\r\nReply-To: " . $this->replyto;
$headers .= "\r\nContent-Type: multipart/related; boundary=\"mixed-" . $random_hash . "\"\r\nMIME-Version: 1.0";
$flags = '-f ' . BOUNCED_EMAIL_ADDRESS;
return mail($emailAddress, $this->subject, $message, $headers, $flags);
}
是否值得这样做是另一个问题.如果附加图像,则使用带宽来发送消息.也许最好一次全部使用带宽而不是分散使用带宽?也许不吧.
在必须将不同的图像发送给每个收件人之前,我没有使用附加的图像.我不会在我们的服务器上存储和管理所有这些图像(并尝试为其唯一命名).
内容总结
以上是互联网集市为您收集整理的嵌入图像以使用PHP在电子邮件中使用?全部内容,希望文章能够帮你解决嵌入图像以使用PHP在电子邮件中使用?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。