PHP Email Attachment - BLANK Canvas Image

UPDATE: the image that I get is an empty image, the size of my canvas. This is not a canvas image. I can insert a new PNG PNG into the DOM, so I know that the image data is good.

NEXT: I can copy the image data before submitting it to the PHP form, paste this code into a tag on another page, as its src and the correct image will be displayed! SO CLOSE HERE!

My user creates an image on HTML5 canvas. I need to send an image as an email application to the user. I can capture an image and submit it to a PHP page using jquery AJAX.

Yes, I read about a dozen other related posts, and that is how I got to this point. No - not one of them answers my question - this part is always skipped. NO - I do not want to use PHPMailer (terrifying documentation) or any other class. This is possible using PHP mail (). And I'm close.

This is javascript in my web application:

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(); } }); 

MAIL DATA:

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

To this PHP page (updated, easier):

 <?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'; } ?> 
+4
source share
1 answer

I noticed that you are using heredoc. This can cause problems here.

Line endings in letters MUST be \r\n (CRLF), so I would suggest rewriting it using string concatenation, and then it should work.

Also, make sure you put chunk_split() on $attachment to keep the line length <= 76.

0
source

Source: https://habr.com/ru/post/1436649/


All Articles