Tuesday 22 November 2016

EMAIL FORM IN AS3 AND PHP WITH INFINITE ATTACHMENTS

 EMAIL FORM IN AS3 AND PHP WITH INFINITE ATTACHMENTS
Build an email form in AS3 and PHP with infinite attachements

 email form in AS3
 EMAIL FORM IN AS3 AND PHP WITH INFINITE ATTACHMENTS
 EMAIL FORM IN AS3 AND PHP WITH INFINITE ATTACHMENTS

000WEB HOSTING PHP

------------------------------------------------------------------------------------------------------
READ MORE:
http://www.aymericlamboley.fr/blog/build-an-email-form-in-as3-and-php-with-infinite-attachements/
--------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------
BEFORE SEND MESSAGE
1) FIRST SOVLE SMTP PROBLEM 
SMTP AUTHENTICATION EXCEPTION IN VB
2)  IMAP ENABLED
3) CORRECT GMAIL ADDRESS IN CODE
4) CORRECT THIS GMAIL PASSWORD IN CODE
--------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------
USEFUL IMAGES:
https://www.google.com/settings/security/lesssecureapps
https://mail.google.com/mail/u/0/#settings/fwdandpop
------------------------------------------------------------------------------------------------------------------
https://www.google.com/settings/security/lesssecureapps

https://mail.google.com/mail/u/0/#settings/fwdandpop


--------------------------------------------------------------------------------------------------------------
Main.as  CODE
--------------------------------------------------------------------------------------------------------------
package{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.StageScaleMode;
import flash.display.StageAlign;
import flash.events.Event;
import flash.events.DataEvent;
import flash.events.MouseEvent;
import flash.events.ProgressEvent;
import flash.net.FileReference;
import flash.net.FileReferenceList;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
import flash.net.URLLoaderDataFormat;
public class Main extends Sprite{
var pendingFiles:Vector.<FileReference>;
var uniqueFileNames:Vector.<String>;
var myFileReferenceList:FileReferenceList;
var attachedFile:MovieClip;
var attachedList:Vector.<MovieClip>;
var emailRequest:URLRequest;
var uploadRequest:URLRequest;
var variables:URLVariables;
var loader:URLLoader;
var filesAttached:Boolean = false;
var readyToSend:Boolean = true;
var nbFilesUploaded:uint = 0;
var nbCurrentFiles:uint;
var nbTotalFiles:uint;
var MAX_SIZE:uint = 2000 * 1024;
public function Main() {
if (stage) init();
else this.addEventListener(Event.ADDED_TO_STAGE, init);
}
function init(e:Event = null):void{
if (e != null) this.removeEventListener(Event.ADDED_TO_STAGE, init);
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
uploadRequest = new URLRequest("https://fbigadgets.000webhostapp.com/upload.php");
uploadRequest.method = URLRequestMethod.POST;
myFileReferenceList = new FileReferenceList();
pendingFiles = new Vector.<FileReference>();
attachedList = new Vector.<MovieClip>();
uniqueFileNames = new Vector.<String>();
initFormListeners();
stage.focus = subject;
}
function initFormListeners():void{
myFileReferenceList.addEventListener(Event.SELECT, onSelect);
_send.buttonMode = true;
_send.addEventListener(MouseEvent.CLICK, sendHandler);
_attachement.buttonMode = true;
_attachement.addEventListener(MouseEvent.CLICK, browseFiles);
}
function browseFiles(e:MouseEvent):void{
myFileReferenceList.browse();
}
function onSelect(e:Event):void{
var file:FileReference;
nbTotalFiles = myFileReferenceList.fileList.length + nbCurrentFiles;
for (var i:uint = 0; i < myFileReferenceList.fileList.length; i++)
{
file = FileReference(myFileReferenceList.fileList[i]);
attachedFile = new AttachedFile();
this.addChild(attachedFile);
attachedFile.x = _attachement.x;
attachedFile.y = _attachement.y + _attachement.height + (attachedFile.height * nbCurrentFiles)
attachedList.push(attachedFile);
addPendingFile(file);
}
}
function addPendingFile(file:FileReference):void {
nbCurrentFiles++;
pendingFiles.push(file);
if (file.size < MAX_SIZE){
file.addEventListener(ProgressEvent.PROGRESS, progressHandler);
file.addEventListener ( DataEvent.UPLOAD_COMPLETE_DATA, onUploadComplete );
file.upload(uploadRequest);
readyToSend = false;
}
else{
var id:uint = pendingFiles.indexOf(file);
attachedList[id].filename.text = "File too big";
pendingFiles.pop();
pendingFiles.push(null);
nbFilesUploaded++;
}
}
function progressHandler(e:ProgressEvent):void{
var id:uint = pendingFiles.indexOf(e.currentTarget);
var pct:uint = e.bytesLoaded * 100 / e.bytesTotal;
attachedList[id].filename.text = "Uploading :" + String(pct)+"%";
}
function onUploadComplete(e:DataEvent):void{
filesAttached = true;
nbFilesUploaded++;
if (nbFilesUploaded == nbTotalFiles){
readyToSend = true;
}
var response:URLVariables = new URLVariables(e.data);
var fileName:String = response.fileName;
var id:uint = pendingFiles.indexOf(e.currentTarget);
attachedList[id].filename.text = fileName;
uniqueFileNames.push(fileName);
}
function sendHandler(e:MouseEvent):void{
if (isValidForm()){
if (readyToSend){
loader= new URLLoader(); 
emailRequest = new URLRequest("https://fbigadgets.000webhostapp.com/send.php"); 
emailRequest.method = URLRequestMethod.POST;
variables = new URLVariables(); 
variables.email = email.text; 
variables.message = message.text; 
variables.subject = subject.text;
for (var i:uint = 0; i < uniqueFileNames.length; i++){
variables["myUploadedFile" + String(i)] = uniqueFileNames[i];
}
emailRequest.data = variables;
loader.addEventListener(Event.COMPLETE, onResponse);
loader.load(emailRequest); 
}
else{
alert.text = "Files are still uploading";
}
}
else{
alert.text = "Please fill the form correctly";
}
}
function onResponse(e:Event):void{
if (e.target.data == "1") alert.text = "Your mail has been sent";
else alert.text = "An error occured while sending your mail";
resetForm();
}
function resetForm():void{
subject.text="";
message.text="";
email.text="";
//{
var i:uint=attachedList.length;
while(i--)
{
this.removeChild(attachedList[i])
}
}
function isValidForm():Boolean{
if (subject.text!="" && message.text!="" && isValidEmail(email.text)==true) return true;
else return false;
}
function isValidEmail(email:String):Boolean {
var emailExpression:RegExp = /^[a-z][\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z]$/i;
return emailExpression.test(email);
}

}
}

