doutuohan6606 2014-10-30 18:40
浏览 35
已采纳

too long

Could somebody please take a look at this extremely simple php file upload form and tell me what I am doing wrong, it all works fine on the website, even recieving the confirmation message. However no email is being recieved at my email address. This is my first attempt at a PHP form.

<?php
if(isset($_POST['submit']))
{
    //The form has been submitted, prep a nice thank you message
    $output = '<h1>Your Application Has Been Received</h1>';
    //Set the form flag to no display (cheap way!)
    $flags = 'style="display:none;"';

    //Deal with the email
    $to = 'myemail.com';
    $subject = 'Application';

    $message = strip_tags($_POST['message']);
    $attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'])));
    $filename = $_FILES['file']['name'];

    $boundary =md5(date('r', time())); 

    $headers = "From: myemail.com
Reply-To:   myemail.com";
    $headers .= "
MIME-Version: 1.0
Content-Type: multipart/mixed;   boundary=\"_1_$boundary\"";

    $message="This is a multi-part message in MIME format.


--_1_$boundary
Content-Type: multipart/alternative; boundary=\"_2_$boundary\"

--_2_$boundary
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit

$message

--_2_$boundary--
--_1_$boundary
Content-Type: application/octet-stream; name=\"$filename\" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

$attachment
--_1_$boundary--";

    mail($to, $subject, $name, $email, $headers);
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>MailFile</title>
</head>

<body>

<?php echo $output; ?>

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" <?php echo $flags;?>>
<label for="name">Name</label><input type="name" name="name" id="name"></p>
<p>
<label for="phone">Telephone</label> <input type="phone" name="phone" id="phone"></p>
<p> 
<label for="email">Email</label> <input type="email" name="email" id="email"></p>
<p>
<label for="message">Cover Note</label> <textarea name="message" id="message" cols="20" rows="8">  </textarea></p>
<p><label for="file">File</label> <input type="file" name="file" id="file"></p>
<p><input type="submit" name="submit" id="submit" value="send"></p>
</form>
</body>
</html>

UPDATE

The form is working but only the Cover Note is being recieved, why are the other fields not sending by email?

  • 写回答

1条回答 默认 最新

  • duanchi1230 2014-10-30 19:30
    关注

    There are a few problems with your code:

    • You're passing the wrong arguments to mail(). This line:

      mail($to, $subject, $name, $email, $headers);
      

      should be:

      mail($to, $subject, $message, $headers);
      
    • You're not storing all the form fields:

      You need to retrieve all the form fields from $_POST and incorporate them into your message. Perhaps something along these lines:

      $message  = "Name: " . $_POST['name'] . "
      ";
      $message .= "Phone: " . $_POST['phone'] . "
      ";
      $message .= "Email: " . $_POST['email'] . "
      ";
      $message .= "Message:
      
      " . strip_tags($_POST['message']);
      
    • Personally I would check the file type against a white-list, and only use application/octet-stream for unknown types. For example:

      $type = $_FILES['file']['type'];
      if (!in_array ($type, array ('text/plain', 'image/png', 'audio/wav'))) {
        $type = 'application/octet-stream';
      }
      

      And then set Content-Type: $type; name="$filename" in the message body.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?