2014-10-30 19 views
0

有人可以请看看这个非常简单的PHP文件上传表单,并告诉我我做错了什么,它在网站上工作正常,甚至收到确认消息。但是,我的电子邮件地址没有收到电子邮件。这是我在PHP表单上的第一次尝试。我的PHP上传表单工作,但没有收到电子邮件

<?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\r\nReply-To: myemail.com"; 
    $headers .= "\r\nMIME-Version: 1.0\r\nContent-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

的形式工作,但只盖注意正在收到,为什么其他字段不通过电子邮件发送?

+1

帮你一个忙,下载PHPMailer。它会做你想要的一切,消除你的头痛。 – 2014-10-30 18:44:13

+0

您有SMTP服务在测试此代码的位置运行吗? – yajakass 2014-10-30 18:44:43

+0

是的,它是在它将要保持的网页上。该网站实际上是一个设计网站,但网页设计师无法做到这一点,所以它已经下降到我身上! – 2014-10-30 18:46:19

回答

0

有几个问题与您的代码:你传递了错误的参数mail()

  • 。这条线:

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

    应该是:

    mail($to, $subject, $message, $headers); 
    
  • 你不是存储所有表单字段:

    你需要从$_POST检索所有表单域,并将其纳入您的信息。也许东西沿着这些线路:

    $message = "Name: " . $_POST['name'] . "\n"; 
    $message .= "Phone: " . $_POST['phone'] . "\n"; 
    $message .= "Email: " . $_POST['email'] . "\n"; 
    $message .= "Message:\n\n" . strip_tags($_POST['message']); 
    
  • 个人而言,我会检查文件类型对一个白名单,并只使用application/octet-stream未知类型。例如:

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

    然后在邮件正文中设置Content-Type: $type; name="$filename"

相关问题