问题
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.
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?
The main problem is how to send the canvas image with email, I don't have a complete knowledge for going about that. I followed some posts and I thought I needed to create a temporary image file on the serverside to be able to attach it to an email with the build in mail function. Because 0 bytes where actually written, it seemed logical to solve that problem first and the mail problem would solve itself. From the comments below, it seems creating an image first is not neccesary, but I have to investigate some more before I have a workable solution..
Also if anybody has another solution or php class to send the image in the body of the mail as opposed to an attachment
I send this with ajax
var canvas = document.getElementById('doodle');
function serialize(canvas) {
return canvas.toDataURL();
}
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!";
}
}
thanks, Richard
回答1:
You should first of all better make yourself comfortable with the problem you face. Is it either:
- Converting a canvas to an image "file" -or-
- Attaching an image "file" to an outgoing mail message
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:
- 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.
来源:https://stackoverflow.com/questions/12651504/how-to-send-html5-canvas-image-with-email