如何使用PHP在邮件正文上发送base64图像?(How can I send an base64 image on a mail body with PHP?)

编程入门 行业动态 更新时间:2024-10-27 21:20:17
如何使用PHP在邮件正文上发送base64图像?(How can I send an base64 image on a mail body with PHP?)

我正在尝试使用下面的代码使用PHP在body64上发送带有base64图像的电子邮件,但图像永远不会出现...如果我更改为URL它可以工作,但它不适用于base64 ..我只用<img src=base64>在新页面上测试了base64,并且也工作了......我错过了什么?

<?php // recipients $to = $_POST['email']; // subject $subject = 'Test'; // message $message = ' <html> <head> <title>Test</title> </head> <body> <img src="'.$_POST['imageFromOtherPage'].'"/> </body> </html> '; // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // Mail it mail($to, $subject, $message, $headers); ?>

这是我的base64图像示例: http : //jsfiddle.net/28nP4/

I'm trying to send an email with an image in base64 on the body with PHP using the code below, but the image never appears... If I change to an URL it works, but it doesn't with the base64... I tested the base64 on a new page only with <img src=base64> and worked too... What am I missing??

<?php // recipients $to = $_POST['email']; // subject $subject = 'Test'; // message $message = ' <html> <head> <title>Test</title> </head> <body> <img src="'.$_POST['imageFromOtherPage'].'"/> </body> </html> '; // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // Mail it mail($to, $subject, $message, $headers); ?>

Here is my base64 image example: http://jsfiddle.net/28nP4/

最满意答案

我尝试了不同的东西,我找到的唯一方法是上传图片并获取URL,我从这个链接获得了这个: http : //j-query.blogspot.in/2011/02/save-base64-encoded-canvas-图像到png.html

这很简单:

<?php // requires php5 define('UPLOAD_DIR', 'images/'); $img = $_POST['img']; $img = str_replace('data:image/png;base64,', '', $img); $img = str_replace(' ', '+', $img); $data = base64_decode($img); $file = UPLOAD_DIR . uniqid() . '.png'; $success = file_put_contents($file, $data); print $success ? $file : 'Unable to save the file.'; ?>

这会生成一个URL,因此,我使用生成的URL,而不是使用<img src="'.$_POST['imageFromOtherPage'].'"/> 。 工作得很完美!

I tried different things and the only way I found was uploading the image and getting the URL, I got that from this link: http://j-query.blogspot.in/2011/02/save-base64-encoded-canvas-image-to-png.html

It is very simple:

<?php // requires php5 define('UPLOAD_DIR', 'images/'); $img = $_POST['img']; $img = str_replace('data:image/png;base64,', '', $img); $img = str_replace(' ', '+', $img); $data = base64_decode($img); $file = UPLOAD_DIR . uniqid() . '.png'; $success = file_put_contents($file, $data); print $success ? $file : 'Unable to save the file.'; ?>

And this generates an URL, so, instead of using <img src="'.$_POST['imageFromOtherPage'].'"/>, I use the generated URL. Worked perfectly!

更多推荐

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

发布评论

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

>www.elefans.com

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