Hi all!
I have a page with a form that lets users fill some inputs and submit up to 3 image files.
When form is submitted, all the input fields and the 3 images (as attachments) are sent to a mailbox.
It works fine if I call the page alone.
But then I rearranged the site and the page is included when a link is clicked, so I have index.php that includes page1.php (that contains the submission form).
The mail gets sent, with all the fields, but not the files in attachment.
I tried print_r($_FILES) but the array is empty.
Is this some kind of scope problem?
My Form (page1.php):
HTML Code:<form id="frm5" name="frm5" enctype="multipart/form-data" method="post" action="<?php $_SERVER['PHP_SELF']; ?>"> <fieldset class="fldset"> <legend class="fldsetlgnd">Submeter Dados e Fotos para O Teu Carro</legend> <table border="0"> <tr> <td>Nome:</td> <td> <input type="text" id="txtnomeotc" name="txtnomeotc" size="50" value="<?php echo $nomeotc; ?>" class="txt" /> </td> </tr> <tr> <td>Telefone:</td> <td> <input type="text" id="txttlfotc" name="txttlfotc" size="12" value="<?php echo $tlfotc; ?>" class="txt2" /> </td> </tr> <tr> <td>Email:</td> <td> <input type="text" id="txtemailotc" name="txtemailotc" size="50" value="<?php echo $emailotc; ?>" class="txt" /> </td> </tr> <tr> <td>Marca:</td> <td> <input type="text" id="txtmarcaotc" name="txtmarcaotc" size="50" value="<?php echo $marcaotc; ?>" class="txt" /> </td> </tr> <tr> <td>Modelo:</td> <td> <input type="text" id="txtmodelootc" name="txtmodelootc" size="50" value="<?php echo $modelootc; ?>" class="txt" /> </td> </tr> <tr> <td>Foto 1:</td> <td> <input type="file" name="txtfoto1otc" size="30" value="" class="btn" /> </td> </tr> <tr> <td>Foto 2:</td> <td> <input type="file" name="txtfoto2otc" size="30" value="" class="btn" /> </td> </tr> <tr> <td>Foto 3:</td> <td> <input type="file" name="txtfoto3otc" size="30" value="" class="btn" /> </td> </tr> <tr> <td colspan="2"> </td> </tr> <tr> <td colspan="2" align="center"><input type="submit" id="btnSubmit5" name="btnSubmit5" value="Submeter" class="btn" /></td> </tr> </table> </fieldset> </form>
and my code that process the form (on index.php):
PHP Code:if(isset($_POST['btnSubmit5']))
{
$to = 'mramos@motorpress.pt';
$nome = $_POST['txtnomeotc'];
$tlf = $_POST['txttlfotc'];
$email = $_POST['txtemailotc'];
$marca = $_POST['txtmarcaotc'];
$modelo = $_POST['txtmodelootc'];
$assunto = 'Dados e Fotos para «O Teu Carro»';
$msgtxt = "«Dados e Fotos para O Teu Carro»<br /><br />"; // Message that the email has in it
$nome = strip_tags($nome);
$tlf = strip_tags($tlf);
$email = strip_tags($email);
$marca = strip_tags($marca);
$modelo = strip_tags($modelo);
$msgtxt .= '<table>';
$msgtxt .= '<tr><td>Nome:</td><td>'.$nome.'</td></tr>';
$msgtxt .= '<tr><td>Telefone:</td><td>'.$tlf.'</td></tr>';
$msgtxt .= '<tr><td>Email:</td><td>'.$email.'</td></tr>';
$msgtxt .= '<tr><td>Marca:</td><td>'.$marca.'</td></tr>';
$msgtxt .= '<tr><td>Modelo:</td><td>'.$modelo.'</td></tr>';
$msgtxt .= '</table>';
// generate a random string to be used as the boundary marker
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers = "From: $email\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";
$msgtxt .= "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" .
$msgtxt . "\n\n";
echo '<b>Files: </b>'.count($_FILES).'<br />';
foreach($_FILES as $fileatt)
{
if($fileatt['name'] != '')
{
$fileatt_type = $fileatt['type']; // File Type
$fileatt_name = $fileatt['name']; // Filename that will be used for the file as the attachment
$fileatt_size = $fileatt['size']; // File size
$fileatt_tmp_name = $fileatt['tmp_name']; // Upload file
echo 'Type: '.$fileatt_type.'<br />';
echo 'Name: '.$fileatt_name.'<br />';
echo 'Size: '.$fileatt_size.'<br />';
echo 'TMP Name: '.$fileatt_tmp_name.'<br />';
if ( ($fileatt_type == 'image/gif') || ($fileatt_type == 'image/jpeg') || ($fileatt_type == 'image/png') || ($fileatt_type == 'image/tiff') || ($fileatt_type == 'image/bmp') )
{
if($fileatt_size > 512000)
{
//die("Sorry but the email could not be sent. File size must be under 500KB!");
$mensagem = 'A sua mensagem não pode ser enviada. Os ficheiros não devem ultrapassar os 500KB cada!';
}
else
{
// if the upload succeded, the file will exist
if(file_exists($fileatt_tmp_name))
{
// check to make sure that it is an uploaded file and not a system file
if(is_uploaded_file($fileatt_tmp_name))
{
$file = fopen($fileatt_tmp_name,'rb');
$data = fread($file,filesize($fileatt_tmp_name));
fclose($file);
$data = chunk_split(base64_encode($data));
} // end is_uploaded_file
$msgtxt .= "--{$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";
} // end file_exists
unset($data);
unset($file);
unset($fileatt);
unset($fileatt_type);
unset($fileatt_name);
unset($fileatt_size);
unset($fileatt_tmp_name);
} //end file size check
}
else
{
//die("Sorry but the email could not be sent. Wrong file type!");
$mensagem = 'A sua mensagem não pode ser enviada. O tipo de ficheiro não é permitido!';
} // end filetype check
} // end file count
} // end foreach FILES
$enviar = mail($to, $assunto, $msgtxt, $headers);
$mensagem = $enviar ? "Dados Enviados" : "Dados NÃO Enviados";
} // end btnSubmit5
Another problem with this emailing script is that along with the image as attachment I get an empty file like 'ATT9353397.txt' at my work email with Outlook, but if I send it to any other webmail system it doesn't appear.
Thankx for your help.








Bookmarks