0

我希望我的访问者能够将我的应用程序中的创作作为附件发送给朋友(就像创作的屏幕截图)。我可以让 Flex 和 PHP 进行通信并发送电子邮件,但我无法让图像发送。它发送一个 2o 或更小的附件并且无法打开(当然!)。

我的应用程序没有返回错误

我发现下面链接的这些帖子对我有帮助,但我仍然不知道如何将图像从 Flex 传递到 PHP。

这是我来自 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>

这是我的 PHP 代码:

$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);

玛丽

4

1 回答 1

0

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 !

于 2013-05-30T12:30:25.280 回答