使用Laravel发送Base64图像

编程入门 行业动态 更新时间:2024-10-22 17:28:03
本文介绍了使用Laravel发送Base64图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用Laravel发送电子邮件,纯文本效果很好.但是,我在将图像附加到电子邮件时遇到问题.到目前为止,图像已作为Base64字符串存储在数据库中.

I am using Laravel to send emails and plain text works fine. However, I am having an issue attaching images to the email. As of right now, images are stored in the DB as Base64 Strings.

这是在Laravel中调用Mail门面的代码:

This is the code that calls the Mail facade in Laravel:

Mail::queue('emails.infraction', $data, function($message) use ($email){ $message -> from($fromEmail, 'Blah); $message -> to($toEmail) -> subject('Testing'); });

基本上,$ data变量包含我将在发送的HTML电子邮件中显示的所有信息.在电子邮件中显示变量时,我可以很好地访问变量,但是在将base64图像附加到电子邮件中时遇到问题.

Basically the $data variable contains all the information I will be displaying in the HTML email that is being sent. I can access the variables just fine when displaying them in the email but I am having issues attaching the base64 images into the email.

这是我到目前为止尝试过的:

This is what I have tried so far:

Evidence of Intraction:</p> <?php $elementCount = count($photos); for($x=0; $x<$elementCount; $x++){ echo '<p style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; margin-left: 80px;">'; echo "<img src=" . $message->embedData($data['photos'][$x], 'Blah') . ">"; echo "</p>"; } ?>

但是正如您在此处看到的,gmail无法将rawDataString正确编码为图像:

But as you can see here, gmail does not properly encode that rawDataString into the image:

它只是显示好像有图片,而不显示内容.我已经验证了基本的64位字符串,它是正确的.

It just displays as if a picture was there but not the contents. I have verified the base 64 string and it is correct.

可能是什么错误?

Laravel邮件文档说在消息生成器中使用以下内容:

Laravel Mail Documentation says to use something like this in the message builder:

但是我对如何正确地执行操作有些迷茫.另一个选择是将base64字符串即时转换为图像并将其作为附件发送给用户,但是我无法将图像存储到服务器中,因此必须将其转换为php中的图像并通过发送没有实际文件路径的laravel mailer.

but I am a little lost on how to do that properly. The other option would be to convert the base64 string into an image on the fly and send it as an attachment to the user but I cannot store the images into the server and thus would have to convert it to an image within php and sending it through laravel mailer without an actual filepath.

请告知

推荐答案

您必须先对图像进行解码,如下所示:

You have to decode the image first like this:

$message->embedData(base64_decode($data['photos'][$x]), 'Blah')

更多推荐

使用Laravel发送Base64图像

本文发布于:2023-07-27 00:13:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219175.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   Laravel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!