Multiple email attachments
Posted: Tue Dec 16, 2008 1:28 pm
Hi,
I am trying to create a form that will allow a user to upload and send multiple email attachments. I need it to send one email with all attachments but right now it is sending one email per attachment. Would greatly appreciate some help.
Here is the code:
I am trying to create a form that will allow a user to upload and send multiple email attachments. I need it to send one email with all attachments but right now it is sending one email per attachment. Would greatly appreciate some help.
Here is the code:
Code: Select all
foreach ($_FILES["filename"]["error"] as $key => $error){
if ($error == UPLOAD_ERR_OK) {
//check that to and from are email addresses
//-----------------------------------------------------------------------------------------------
if(!isValidEmail($to) || !isValidEmail($from)) {
echo "We're sorry, we can't email this cap yet. Email address invalid format, must be user@website format.<br/>";
echo "TO: " . $to . "<BR>";
echo "FROM: " . $from . "<BR>";
exit;
}
//-----------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------
//make the image, and output it to disk, under the temp directory
//$image=imagecreatetruecolor( $width ,$height );
//$background = imagecolorallocate( $image ,0 , 0 , 0 );
//Copy pixels
//$i = 0;
//for($x=0; $x<=$width; $x++){
//for($y=0; $y<=$height; $y++){
// $int = hexdec($data[$i++]);
// $color = ImageColorAllocate ($image, 0xFF & ($int >> 0x10), 0xFF & ($int >> 0x8), 0xFF & $int);
// imagesetpixel ( $image , $x , $y , $color );
// }
// }
//save the new image to the image directory, named $refnum.jpg, with quality level 100%
// ImageJPEG($image, IMG_PATH . $refnum . ".jpg", 100);
//manual garbage collection:
// imagedestroy($image);
//-----------------------------------------------------------------------------------------------
//This is what is outputted to the browser, and the email message body
$message = file_get_contents(TEMPLATE);
//replace the fields
$message = str_replace("[CUSTOMER]", $customer, $message);
$message = str_replace("[ORDEREDBY]", $orderedBy, $message);
$message = str_replace("[CITY]", $city, $message);
$message = str_replace("[STATE]", $state, $message);
$message = str_replace("[DATE]", $date, $message);
$message = str_replace("[NEEDBY]", $needBy, $message);
$message = str_replace("[ASAP]", $asap, $message);
$message = str_replace("[PHONE]", $phone, $message);
$message = str_replace("[FAX]", $fax, $message);
$message = str_replace("[PONUMBER]", $POnumber, $message);
$message = str_replace("[SAMESHIPPING]", $sameShipping, $message);
$message = str_replace("[SHIPPINGCUSTOMER]", $shippingCustomer, $message);
$message = str_replace("[SHIPPINGATTN]", $shippingAttn, $message);
$message = str_replace("[SHIPPINGADDRESS]", $shippingAddress, $message);
$message = str_replace("[SHIPPINGCITY]", $shippingCity, $message);
$message = str_replace("[SHIPPINGSTATE]", $shippingState, $message);
$message = str_replace("[SHIPPINGZIP]", $shippingZip, $message);
$message = str_replace("[NEWORDER]", $newOrder, $message);
$message = str_replace("[RP]", $repeatOrder, $message);
$message = str_replace("[RR]", $repeatReference, $message);
//____________________________________________________________________________________________________
//Create attachment
// generate a random string to be used as the boundary marker
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
// store the file information to variables for easier access
$tmp_name = $_FILES['filename']['tmp_name'][$key];
$type = $_FILES['filename']['type'][$key];
$name = $_FILES['filename']['name'][$key];
$size = $_FILES['filename']['size'][$key];
// if the upload succeded, the file will exist
if (file_exists($tmp_name)){
// check to make sure that it is an uploaded file and not a system file
if(is_uploaded_file($tmp_name)){
// open the file for a binary read
$file = fopen($tmp_name,'rb');
// read the file content into a variable
$data = fread($file,filesize($tmp_name));
// close the file
fclose($file);
// now we encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));
}
// now we'll build the message headers
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";
// next, we'll build the message body
// note that we insert two dashes in front of the
// MIME boundary when we use it
$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" .
$message . "\n\n";
// now we'll insert a boundary to indicate we're starting the attachment
// we have to specify the content type, file name, and disposition as
// an attachment, then add the file content and set another boundary to
// indicate that the end of the file has been reached
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
// now we just send the message
if (@mail($to, $subject, $message, $headers))
echo "Your message has been sent. Thank you for the order. Please see below for order details.";
else
echo "Failed to send";
}
}
}
}
//---------------------------------------------------------
function isValidEmail($email) {
// First, we check that there's one @ symbol, and that the lengths are right
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
return false;
}
// Split it into sections to make life easier
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
return false;
}
}
if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
return false;
}
}
}
return true;
}
//---------------------------------------------------------
?>