2014-10-30 17 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"

相關問題