<?php
//mail form
echo " <html><title> PHP mail with multiple attachments</title>
<form id='frmEmail' enctype='multipart/form-data' accept-charset='UTF-8'>
<table>
<tr><td>To</td><td><input name='to'></input></td></tr>
<tr><td>Subject</td><td><input name='subject'></input></td></tr>
<tr><td valign='top'>Attachments</td><td><dt id='files_list'></dt></td> </tr>
<tr><td>Add attachment</td><td><input type='file' name='file' id='file'></input></td> </tr>
<tr><td>Message</td><td><textarea name='message'></textarea></td> </tr>
<tr><td ><input type='submit' value='Send mail' name='send'></td><td><input type='reset'></td></tr>
</table>
</form>
</html>
";
if($_POST['send']){
//upload files
foreach ($_FILES as $file) {
if ($file['error'] == UPLOAD_ERR_OK && $file['size'] ) {
if(move_uploaded_file($file['tmp_name'], "/tmp/{$file['name']}")){
$files[]= "/tmp/{$file['name']}";
}
}
}
//boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
//message
$message=$_POST['message'];
//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($files[$x],"rb");
$data = fread($file,filesize($files[$x]));
fclose($file);
$filename = basename($files[$x]);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$filename\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$filename\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= ($x==count($files)-1) ? "--{$mime_boundary}--\n" : "--{$mime_boundary}\n";
}
// extra headers
$more_headers = "-f".$rs->EFrom;
// mail it,
$done = @mail($_POST['message'], $_POST['subject'],$message,$headers,$more_headers);
//status ?
echo $done?"Mail sent":"Mail failed";
}
?>
<script type="text/javascript" language='javascript'>
var multi_selector = new MultiSelector( document.getElementById( 'files_list' ),20 );
multi_selector.addElement( document.getElementById( 'file' ) );
//function MultiSelector( list_target, max ){this.list_target = list_target;this.count = 0;this.id = 0;if( max ){this.max = max;} else {this.max = -1;};this.addElement = function( element ){if( element.tagName == 'INPUT' && element.type == 'file' ){element.name = 'file_' + this.id++;element.multi_selector = this;element.onchange = function(){var new_element = document.createElement( 'input' );new_element.type = 'file';this.parentNode.insertBefore( new_element, this );this.multi_selector.addElement( new_element );this.multi_selector.addListRow( this );this.style.position = 'absolute';this.style.left = '-1000px';};if( this.max != -1 && this.count >= this.max ){element.disabled = true;};this.count++;this.current_element = element;} else {alert( 'Error: not a file input element' );};};this.addListRow = function( element ){var new_row = document.createElement( 'div' );var new_row_button = document.createElement( 'input' );new_row_button.type = 'button';new_row_button.value = 'Delete';new_row.element = element;new_row_button.onclick= function(){this.parentNode.element.parentNode.removeChild( this.parentNode.element );this.parentNode.parentNode.removeChild( this.parentNode );this.parentNode.element.multi_selector.count--;this.parentNode.element.multi_selector.current_element.disabled = false;return false;};new_row.innerHTML = element.value;new_row.appendChild( new_row_button );this.list_target.appendChild( new_row );};};
function MultiSelector( list_target, max ){
this.list_target = list_target;
this.count = 0;
this.id = 0;
if( max ){
this.max = max;
} else {
this.max = -1;
};
this.addElement = function( element ){
if( element.tagName == 'INPUT' && element.type == 'file' ){
element.name = 'file_' + this.id++;
element.multi_selector = this;
element.onchange = function(){
var new_element = document.createElement( 'input' );
new_element.type = 'file';
this.parentNode.insertBefore( new_element, this );
this.multi_selector.addElement( new_element );
this.multi_selector.addListRow( this );
this.style.position = 'absolute';
this.style.left = '-1000px';
};
if( this.max != -1 && this.count >= this.max ){
element.disabled = true;
};
this.count++;
this.current_element = element;
} else {
alert( 'Error: not a file input element' );
};
};
this.addListRow = function( element ){
var new_row = document.createElement( 'div' );
var new_row_button = document.createElement( 'input' );
new_row_button.type = 'button';
new_row_button.value = 'Remove';
new_row.element = element;
new_row_button.onclick= function(){
this.parentNode.element.parentNode.removeChild( this.parentNode.element );
this.parentNode.parentNode.removeChild( this.parentNode );
this.parentNode.element.multi_selector.count--;
this.parentNode.element.multi_selector.current_element.disabled = false;
return false;
};
new_row.innerHTML = element.value;
new_row.appendChild( new_row_button );
this.list_target.appendChild( new_row );
};
};
</script>
Return to Kesavan Muthuvel's home page. Please send comments on these web pages to hi@kesavan.info (or) Feedback here CopyLeft (Ͻ) 2008 - 2023 Kesavan Muthuvel More on CopyLeft |