【发布时间】:2011-05-20 17:48:02
【问题描述】:
更新:查看消息底部
大家好,问题是: 我正在尝试在 Flash 中拍摄影片剪辑的“屏幕截图”,使用 AS Core Lib JPGEncoder 类将其编码为 Jpg,然后 POST 将其提交给 PHP,并将图像嵌入到 MIME 编码的电子邮件中。
目前,我已经测试了将编码图像保存在本地,并且可以正常工作,因此编码器肯定可以正常工作。电子邮件发送,它有一个 100kb 的 jpg 附件,但是,该图像似乎包含错误数据,因为它无法在任何应用程序中正确打开。
这是动作脚本:
trace("Sending Email");
var rootMC:MovieClip = MovieClip(root);
var data1:BitmapData = new BitmapData(rootMC.width, rootMC.height);
data1.draw(rootMC);
var en:JPGEncoder = new JPGEncoder(80);
var bArray:ByteArray= en.encode(data1);
var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
var request:URLRequest = new URLRequest();
request.requestHeaders.push(header);
request.url = mailLoc;//MailLoc is the URL of the PHP.
request.method = URLRequestMethod.POST;
request.data = bArray;
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, MailCompleteHandler);
try
{
loader.load(request);
}
catch(error:Error)
{
trace("Unable to load URL");
}
这里是 PHP:
require_once 'lib/swift_required.php';
$image = file_get_contents("php://input");
$attachment = SwiftAttachment::newInstance($image, 'submission.jpg', 'image/jpg');//<--This line stuffs it
$message = Swift_Message::newInstance()
/*Give the message a subject*/
->setSubject('Your subject')
/*Set the from address with an associative array*/
->setFrom(array('info@battleforbrisbane.com.au'=>'Battle for Brisbane'))
/*Set the to addresses with an associative array*/
->setTo(array('jordaanm@gmail.com'))
/*Give it a body*/
->setBody('Congratulations! You submission to Battle for Brisbane was received');
$message->attach($attachment);//<--When the attachment above is commented out, so is this
$transport = Swift_SendmailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
$mailer->send($message);
更新:我现在使用 SwiftMailer 而不是手动写出 MIME。但是,这是新的交易:在 php 代码中,我标记了一行,将 POSTed 图像数据作为 jpg 附加到电子邮件中。如果我注释掉这一行和 message->attach 行,那么每个发送都很好。但是,如果未注释它们,则不会发送任何电子邮件,这使我相信尝试从提供的数据创建 jpg 附件会导致问题。 这一切只是证实了我的怀疑,即 PHP 脚本接收到的数据不正确。多么美好又令人沮丧。
【问题讨论】:
-
由于您可以将图像保存在服务器上并查看它,因此问题与 Flash 无关,除了 chunk_split/base64 编码之外,当您保存图像与通过电子邮件发送图像时,还有什么不同?您是否尝试过使用保存在服务器上的 JPG 进行相同的电子邮件发送程序?