Jump to content

Jquery Multi-File Upload To Multi-File Email Attachment


lingo5

Recommended Posts

Hi, I had trouble sending email attachments and could not find a lot of help anywhere...well at least not a lot of help that I (a newbie) could understand, so I did a lot of looking around and came up with a very nice JQuery solution to attach mutiple files and email them correctly using the Multiple File Upload JQuery plugin...which is verrry cool.

This is how I do it:

 

1 - download the Multiple File Upload JQuery plugin here

http://www.fyneworks.com/jquery/multiple-file-upload/#

 

2 - create your html form

<html>
<head>
    <!-- INICIO SECCION DE SCRIPTS PARA EL MULTIPLE ATTACHMENT-->
    <link href="../css/contactenos.css" rel="stylesheet" type="text/css" />	
   	<script src="../js/jquery-1.7.js" type="text/javascript"></script>  // link to jquery-1.7.js and jquery.MultiFile.pack.js
<script src="../js/jquery.MultiFile.pack.js" type="text/javascript"></script>
    <script src="../js/jquery.blockUI.js" type="text/javascript"></script>
    
    <!-- FINAL SECCION DE SCRIPTS PARA EL MULTIPLE ATTACHMENT-->
</head>
<body>
<form action="mail2.php" method="post" name="form1" enctype="multipart/form-data">
<table width="343" border="1">
<tr>
<td>To</td>
<td><input name="txtTo" type="text" id="txtTo"></td>
</tr>
<tr>
<td>Subject</td>
<td><input name="txtSubject" type="text" id="txtSubject"></td>
</tr>
<tr>
<td>Description</td>
<td><textarea name="txtDescription" cols="30" rows="4" id="txtDescription"></textarea></td>
</tr>
<tr>
<td>Form Name</td>
<td><input name="txtFormName" type="text"></td>
</tr>
<tr>
<tr>
<td>Form Email</td>
<td><input name="txtFormEmail" type="text"></td>
</tr>
<tr>
<td>Attachment</td>
<td>
<input name="fileAttach[]" type="file" class="multi"><br><br> //don't forget to add class="multi" for the juery multiple selection to work</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Send"></td>
</tr>
</table>
</form>
</body>
</html>

 

3 - Create your sendmail script:

<?
$strTo = $_POST["txtTo"];
$strSubject = stripslashes($_POST["txtSubject"]);
$strMessage = stripslashes($_POST["txtDescription"]);
$from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">"; 
//*** Uniq id Session ***//
$strSid = md5(uniqid(time()));


$strHeader = "";
$strHeader .= "From: $from\r\n" . "MIME-Version: 1.0\r\n" . "Content-Type: multipart/mixed;\r\n"; 
$strHeader .= "Cc: {$_POST['cc']}\r\n";
$strHeader .= "Bcc: {$_POST['bcc']}\r\n";

$strHeader .= "MIME-Version: 1.0\n";
$strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";
$strHeader .= "This is a multi-part message in MIME format.\n";

$strHeader .= "--".$strSid."\n";
$strHeader .= 'Content-Type: text/html; charset=iso-8859-1'.$eol;
$strHeader .= "Content-Transfer-Encoding: 7bit\n\n";
$strHeader .= $strMessage."\n\n";

//*** Attachment ***//
for($i=0;$i<count($_FILES["fileAttach"]["name"]);$i++)
{
if($_FILES["fileAttach"]["name"][$i] != "")
{
$strFilesName = $_FILES["fileAttach"]["name"][$i];
$strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"][$i])));
$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n";
$strHeader .= "Content-Transfer-Encoding: base64\n";
$strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";
$strHeader .= $strContent."\n\n";
}
}

$flgSend = @mail($strTo,$strSubject,null,$strHeader); // @ = No Show Error //

if($flgSend)
{
header("Location: ../PC_contactos_display.php?emailsentok=true"); 
}
//echo "Mail send completed.";
//}
else
{
header("Location: ../PC_contactos_display.php?emailfailed=true"); 
}
?>

 

 

..and that's it...try it because it's quite nice...

Link to comment
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.