Forum Moderators: coopster

Message Too Old, No Replies

Image Upload - Send To Email

         

reyna12

4:45 pm on Dec 24, 2006 (gmt 0)

10+ Year Member



In DESPERATE NEED of a script which allows max of 5 photo uploads and sends those photos via email.

I have tried one script, only sent a few of the images (out of 5) some of the time, tried something else which gets a fatal error of allowed memory size X bytes exhausted....

It pulls info from a form and sends the info and image uploads (5 image upload forms in total, not all or if any at all have to be used)

This is the one with fatal error,

<?php

// Read POST request parms into global vars
$email= $_POST['email'];
$subject= "Caricature Details";
$fullname= $_POST['fullname'];
$telephone= $_POST['telephone'];
$address= $_POST['address'];
$postcode= $_POST['postcode'];
$emailmessage= $_POST['message'];
$country= $_POST['country'];
$orderid= $_POST['orderid'];
$website= $_POST['website'];
$ThankyouURL = "thanks.php";
$BadAddy= "error.php";
$YourWebsiteURL = "test.php";
$to= "sales@funny-faces.co.uk";

// Format the message body
$message = ""

. "Order ID: " . $orderid . "
"
. "Full Name: " . $fullname . "
"
. "Email Address: " . $email . "
"
. "Telephone: " . $telephone . "
"
. "Address: " . $address . "

"
. "Postcode: " . $postcode . "
"
. "Country: " . $country . "

"
. "Can we use your characature on our website? " . $website . "

"
. "How did you hear about us? " . $hear . "

"

. "Specification: \n\n"


. $emailmessage . "";

// Obtain file upload vars
$fileatt= $_FILES['fileatt']['tmp_name'];
$fileatt_type= $_FILES['fileatt']['type'];
$fileatt_name= $_FILES['fileatt']['name'];
$fileatt2= $_FILES['fileatt2']['tmp_name'];
$fileatt_type2= $_FILES['fileatt2']['type'];
$fileatt_name2= $_FILES['fileatt2']['name'];
$fileatt3= $_FILES['fileatt3']['tmp_name'];
$fileatt_type3= $_FILES['fileatt3']['type'];
$fileatt_name3= $_FILES['fileatt3']['name'];
$fileatt4= $_FILES['fileatt4']['tmp_name'];
$fileatt_type4= $_FILES['fileatt4']['type'];
$fileatt_name4= $_FILES['fileatt4']['name'];
$fileatt5= $_FILES['fileatt5']['tmp_name'];
$fileatt_type5= $_FILES['fileatt5']['type'];
$fileatt_name5= $_FILES['fileatt5']['name'];

$headers = "From: $email";

if (is_uploaded_file($fileatt) ¦¦ is_uploaded_file($fileatt2) ¦¦ is_uploaded_file($fileatt3) ¦¦ is_uploaded_file($fileatt4) ¦¦ is_uploaded_file($fileatt5)) {
// Read the file to be attached ('rb' = read binary)
if (is_uploaded_file($fileatt)) {
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);

// Base64 encode the file data
$data = chunk_split(base64_encode($data));
}

if (is_uploaded_file($fileatt2)) {
$file2 = fopen($fileatt2,'rb');
$data2 = fread($file2,filesize($fileatt2));
fclose($file2);

// Base64 encode the file data
$data2 = chunk_split(base64_encode($data2));
}

if (is_uploaded_file($fileatt3)) {
$file3 = fopen($fileatt3,'rb');
$data3 = fread($file3,filesize($fileatt3));
fclose($file3);

// Base64 encode the file data
$data3 = chunk_split(base64_encode($data3));
}

if (is_uploaded_file($fileatt4)) {
$file4 = fopen($fileatt4,'rb');
$data4 = fread($file4,filesize($fileatt4));
fclose($file4);

// Base64 encode the file data
$data4 = chunk_split(base64_encode($data4));
}

if (is_uploaded_file($fileatt5)) {
$file5 = fopen($fileatt5,'rb');
$data5 = fread($file5,filesize($fileatt5));
fclose($file5);

// Base64 encode the file data
$data5 = chunk_split(base64_encode($data5));
}

// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

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

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

// Add file attachment(s) to the message
if ($fileatt_name > "") {
$message .= "Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name}\"\n\n" .
$data . "\n" .
"--{$mime_boundary}\n";
}

if ($fileatt_name2 > "") {
$message .= "Content-Type: {$fileatt_type2};\n" .
" name=\"{$fileatt_name2}\"\n" .

"Content-Transfer-Encoding: base64\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name2}\"\n\n" .
$data2 . "\n" .
"--{$mime_boundary}\n";
}

if ($fileatt_name3 > "") {
$message .= "Content-Type: {$fileatt_type3};\n" .
" name=\"{$fileatt_name3}\"\n" .

"Content-Transfer-Encoding: base64\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name3}\"\n\n" .
$data3 . "\n" .
"--{$mime_boundary}\n";
}

if ($fileatt_name4 > "") {
$message .= "Content-Type: {$fileatt_type4};\n" .
" name=\"{$fileatt_name4}\"\n" .

"Content-Transfer-Encoding: base64\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name4}\"\n\n" .
$data4 . "\n" .
"--{$mime_boundary}\n";
}

if ($fileatt_name5 > "") {
$message .= "Content-Type: {$fileatt_type5};\n" .
" name=\"{$fileatt_name5}\"\n" .
"Content-Transfer-Encoding: base64\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name5}\"\n\n" .
$data5 . "\n" .
"--{$mime_boundary}";
}
$message .= "--\n\n";
}

//Validate the email address used
if (eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$', $_POST['email'])) {
// Send the message
$ok = @mail("$to", $subject, $message, $headers);
if ($ok) {
header("Location: $ThankyouURL");
}
else {
echo "<p>Mail could not be sent. Sorry!</p>";
}
}
else {
header("Location: $BadAddy");
}
?>

Thanks :)

eelixduppy

5:13 pm on Dec 24, 2006 (gmt 0)



Welcome to WebmasterWorld!

>>>This is the one with fatal error,

Can you please post the fatal error this script gives.

rokec

5:17 pm on Dec 24, 2006 (gmt 0)

10+ Year Member



This script is very long and not optimized, so i'd suggest to write that script yourself.

reyna12

3:49 pm on Dec 25, 2006 (gmt 0)

10+ Year Member



*Fatal error*: Allowed memory size of 20971520 bytes exhausted (tried to allocate 5659366 bytes) in
*/home/fhlinux182/f/example.co.uk/user/htdocs/email.php* on line *183

that's the error

[edited by: coopster at 4:48 pm (utc) on Dec. 25, 2006]
[edit reason] generalized domain in directory path [/edit]

coopster

4:59 pm on Dec 25, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You've gone beyond the maximum amount of memory in bytes that a script is allowed to allocate.

See memory_limit [php.net] for more information.

reyna12

11:26 pm on Dec 25, 2006 (gmt 0)

10+ Year Member



What other options is there?

coopster

4:21 pm on Dec 26, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



memory_limit
is a directive of type PHP_INI_ALL [php.net] so you could use ini_set() [php.net] to extend your resources. Watch that you don't eat up all available memory on your server though. As mentioned in the directive documentation as well as pointed out earlier in this thread, you may want to reconsider the way the code is written. You are allocating quite a bit of memory with 5 images being uploaded and then pushed out via email.

cmarshall

6:01 pm on Dec 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do one image at a time, spool it to temp files, then read in each temp file as a base64 when you send the mail. That's what I do.

It's a pain to do it this way, but it works. The plus is that it can be code that you can reuse to store images in a database, or render realtime altered images.