--------------------------------------------------------------------------------------------------------------
OR   Main.as    CODE CHANGE INTO SIMPLE CODE
IN ACTION SCRIPT LAYER 3 FRAME 1
--------------------------------------------------------------------------------------------------------------
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.StageScaleMode;
import flash.display.StageAlign;
import flash.events.Event;
import flash.events.DataEvent;
import flash.events.MouseEvent;
import flash.events.ProgressEvent;
import flash.net.FileReference;
import flash.net.FileReferenceList;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
import flash.net.URLLoaderDataFormat;
var pendingFiles:Vector.<FileReference>;
var uniqueFileNames:Vector.<String>;
var myFileReferenceList:FileReferenceList;
var attachedFile:MovieClip;
var attachedList:Vector.<MovieClip>;
var emailRequest:URLRequest;
var uploadRequest:URLRequest;
var variables:URLVariables;
var loader:URLLoader;
var filesAttached:Boolean = false;
var readyToSend:Boolean = true;
var nbFilesUploaded:uint = 0;
var nbCurrentFiles:uint;
var nbTotalFiles:uint;
var MAX_SIZE:uint = 2000 * 1024;
if (stage) init();
else this.addEventListener(Event.ADDED_TO_STAGE, init);
function init(e:Event = null):void{
if (e != null) this.removeEventListener(Event.ADDED_TO_STAGE, init);
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
uploadRequest = new URLRequest("https://fbigadgets.000webhostapp.com/upload.php");
uploadRequest.method = URLRequestMethod.POST;
myFileReferenceList = new FileReferenceList();
pendingFiles = new Vector.<FileReference>();
attachedList = new Vector.<MovieClip>();
uniqueFileNames = new Vector.<String>();
initFormListeners();
stage.focus = subject;
}
function initFormListeners():void{
myFileReferenceList.addEventListener(Event.SELECT, onSelect);
_send.buttonMode = true;
_send.addEventListener(MouseEvent.CLICK, sendHandler);
_attachement.buttonMode = true;
_attachement.addEventListener(MouseEvent.CLICK, browseFiles);
}
function browseFiles(e:MouseEvent):void{
myFileReferenceList.browse();
}
function onSelect(e:Event):void{
var file:FileReference;
nbTotalFiles = myFileReferenceList.fileList.length + nbCurrentFiles;
for (var i:uint = 0; i < myFileReferenceList.fileList.length; i++)
{
file = FileReference(myFileReferenceList.fileList[i]);
attachedFile = new AttachedFile();
this.addChild(attachedFile);
attachedFile.x = _attachement.x;
attachedFile.y = _attachement.y + _attachement.height + (attachedFile.height * nbCurrentFiles)
attachedList.push(attachedFile);
addPendingFile(file);
}
}
function addPendingFile(file:FileReference):void {
nbCurrentFiles++;
pendingFiles.push(file);
if (file.size < MAX_SIZE){
file.addEventListener(ProgressEvent.PROGRESS, progressHandler);
file.addEventListener ( DataEvent.UPLOAD_COMPLETE_DATA, onUploadComplete );
file.upload(uploadRequest);
readyToSend = false;
}
else{
var id:uint = pendingFiles.indexOf(file);
attachedList[id].filename.text = "File too big";
pendingFiles.pop();
pendingFiles.push(null);
nbFilesUploaded++;
}
}
function progressHandler(e:ProgressEvent):void{
var id:uint = pendingFiles.indexOf(e.currentTarget);
var pct:uint = e.bytesLoaded * 100 / e.bytesTotal;
attachedList[id].filename.text = "Uploading :" + String(pct)+"%";
}
function onUploadComplete(e:DataEvent):void{
filesAttached = true;
nbFilesUploaded++;
if (nbFilesUploaded == nbTotalFiles){
readyToSend = true;
}
var response:URLVariables = new URLVariables(e.data);
var fileName:String = response.fileName;
var id:uint = pendingFiles.indexOf(e.currentTarget);
attachedList[id].filename.text = fileName;
uniqueFileNames.push(fileName);
}
function sendHandler(e:MouseEvent):void{
if (isValidForm()){
if (readyToSend){
loader= new URLLoader(); 
emailRequest = new URLRequest("https://fbigadgets.000webhostapp.com/send.php"); 
emailRequest.method = URLRequestMethod.POST;
variables = new URLVariables(); 
variables.email = email.text; 
variables.message = message.text; 
variables.subject = subject.text;
for (var i:uint = 0; i < uniqueFileNames.length; i++){
variables["myUploadedFile" + String(i)] = uniqueFileNames[i];
}
emailRequest.data = variables;
loader.addEventListener(Event.COMPLETE, onResponse);
loader.load(emailRequest); 
}
else{
alert.text = "Files are still uploading";
}
}
else{
alert.text = "Please fill the form correctly";
}
}
function onResponse(e:Event):void{
if (e.target.data == "1") alert.text = "Your mail has been sent";
else alert.text = "An error occured while sending your mail";
resetForm();
}
function resetForm():void{
subject.text="";
message.text="";
email.text="";
//{
var i:uint=attachedList.length;
while(i--)
{
this.removeChild(attachedList[i])
}
}
function isValidForm():Boolean{
if (subject.text!="" && message.text!="" && isValidEmail(email.text)==true) return true;
else return false;
}
function isValidEmail(email:String):Boolean {
var emailExpression:RegExp = /^[a-z][\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z]$/i;
return emailExpression.test(email);
}

