2

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

Community
  • 1
  • 1
Richard
  • 4,516
  • 11
  • 60
  • 87

1 Answers1

3

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

  1. Converting a canvas to an image "file"   -or-
  2. 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.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • I do not understand completely what you are saying and I have been searching the posts over here, obviously you think to know more about the subject. What mail library, besides the standard mail..I don't know? Like I said I have try'd Pear. You say I need one or the other..don't know why...I think I have read all the posts about mailing canvas images and that did not come up..if this seems to obvious for you..then please enlighten me some more..because I can not find anymore usefull posts.. – Richard Sep 29 '12 at 09:50
  • you don't inmediatly have to give code, but more help would be appreciated..I have been going with this for a day already..but that's not really an issue, and the main problem I have is ofcourse in the subject of the post..anything I do wrong is for others to review – Richard Sep 29 '12 at 09:52
  • 1
    A mail library that has support for attachments both via the file-system as well via memory (strings) is [Phpmailer](http://code.google.com/a/apache-extras.org/p/phpmailer/), it is fairly easy to integrate if you do not use any package manager. Otherwise another really good one is Swiftmailer. – hakre Sep 29 '12 at 10:06
  • 1
    Try to pinpoint your major issue first. Is it generating a file out of a canvas or is it making the attachment. That has not much to do with knowledge, just how to divide an overall problem to break it apart into smaller one. Smaller problems are easier to solve and if you solved all small problems, the big problem is solved, too. It's just that. – hakre Sep 29 '12 at 10:07
  • I look into that, I already downloded phpMailer, but I wanted to try this code first, cause it seemed simple enough, I also thought you needed to be in control of the server for phpmailer..anyway I keep on reading.. – Richard Sep 29 '12 at 10:11