php邮件附件无法正常工作

时间:2012-10-13 10:50:04

标签: php

我是php的新手。我从互联网上找到了一个正常运行的脚本。但是当我将附件脚本添加到此文件时,它不再正常工作。

为了使其正常工作,我应该做些什么改变?

或者请让更改正常工作。

<html>
<?php
if($_GET['name']== '' || $_GET['email']=='' || $_GET['Message']=='' )
{
?>

<form action="check2.php" method="get" name="frmPhone"> 
<fieldset> 
<legend style="color:#000">Contact    </legend>  
<table width="100%">
<tr>
<td width="29%">
<label for="name"     <?php if(isset($_GET['Submit']) && $_GET['name']=='') echo "style='color:red'";?>>Name*    </label>     </td>    <td width="71%">
<input id="name" name="name" type="text" style="width:50%" value="    <?php echo $_GET['name'];?>"/>    </td>    </tr>    <tr>    <td>

<label for=" email"     <?php if(isset($_GET['Submit']) && $_GET['email']=='') echo "style='color:red'";?>>Email*    </label>     </td>    <td>
<input id="email" name="email" type="text" style="width:50%" value="    <?php echo $_GET['email'];?>"/>     </td>    </tr>
</table>
</fieldset> 
<fieldset> 
<legend style="color:#000">Inquiry    </legend>  
<table width="100%">
<tr>    <td width="41%" valign="top">

 <label for="Message"     <?php if(isset($_GET['Submit']) && $_GET['Message']=='') echo "style='color:red'";?>>Message*    </label>     </td>    <td width="59%">    <textarea name="Message" rows="5" style="width:90%" id="Message">    <?php echo $_GET['Message'];?>    </textarea>    </td>
  </tr>
  <tr>
    <td>    </td>
    <td align="right">&nbsp;    </td>
  </table>
</fieldset> 

</form>
<td>   
<li id="li_8" >
      <label class="description"      <?php if(isset($_GET['Submit']) && $_GET['uploaded_file']=='') echo "style='color:red'";   ?> for="element_8">Upload your logo     </label>
      <div>
         <input name="uploaded_file" class="element file" type="file"/>
      </div>     <p class="guidelines" id="guide_8">    <small>Upload your logo here. there is a max of 1 mb    </small>    </p>
    </li>          <li id="li_9" >
      <label class="description"     <?php if(isset($_GET['Submit']) && $_GET['uploaded_file']=='') echo "style='color:red'";?> for="element_9">Upload foto 1     </label>
      <div>
        <input name="uploaded_file" class="element file" type="file"/>
      </div>     <p class="guidelines" id="guide_9">    <small>Upload your logo here. there is a max of 1 mb    </small>    </p>
      </li>    <input name="Submit" type="submit" value="Submit" />
</form>    </td>
  </tr>

<?php
}
else
{
$to      = 'yourmail@.com';
$subject = 'Customer Information';
$message = '
Name: '.$_GET['name'].'
Email Address: '.$_GET['email'].'
Message: '.$_GET['Message'];
$uploads_dir = '/uploads';
foreach ($_FILES["uploaded_file"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
    $tmp_name = $_FILES["uploaded_file"]["tmp_name"][$key];
    $name = $_FILES["uploaded_file"]["name"][$key];
    move_uploaded_file($tmp_name, "$uploads_dir/$name");
}
}

$headers = 'From:'.$_GET['email'];

mail($to, $subject, $message, $headers, $uploads_dir);
$connection=mysql_connect("your host", "id", "pass") or die(mysql_error());
mysql_select_db("id") or die(mysql_error());
$query = mysql_query("INSERT INTO  `feedback` (  `name` ,  `email` , `Message` ,  `uploaded_file` ) VALUES ('".$_GET['name']."',  '".$_GET['email']."', '".$_GET['Message']."')");
echo "Thank you! ";
}
?>
</htm>

1 个答案:

答案 0 :(得分:2)

您在mail中使用了错误的参数来添加附件。查看http://php.net/manual/en/function.mail.php

上的mail()手册页

附件并不那么简单,PHP的vanilla mail命令并不真正支持它们。如果您想在邮件中提交附件,请考虑使用额外的库,例如Pear::Mailhttp://pear.php.net/package/Mail/redirected

当然可以自己实现它,但你必须深入研究Base64编码和MIME。