Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Mail

Problem attaching file into form

the mail() function

Moderators: macek, egami, gesf

Problem attaching file into form

Postby amvc » Tue Jun 15, 2004 5:59 am

Hi,
i made a simple html form where its possible to attach a file, to send this information by email i used a .php file. The problem is that while all the information in the form is received correctly, the attached file is not.
I think the problem has to do with uploading the file into the server (i dont know how to do that). Here are the html and php files codes i've used:

HTML:
Code: Select all
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<form name=a method=POST action="submit.php" enctype="multipart/form-data" autocomplete=off>
  <input type="hidden" name="to" value="alexmvc@yahoo.com">
  <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td valign="middle" align="left" width="14%" class="tit1">Nome&nbsp;</td>
      <td valign="top" colspan="2">
        <input type="text" name="nome" size="50" >
        <br>
      </td>
    </tr>
    <tr>
      <td valign="middle" align="left" width="14%" class="tit1"><span class="form">Tel./Telm.</span>&nbsp;</td>
      <td valign="top" colspan="2">
        <input type="text" name="Telefone" size="50">
        <br>
      </td>
    </tr>
    <tr>
      <td valign="middle" align="left" width="14%" class="tit1">Email&nbsp;</td>
      <td valign="top" colspan="2">
        <input type="text" name="Email" size="50">
        <br>
      </td>
    </tr>
    <tr>
      <td height="2" width="14%" align="left" valign="middle" class="tit1">&Aacute;rea
      </td>
      <td height="2" valign="top" colspan="2">
        <input type="text" name="&Aacute;rea" size="50" value="">
      </td>
    </tr>
    <tr>
      <td width="14%" class="tit1">Anexar C.V. </td>
      <td colspan="2" align="left" valign="middle"> <br>
        <input name="message" type="file" size="25">
        <br>
        <br>
      </td>
    </tr>
    <tr>
      <td width="14%">&nbsp;</td>
      <td align="right" width="25%">
        <input type="submit" value="Enviar" name="submit2">
        <input type="reset" value="Limpar" name="reset2">
      </td>
      <td align="right" width="61%">&nbsp;</td>
    </tr>
  </table>
</form>
</body>
</html>



PHP:

Code: Select all
<?php
// --- Create Message Text
$form_fields=array_keys($HTTP_POST_VARS);
$temp="\n";
while($field=array_pop($form_fields)){
$temp.=" $field : = $HTTP_POST_VARS[$field] \n";
}

// Read POST request params into global vars
$to = $_POST['to'];
$from = $_POST['Email'];
$message = $temp;

// Obtain file upload vars
$fileatt = $_FILES['Logo']['tmp_name'];
$fileatt_type = $_FILES['Logo']['type'];
$fileatt_name = $_FILES['Logo']['name'];

$headers = "From: $from";

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

// 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; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";

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

// Add file attachment to the message
$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";
}

// Send the message
$ok = @mail($to, "New Request", $message, $headers);

// -- Send Another Message to SENDER
$message = '
<html>
<head>
<title></title>
<style>
a:hover { color: red }
v\:* { behavior: url(#default#VML) }
o\:* { behavior: url(#default#VML) }
.shape { behavior: url(#default#VML) }
</style>
</head>
<body link="#0000FF" vlink="#0000FF" alink="#0000FF">
<p><font face="Verdana" size="2">OMID SOFT Open Source Free Scripts.<br>
Our records indicate that you selected OMID SOFT\'s Open Source Free Scripts.<br>
This message is to confirm that your order has been submitted.<br>
Please do not send duplicates of your e-mail unless you want to provide additional information.<br>
Thank you.<br>
OMID SOFT Open Source Free Scripts Team.</font></p>
</body>
</html>
';

$to = $HTTP_POST_VARS['Email'];
$subject = "Candidatura Enviada";

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "To: ". $to ."\r\n";
$headers .= "From: alexmvc@yahoo.com <alexmvc@yahoo.com>\r\n";

mail($to, $subject, $message, $headers);

// -- Goto Another Page
header("location:http://www.sapo.pt/");
?>




I hope that someone is able to help me.
Thanks,

Alex
amvc
New php-forum User
New php-forum User
 
Posts: 3
Joined: Mon Jun 14, 2004 8:31 am

Re: Problem attaching file into form

Postby 2nejunkie » Sun Aug 10, 2008 11:53 am

I am having the same problem.
:help:
User avatar
2nejunkie
New php-forum User
New php-forum User
 
Posts: 3
Joined: Sun Aug 10, 2008 11:18 am
Location: New York, Central New York


Return to PHP coding => Mail

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.