如何使用电子邮件发送HTML5画布图像 [英] how to send HTML5 canvas image with email

查看:314
本文介绍了如何使用电子邮件发送HTML5画布图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我尝试用Pear的Mail_mime做它,但是我无法从我的共享主机提供商和cpanel加载课程。

First I try'd doing it with Mail_mime from Pear, but I can't load the class from my shared host provider and cpanel.

接下来我遵循指示从此帖
。它将图像写入temp文件夹,但它写入零字节。
显然问题是,我错过了什么。
在某处我读到关于在编码字符串中转换+符号,但我不确定?

Next I followed directions from this post .It writes the image to the "temp" folder but it writes zero bytes. Obviously the question is, what am I missing. Somewhere I read about converting + signs in the encoded string, but i'm not sure?

主要问题是如何发送画布图像与电子邮件,我没有一个完整的知识,为此而进行。我遵循一些帖子,我以为我需要在服务器端创建一个临时映像文件,以便将其附加到邮件内置邮件功能的电子邮件中。因为实际写入的0个字节,首先解决这个问题似乎是合乎逻辑的,邮件问题会自动解决。
从下面的评论来看,似乎首先创建一个图像并不是必需的,但是在我有一个可行的解决方案之前,我必须先调查一下。

另外,如果有人有另一个解决方案或php类发送图像在邮件
的正文,而不是一个附件

Also if anybody has another solution or php class to send the image in the body of the mail as opposed to an attachment

我发送这个ajax

var canvas = document.getElementById('doodle');
function serialize(canvas) {
    return canvas.toDataURL();
}

这是serverside

this is serverside

case 'doodle':
    $image = $_POST['doodle'];
    $data ="doedeliedoe";

    $email ="from@msn.com";
    $headers="From:".$email."\r\n";
    $headers .= "MIME-Version: 1.0\r\n"; 
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
    list($settings, $encoded_string) = explode(',', $image);
    list($img_type, $encoding_method) = explode(';', substr($settings, 5));

    if($encoding_method == 'base64'){

        $file=fopen("/home/user/public_html/user/temp/newLego.png",'w+');

       fwrite($file,base64_decode($encoded_string)) ;
       fclose($file);
    }
    $my_file = "newLego.png";
    $my_path = "/home/user/public_html/user/temp/";
    $my_subject = "My Design";
    $my_message = "Designed by ".$email;
   $this-> mail_attachment($my_file, $my_path, "mymail@gmail.com", $email, $email, $email,  $my_subject, $my_message);  



function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
    $file = $path.$filename;
    $file_size = filesize($file);
    $handle = fopen($file, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $content = chunk_split(base64_encode($content));
    $uid = md5(uniqid(time()));
    $name = basename($file);
    $header = "From: ".$from_name." <".$from_mail.">\r\n";
    $header .= "Reply-To: ".$replyto."\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= $message."\r\n\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $header .= $content."\r\n\r\n";
    $header .= "--".$uid."--";
    if (mail($mailto, $subject, "", $header)) {
        //echo "mail send ... OK"; // or use booleans here
    } else {
       // echo "mail send ... ERROR!";
    }
}   

谢谢Richard

推荐答案

你应该首先更好地让自己感到你所面对的问题。是否是:

You should first of all better make yourself comfortable with the problem you face. Is it either:


  1. 将画布转换为图像文件  - 或 -

  2. 将图像文件附加到外发邮件

  1. Converting a canvas to an image "file"   -or-
  2. Attaching an image "file" to an outgoing mail message

你不能做2.在你的情况下,没有做1,但是2.可以独立于1 ..

You would not be able to do 2. in your scenario without doing 1., however 2. can be solved independently of 1..

我建议你要么问一个或另一个。要整合两者,实际上是很直接的:

I suggest you either ask the one or the other. To integrate both would be actually quite straight forward:


  • 如果你有一个只接受附件文件的邮件库,你需要创建一个临时文件。您可以使用 SplTempFileObject 在PHP中创建临时文件。

  • 如果您有一个邮件库接受附件的二进制字符串,您只需传递二进制字符串。

  • 如果需要调试是否从画布位图数据创建文件确实有效,您应该保存到一个文件并测试文件是否是一个图像(这最容易手动完成,但是这个可以自动化)

  • If you have a mail library that only accepts files for attachment, you would need to create a temporary file. You can create temporary files in PHP with the SplTempFileObject quite easily.
  • If you have a mail library that accepts a binary string for an attachment, you can just pass the binary string.
  • If you need to debug whether or not creating a file out of the canvas bitmap data did work, you should save to a file and the test if the file is an image (this is most easily done manually, however this can be automated, too)

对不起,如果这个答案更像一个评论,那只是我在实际提供代码方面没有多少用处因为问题是很多分段。

Sorry if this answer is more like a comment, it's just that I do not find much use in actually providing code as the question is that much segmented.

这篇关于如何使用电子邮件发送HTML5画布图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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