1 Reply Latest reply on May 30, 2013 5:56 AM by matoma167

    Flex & PHP - Send email attachment

    matoma167

      I'ld like for my visitor to be able to send an email to a friend with the creation from my application as an attatchment (so like a screencap of the creation). I can get Flex and PHP to communicate and send an email, but I can't get the image to send. It sends an attachment that is 2o or less and can't be opened (of course !).

       

      I don't get an error return from my application

       

      I found these posts linked below which helped me, but I still don't get how to pass the image from Flex to PHP.

      Here is my code from Flex :

      private function sendMail():void{
                  var _nomSTF:String = nomSTF.text;
                  var _emailSTF:String = emailSTF.text;
                  var _emailFriendSTF:String = emailFriendSTF.text;
                  var _sujetSTF:String = sujetSTF.text;
                  var _messageSTF:String = messageSTF.text;
      
                  //IMAGE
                  var fr:FileReference = new FileReference();
                  var encoder:PNGEncoder = new PNGEncoder();
      
                  var screenshotData:BitmapData = new BitmapData(conteneurDeMonImage.width, conteneurDeMonImage.height, true, 0xFFFFFF);
      
                  screenshotData.draw(conteneurDeMonImage);
                  //var outputData:ByteArray = encoder.encode(screenshotData);
                  //var base64:Base64Encoder = new Base64Encoder();
                  var png:PNGEncoder = new PNGEncoder();
                  byteArray = png.encode(screenshotData);
      
                  //base64.encodeBytes(outputData);
                  //FIN IMAGE
      
      
                  var evValidMail:ValidationResultEvent = emailSTFValidator.validate();
                  var evValidMailFriend:ValidationResultEvent = emailFriendSTFValidator.validate();
                  var evValidName:ValidationResultEvent = nomSTFValidator.validate();
      
                  if (evValidMail.type == ValidationResultEvent.VALID
                      && evValidName.type == ValidationResultEvent.VALID
                      && evValidMailFriend.type == ValidationResultEvent.VALID){
                      emailService.send({senderName: _nomSTF, senderEmail:_emailSTF, emailFriendSTF: _emailFriendSTF,
                          emailSubject:_sujetSTF, emailMessage: _messageSTF, file : byteArray});
                  } else {
                      resultLabel.text="There are Form errors";
                      resultLabel.setStyle("styleName", "invalid");
                  }
              }
      
              private function emailResultSTF():void{
                  Alert.show("Votre message a bien été envoyé");
                  containerSendToFriend.visible = false;
              }
      <fx:Declarations>
      
          <s:HTTPService id="emailService" url="src/mail.php" method="POST"
                          resultFormat="xml" useProxy="false"
                          result="emailResultSTF()"
                          fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail + '\n' + event.message.toString())"/>
      
      </fx:Declarations>
      

       

       

      And here's my PHP code :

       

      $fileatt_type = "application/octet-stream"; 
      $fileatt_name = "baignoireElyseeConcept.png";
      $sender_name =  $_POST['senderName'];
      $email_from = $_POST['senderEmail']; 
      $email_subject = $_POST['emailSubject'];  
      $email_message = $_POST['emailMessage'];  
      
      
      $email_to = $_POST['emailFriendSTF']; 
      
      
      $headers = 'From:'.$sender_name.'<'.$email_from.'>';   
      
      
      $data= $_POST['file'];
      
      
      $semi_rand = md5(time());   
      $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";   
      
      
      $headers .= "\nMIME-Version: 1.0\n" .   
                  "Content-Type: multipart/mixed;\n" .   
                  " boundary=\"{$mime_boundary}\"";   
      
      
      $email_message = "This is a multi-part message in MIME format.\n\n" .   
                      "--{$mime_boundary}\n" .   
                      "Content-Type:text/html; charset=\"utf-8\"\n" .   
                     "Content-Transfer-Encoding: 7bit\n\n" .   
      $email_message . "\n\n";   
      
      
      
      
      $email_message .= "--{$mime_boundary}\n" .   
                        "Content-Type: {$fileatt_type};\n" .   
                        " name=\"{$fileatt_name}\"\n" .
                        "Content-Transfer-Encoding: base64\n\n" .   
                       $data . "\n\n" .   
                        "--{$mime_boundary}--\n";   
      
      
      $mailsend = mail($email_to, $email_subject, $email_message, $headers);
      
      

       

       

      Thanks for your help

        • 1. Re: Flex & PHP - Send email attachment
          matoma167 Level 1

          I've looked around for an answer for this for days and finally got it to work so here's how I did it (helped by a lot of ressources on the internet and specifically this one : http://j-query.blogspot.com/2011/02/save-base64-encoded-canvas-image-to-png.html)

           

           

          here's my Flex code for my image :

           

           

             

          var je:JPEGEncoder = new JPEGEncoder(100); 
          
              var bd:BitmapData = new BitmapData(conteneurDeMonImage.width,conteneurDeMonImage.height);
              bd.draw(conteneurDeMonImage);
          
              var ba:ByteArray = je.encode(bd);
          
              var be:Base64Encoder = new Base64Encoder();
              be.encodeBytes(ba);
          
              var encodedData:String = be.toString();
          
                                                  // Testing if my base64 is working in FLEX
                                                  /*var byteArr:ByteArray;
                                                  var B64dec:Base64Decoder = new Base64Decoder();
                                                  B64dec.decode(encodedData);
                                                  byteArr = B64dec.toByteArray();
                                                  imageEnGrand.load(byteArr);*/
          
              emailService.send({senderName: _nomSTF, senderEmail:_emailSTF, emailFriendSTF: _emailFriendSTF, emailSubject:_sujetSTF, emailMessage: _messageSTF, file : encodedData});
          

           

          Here's my PHP code :

           

           

             

          $fileatt_type = "application/octet-stream"; 
              $fileatt_name = "test.jpg"; 
          
              $sender_name =  $_POST['senderName'];
              $email_from = $_POST['senderEmail']; 
              $email_subject = $_POST['emailSubject'];  
              $email_message = $_POST['emailMessage'];  
          
              $email_to = $_POST['emailFriendSTF']; 
          
              $headers = 'From:'.$sender_name.'<'.$email_from.'>';   
          
              $data = $_POST['file'];
          
              $data = str_replace('data:image/png;base64,', '', $data);
              $data = str_replace(' ', '+', $data);
          
              $img_data = base64_decode($data);
              $png = chunk_split(base64_encode(($img_data)));
          
              $semi_rand = md5(time());   
              $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";   
          
              $headers .= "\nMIME-Version: 1.0\n"."Content-Type: multipart/mixed;\n"." boundary=\"{$mime_boundary}\"";   
          
              $email_message =
                                                      "This is a multi-part message in MIME format.\n\n".
                                                      "--{$mime_boundary}\n"."Content-Type:text/html; charset=\"utf-8\"\n".
                                                      "Content-Transfer-Encoding: 7bit\n\n".$email_message."\n\n";
          
              $email_message .= "--{$mime_boundary}\n" .   
                                "Content-Type: {$fileatt_type};\n" .   
                                " name=\"{$fileatt_name}\"\n" .
                                "Content-Transfer-Encoding: base64\n\n" .   
                               $png . "\n\n" .   
                                "--{$mime_boundary}--\n";   
          
              $mailsend = mail($email_to, $email_subject, $email_message, $headers);
          

           

          I hope it helps someone who has the same problem as me !