在没有物理图像文件的情况下将图像嵌入邮件 [英] Embed Image in Mail without having physical Image File

查看:143
本文介绍了在没有物理图像文件的情况下将图像嵌入邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google Maps Static API以PNG的形式请求图片。目前我将这个图像保存到文件系统,然后我将这个图像与PHPMailer发送给我的客户。邮件发送完毕后,图像将从文件系统中删除。



现在我的意图是保存将图像保存到文件系统的步骤,并使用内存中的图像对象并通过邮件发送。但似乎PHPMailer必须在Filesystem上有一个Image来使用它。



我现在做的是:

从静态API请求图片

  $ URL =http://maps.googleapis.com /地图/ API / staticmap中心=奥尔巴尼,+ NY&安培;变焦= 13&安培;比例=假安培;大小= 600×300&安培;地图类型=路线&安培;格式= PNG&安培; visual_refresh =真; 
$ image = file_get_contents($ URL);

将图像保存到文件系统并将其发送到我的邮件功能

  $ fp = fopen('../../ img / staticMap / staticMap.png','w +'); 

fputs($ fp,$ image);
fclose($ fp);
unset($ image);

$ file =../../img/staticMap/staticMap.png;
$ notifications-> notifySingleUser($ file); //调用我的邮件功能

在邮件中嵌入图片

  $ mail = new PHPMailer(); 
$ mail-> CharSet ='UTF-8';
$ mail-> IsHTML(true);
$ mail-> AddEmbeddedImage($ file,staticMap);
$ mail-> Body =< img width =600height =300src =cid:staticMap>;

我也做了一些调查 PHPMailer附件,无需物理文件,但在这里他们只是在说怎么用AddAttachment来做,但我想添加到AddEmbeddedImage。我也尝试过,邮件发送时没有任何错误,但没有任何图像。



这甚至可能吗?如果是的话,我怎么能将它归档? 解决方案

它总是有助于阅读文档。您正在寻找的功能是 addStringEmbeddedImage ,它完全符合您的要求:

  $ URL =http://maps.googleapis的.com /地图/ API / staticmap中心=奥尔巴尼,+ NY&安培;变焦= 13&安培;比例=假安培;大小= 600×300&安培;地图类型=路线&安培;格式= PNG&安培; visual_refresh =真; 
$ image = file_get_contents($ URL);
$ mail-> addStringEmbeddedImage($ image,'staticMap','map.png','base64','image / png');
$ mail-> Body ='< img width =600height =300src =cid:staticMap>';

使用嵌入式图像确实有优点和缺点,但数据url具有特别差的客户端兼容性,所以通常是最糟糕的选择。动态链接到图像可能不是可靠的谷歌地图,所以你可能想代理和缓存直接地图查找。这也比每次点击API要快得多,而且如果API不响应或缓慢,则可以提供更老的版本,这也更加可靠。


I´m using Google Maps Static API to request an Image as PNG. Currently I´m saving this Image to the Filesystem and afterwards I´m sending this Image with PHPMailer to my clients. After all Mails are send, the Image is removed from the Filesystem.

My intention is now to save me the step of saving the Image to the Filesystem and just use the Image Object from Memory and send it via Mail. But it seems that PHPMailer must have an Image on the Filesystem to use it.

What I currently do:

Request Image from Static API

 $URL = "http://maps.googleapis.com/maps/api/staticmap?center=Albany,+NY&zoom=13&scale=false&size=600x300&maptype=roadmap&format=png&visual_refresh=true";
 $image = file_get_contents($URL);

Save Image to Filesystem and send it to my Mail Function

 $fp  = fopen('../../img/staticMap/staticMap.png', 'w+');

 fputs($fp, $image);
 fclose($fp);
 unset($image);

 $file = "../../img/staticMap/staticMap.png";
 $notifications->notifySingleUser($file); //Call my Mail Function

Embed Image in Mail

$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->IsHTML(true);
$mail->AddEmbeddedImage($file, "staticMap");
$mail->Body = "<img width="600" height="300" src="cid:staticMap">";

I´ve also done some research PHPMailer attachment, doing it without a physical file, but here they are just talking how to do it with AddAttachment, but I want to AddEmbeddedImage. I´ve also tried that, the Mail is send without any error´s, but with NO image in it.

Is this even possible? If yes, how could I archive it?

解决方案

It always helps to read the docs. The function you're looking for is addStringEmbeddedImage, which does exactly what you want:

$URL = "http://maps.googleapis.com/maps/api/staticmap?center=Albany,+NY&zoom=13&scale=false&size=600x300&maptype=roadmap&format=png&visual_refresh=true";
$image = file_get_contents($URL);
$mail->addStringEmbeddedImage($image, 'staticMap', 'map.png', 'base64', 'image/png');
$mail->Body = '<img width="600" height="300" src="cid:staticMap">';

There are indeed pros and cons to using embedded images, but data urls have especially poor client compatibility, so are generally the worst option. Dynamically linking to the image may not be reliable with google maps, so you may want to proxy and cache the direct map lookups. This will also be much faster than hitting the API every time, and also more reliable as you can deliver an older version if the API doesn't respond or is slow.

这篇关于在没有物理图像文件的情况下将图像嵌入邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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