PHP邮件附件 - 画布图像空白

4
更新:我接收到的图片是一个空白的图片,它与我的画布大小一样。它不是来自画布的图像。我能够将新画布PNG插入到DOM中,所以我知道图像数据没问题。
进一步:在它发送到PHP表单之前,我可以复制图像数据,将该代码粘贴到另一页的< img >标签中,作为它的src,然后正确的图像就会显示!非常接近成功!
我的用户在HTML5画布上创建了一个图像。我需要将该图像作为电子邮件附件发送给用户。我可以使用jquery AJAX捕获图像并将其POST到PHP页面。
是的,我已经阅读了十几个相关的帖子,这就是我到达这一步的方法。不,没有一个回答了我的问题-这部分总是被跳过的。不,我不想使用PHPMailer(可怕的文档)或任何其他类。这可以使用PHP mail() 实现。而且我已经很接近了。
以下是我的Web应用程序中的Javascript代码:
var data = "to=" + to + "&subject=" + subject;          
var canvas2 = document.getElementById("canvas");
var strDataURI = canvas2.toDataURL(); 
data = data + "&attachment="  + strDataURI;

$.ajax({
    type: "POST",
    url: "mail2.php",
    data: data,
    success: function(){

        $("#loading").fadeOut(100).hide();
        $('#message-sent').fadeIn(500).show();               
   }
});

发送这些数据:

 "to=erik@mydomain.com&subject=test&attachment=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA3gAAAHBCAYAAAA7NBnaAAA..."

对于这个PHP页面(更新,更简单):

<?PHP    
$to =   $_REQUEST['to'];
$subject =  'PHP Mail Attachment Test';
$bound_text =   "jimmyP123";
$bound =    "--".$bound_text."\r\n";
$bound_last =   "--".$bound_text."--\r\n";


$attachment = $_REQUEST['attachment'];
$attachment =  substr($attachment,strpos($attachment,",")+1);
$attachment = chunk_split($attachment);



$headers =  "From: admin@server.com\r\n";
$headers .=     "MIME-Version: 1.0\r\n"
."Content-Type: multipart/mixed; boundary=\"$bound_text\"";

$message .=     "If you can see this MIME than your client doesn't accept MIME types!\r\n"
.$bound;

$message .=     "Content-Type: text/html; charset=\"iso-8859-1\"\r\n"
."Content-Transfer-Encoding: 7bit\r\n\r\n"
."hey my <b>good</b> friend here is a picture of regal beagle\r\n"
.$bound;

//$file =   file_get_contents("http://www.litfuel.net/php/regal_004.jpg");

$message .=     "Content-Type: image/png; name=\"index.png\"\r\n"
."Content-Transfer-Encoding: base64\r\n"
."Content-disposition: attachment; file=\"index.png\"\r\n"
."\r\n"
.$attachment
//.chunk_split(base64_encode($file))
.$bound_last;

if(mail($to, $subject, $message, $headers))
{
     echo 'MAIL SENT';
} else {
     echo 'MAIL FAILED';
}

?>

你能否也发布结果邮件(当然是纯文本格式)? - wnrph
无论如何,你能否以纯文本形式发布它? - wnrph
1
不要使用输出缓冲,尝试使用heredoc。http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc - thomas-peter
1
您的图像数据应该只是base64编码的数据。 - aknosis
因此,修剪掉它可以帮助解决问题。但是,我得到了一张空白图片。这不是画布上的图像,而是其背景颜色。这是我用于在“电子邮件”页面上显示图像的相同数据(变量),因此我知道创建了一个“平面”图像,我可以在屏幕上看到它。 - Slopeside Creative
显示剩余2条评论
1个回答

0

我注意到你正在使用heredoc。这可能会导致问题。

电子邮件中的行结尾必须是\r\n(CRLF),因此我建议改用字符串连接方式重写它,然后它应该可以正常工作。

另外,请确保对$attachment应用chunk_split()以保持行长度<= 76。


谢谢你的输入,杰克。我会在几个小时后尝试并回报结果。 - Slopeside Creative
更近了。问题在于将长字符串从浏览器传递到PHP页面时保持完整。有些地方出了问题。 - Slopeside Creative
没有运气。数据在ajax提交之前已经进行了base64编码。我会回到这个问题,现在我将尽可能使用标准的PHPmailer路线,尽管我不想先将图像保存到服务器上 - 我知道这可以做到,并且需要尽快完成。@Jack - Slopeside Creative
某处,数据字符串在途中被打断了。随着时间的推移,我会找到它并使其正常工作。现在,即使这意味着将图像存储在服务器上,我也需要启动作业。感谢您的输入,帮助我走到了这一步/接近目标。@Jack - Slopeside Creative
整个字符串都在,但是字符串中有空隙。不仅仅是空格,而是空隙。所有的数据都在,只是有空隙。我听说加号“+”可以帮助连接它们或者做些什么? - Slopeside Creative
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接