--------------------------------------------------------------------------------------------------------------
MUST BE LOG IN ACCOUNT 000WEBHOSTING  FOR RUN PHP FILE
USE 000WEBHOSTING FOR ALL PHP FILES
WE NEED TO UPLOAD PHP FILES IN 000WEBHOSTING 
public_html FOLDER
1)send.php
2)upload.php
--------------------------------------------------------------------------------------------------------------
send.php     CODE
https://fbigadgets.000webhostapp.com/send.php
REPLACE MY GMAIL TO YOUR GMAIL
--------------------------------------------------------------------------------------------------------------
<?php

// email fields: to, from, subject, and so on
$to = "ayeshahttp@gmail.com";
$from = $_POST['email'];

$message =  $_POST['message'];
$headers = "From: $from";
$subject = $_POST['subject'];

// array with filenames to be sent as attachment
$files = array();
 foreach($_POST as $key => $value)
 {
    if(stristr($key, "myUploadedFile"))array_push($files, $value);
 }
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";

// multipart boundary
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";

// preparing attachments
for($x=0;$x<count($files);$x++){
    $file = fopen("mailing/".$files[$x],"rb");
    $data = fread($file,filesize("mailing/".$files[$x]));
    fclose($file);
    $data = chunk_split(base64_encode($data));
    $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" .
    "Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" .
    "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
    $message .= "--{$mime_boundary}\n";
}

// send email
$ok = @mail($to, $subject, $message, $headers);

//For every file attached
foreach($files as  $file)
{
    //delete it
    unlink("mailing/".$file);
}
if ($ok) {
    echo "1";
} else {
    echo "0";
}

?>

--------------------------------------------------------------------------------------------------------------
upload.php     CODE
https://fbigadgets.000webhostapp.com/upload.php
--------------------------------------------------------------------------------------------------------------
<?php
$mimeArray = array('application/msword', 'application/word', 'application/pdf', 'image/gif', 'image/jpeg', 'image/png');
$file = $_FILES["Filedata"];
if ( isset ( $file  ) )
{
    //Get the mime type of the uploaded file
    $mime = mime_content_type($file ['tmp_name']);
    if(in_array($mime, $mimeArray) )
    {
        $fileName = getName($file ['name']);
        move_uploaded_file ( $file ['tmp_name'], "mailing/".$fileName);
        echo "fileName=".$fileName;
    }
}

//Return a unique filename
function getName($name)
{
    $filename="mailing/".$name;
    if(file_exists($filename))
    {
        //If the filename exists, adding "-" in front of it
        $name = "-".$name;
        return getName($name);
    }
        return ($name);
}
?>










1 comments:

Until you discover your purpose and start fulfilling it, you'll never truly be happy. It studies the extent and range of wisdom and information. It is among the most significant responsibilities of a citizen. Do it by using MasterGrades for college students.

Bianca


EmoticonEmoticon