我怎么可以在邮件正文发送的base64图像用PHP? [英] How can I send an base64 image on a mail body with PHP?
问题描述
我试图用图像中的base64发送电子邮件的身体使用下面的code PHP,但从来没有出现图像...如果我改变它的作品的URL,但它不 IMG SRC =&的base64 GT; t用的base64 ......我只能用&LT测试一个新的页面上的base64
和工作太......我在想什么?
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);
?>
下面是我的base64形象的例子: http://jsfiddle.net/28nP4/
Here is my base64 image example: http://jsfiddle.net/28nP4/
推荐答案
我尝试不同的东西,我发现被上传图像和获取URL的唯一途径,我得到了此链接:http://j-query.blogspot.in/2011/02/save-base64-en$c$cd-canvas-image-to-png.html
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
这是非常简单的:
<?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,这样,而不是使用&LT; IMG SRC =[imageFromOtherPage']'。$ _ POST'。/&GT;
,我使用生成的网址。完美地工作!
And this generates an URL, so, instead of using <img src="'.$_POST['imageFromOtherPage'].'"/>
, I use the generated URL. Worked perfectly!
这篇关于我怎么可以在邮件正文发送的base64图像用PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!