Sublime directory Surf the web anonymous Pagerank Monitor


sending email with multiple attachments

joe2movies
Tue 27 February 2007, 12:00 pm GMT +0100
//somehow it only attaches the first file not the 2nd file, it sends email fine though




<form enctype="multipart/form-data" action="sendMail.php" method="post">
  <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
  Choose a file to upload: <input name="userfile" type="file" /><br>
  <input name="userfile2" type="file" />
<br><br>  <input type="submit" value="Upload File" />
</form>

<?php


function mail_attachment2 ($from , $to, $subject, $message, $attachment,$filetype,$filename,$filetype2,$filename2){
$fileatt = $attachment; // Path to the file

$fileatt_type = $filetype; // File Type
$start= strrpos($attachment, '/') == -1 ? strrpos($attachment, '//') : strrpos($attachment, '/')+1;
//$fileatt_name = substr($attachment, $start, strlen($attachment)); // Filename that will be used for the file as the attachment
$fileatt_name = $filename;
//$fileatt_name = "testEmail.txt";

$email_from = $from; // Who the email is from
$email_subject = $subject; // The Subject of the email
$email_txt = $message; // Message that the email has in it

$email_to = $to; // Who the email is to

$headers = "From: ".$email_from;

$file = fopen($fileatt,"rb");

$si=filesize($fileatt);
$data = fread($file,$si);
fclose($file);
//$msg_txt="\n\nMail created using free code from 4word systems : http://4wordsystems.com";

$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_txt .= $msg_txt;

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

$email_txt . "\n\n";

$data = chunk_split(base64_encode($data));

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




/********************************************** First File ********************************************/


$fileatt = $attachment; // Path to the file
$fileatt_type = $filetype; // File Type
$fileatt_name = $filename; // Filename that will be used for the file as the attachment
//$fileatt_name = "sendEmailMultipleAttachments.rtf";
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);

echo "File1: " . $fileatt_name . "." . $fileatt_type . "<br>";

$data = chunk_split(base64_encode($data));

$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}\n";
unset($data);
unset($file) ;
unset($fileatt) ;
unset($fileatt_type) ;
unset($fileatt_name) ;


/********************************************** Second File ********************************************/


$fileatt = $attachment; // Path to the file
$fileatt_type = $filetype2; // File Type
$fileatt_name = $filename2; // Filename that will be used for the file as the attachment

$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
echo "File2: " . $fileatt_name . "." . $fileatt_type . "<br>";

$data = chunk_split(base64_encode($data));

$email_message .= "Content-Type:{$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
unset($data);
unset($file) ;
unset($fileatt) ;
unset($fileatt_type) ;
unset($fileatt_name) ;





$ok = @mail($email_to, $email_subject, $email_message, $headers);

if($ok) {
echo "And sended sucessfully";
//unlink($fileatt);
} else {
die("Sorry but the email could not be sent. Please go back and try again!");
}
}//end function multiple attachments











if(isset($_FILES['userfile']['name'])){
$from="diep@chariot.net.au";
$to="diep@chariot.net.au";
$subject="send email with attachments";
$message="testing attachment";

//print_r($_FILES);

mail_attachment2($from,$to,$subject,$message,$_FILES['userfile']['tmp_name'],$_FILES['userfile']['type'],$_FILES['userfile']['name'],$_FILES['userfile2']['type'],$_FILES['userfile2']['name']);
}//end if


?>

Nikolas
Tue 27 February 2007, 12:04 pm GMT +0100
That is a long piece of code to read right now.

Why don't use a class like phpmailer?

It has anything you want out of the box :)

olaf
Tue 27 February 2007, 12:05 pm GMT +0100
where do you got the snippet?

sending multiple att. is a tricky thing try phpmailer or this class http://www.finalwebsites.com/snippets.php?id=41

Archive for SMF v1.00 by N.P. Valid XHTML 1.0 Transitional