1 Reply Latest reply on May 29, 2013 6:24 AM by matoma167

    Sending an E-mail with attachment with PHP from Flex

    Vinzcent

      Hey,

       

      I've made a custom compontent wich mails your own drawings to you. But I have a problem to send an e-mail with attachment.

       

      I use the HttpService to send the data to the php-file, but I always get the Fault message (form phpFault()).

       

      This is my code in Flex:

      <mx:Script>
          <![CDATA[
              import mx.rpc.events.FaultEvent;
              import mx.rpc.events.ResultEvent;
              import mx.graphics.codec.PNGEncoder;
              import mx.events.ValidationResultEvent;
              import mx.controls.Alert;


              [Bindable]
              private var byteArray:ByteArray;

              private function mailImage(comp:DisplayObject):void
              {
                  var emailValidation:ValidationResultEvent = validEmail.validate();

                  if(emailValidation.type == ValidationResultEvent.INVALID)
                  {
                      Alert.show("Invalid E-mail");
                  }

                  else
                  {
                      var bitmapData:BitmapData = new BitmapData(comp.width, comp.height);
                      bitmapData.draw(comp);
                      var png:PNGEncoder = new PNGEncoder();
                      byteArray = png.encode(bitmapData);
                      httpMail.send();
                  }          
              }


              private function phpResult(evt:ResultEvent):void
              {
                  Alert.show("You've got mail.");
              }

              private function phpFault(evt:FaultEvent):void
              {
                  Alert.show("Something went wrong. /n" + evt.message.toString());
              }
          ]]>
      </mx:Script>

      <mx:EmailValidator id="validEmail" source="{ipEmail}" property="text"/>

      <mx:HTTPService id="httpMail" url="php/byte-receiver.php" method="POST" result="phpResult(event)" fault="phpFault(event)">
         
      <mx:request>
             
      <img>{byteArray}</img>
             
      <mail>{ipEmail.text}</mail>
         
      </mx:request>
      </mx:HTTPService>

      <mx:Label text="draw your own image" styleName="h1" x="10" y="0" width="493" height="60"/>

      <mx:Canvas
         
      id="drawCanvas"
         
      x="10" y="77"
         
      width="561" height="245"
         
      borderStyle="solid" borderColor="#A6A6A6">
      </mx:Canvas>

      <mx:Label x="10" y="329" text="Your e-mail:" styleName="text"/>

      <mx:TextInput
         
      id="ipEmail"
         
      x="86" y="324" width="417"/>   

      <mx:Label
         
      id="lblMailImage"
         
      x="10" y="383"
         
      text="Mail my image"
         
      click="mailImage(drawCanvas)"
         
      mouseOver="lblMailImage.setStyle('color',  '#00067b')"
         
      mouseOut="lblMailImage.setStyle('color',  '#717171')"
         
      styleName="button"/>
      </mx:Canvas>


      This is my PHP code


      <?php                 
      $fileatt_type
      = "application/octet-stream"; // File Type
      $fileatt_name
      = "ImgContact.png"; // Filename that will be used for the file as the attachment

      $email_from
      = "my@email.here"; //Who the email is from
      $email_subject
      = "Contact Winckelmans.net"; // The Subject of the email
      $email_message
      = "Mail send by winckelmans.net. Your drawing is in the attachment"; // Message that the email has in it

      $email_to
      = $_POST['mail']; // Who the email is too

      $headers
      = "From: $email_from";  

      $data
      = $_POST['img'];

      $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=\"iso-8859-1\"\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-Disposition: attachment;\n" .
                       
      //" filename=\"{$fileatt_name}\"\n" .
                       
      "Content-Transfer-Encoding: base64\n\n" .  
                       $data
      . "\n\n" .  
                       
      "--{$mime_boundary}--\n";  

      $mailsend
      = mail($email_to, $email_subject, $email_message, $headers);  

      echo $mailsend
      ;

      ?>

       

       


      This is the error I get in an Alert:

      (mx.messaging.messages::ErrorMessage)#0
        body
      = ""
        clientId
      = "DirectHTTPChannel0"
        correlationId
      = "F3C16CE1-65CF-E690-1907-D28293FD6BB9"
        destination
      = ""
        extendedData
      = (null)
        faultCode
      = "Server.Error.Request"
        faultDetail
      = "Error: [IOErrorEvent type="ioError" bubbles=false cancelable=false eventPhase=2 text="Error #2032: Stream Error. URL: php/byte-receiver.php"]. URL: php/byte-receiver.php"
        faultString
      = "HTTP request error"
        headers
      = (Object)#1
         
      DSStatusCode = 0
        messageId
      = "7A1DCDBE-0358-7E39-3AF8-D282945A7748"
        rootCause
      = (flash.events::IOErrorEvent)#2
          bubbles
      = false
          cancelable
      = false
          currentTarget
      = (flash.net::URLLoader)#3
            bytesLoaded
      = 0
            bytesTotal
      = 0
            data
      = ""
            dataFormat
      = "text"
          eventPhase
      = 2
          target
      = (flash.net::URLLoader)#3
          text
      = "Error #2032: Stream Error. URL: php/byte-receiver.php"
          type
      = "ioError"
        timestamp
      = 0
        timeToLive
      = 0

       

       

       

      Thanks in advance

      Vincent