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