2012-04-18 81 views
0

我是PHP編程新手。我試圖製作一個包含文件附件的表單。當我測試它時,它未能將表格發送到我的電子郵件。請幫我看看我的PHP代碼,如果我錯過了任何東西。預先感謝您的指導。我提到這個編碼從http://www.shotdev.com/php/php-mail/php-send-email-upload-form-attachment-file/PHP郵件表單未能發送

錯誤信息顯示: 警告:mail()[function.mail] 它指向「$ sendMail = mail($ sendTo,$ mailSubject,null,$ header);」

下面是我的PHP代碼:

<?php 

/*Retrive inputs from the form */ 
//Required fields 
$fullName = $_POST["Fullname"]; 
$gender = $_POST["genderRadioGroup"]; 
$DOB = $_POST["DOB"]; 
$address = $_POST["Address"]; 
$state = $_POST["State"]; 
$zipCode = $_POST["Zipcode"]; 
$contactNo = $_POST["Contact"]; 
$emailAdd = $_POST["Email"]; 


//Optional fields 
// If applicant filled the field, take it. Else, make it " - ". 
$nickName = $_POST["Nickname"]; 
if($nickName!="") 
{ 
    $nickName = $nickName; 
} 
else 
{ 
    $nickName = " - "; 
} 

$description = $_POST["Description"]; 
if($description!="") 
{ 
    $description = $description; 
} 
else 
{ 
    $description = " - "; 
} 

//Message content 
$message = <<<EOD 
<br><hr><br> 
Full Name: $fullName <br> 
Nickname: $nickName <br> 
Gender: $gender <br> 
Date of Birth: $DOB <br> 
Address: $address <br> 
State: $state <br> 
Zip Code: $zipCode <br> 
Contact Number: $contactNo <br> 
E-mail address: $emailAdd <br> 
Description: $description <br> 
EOD; 

/* Uniqid Session */ 
$sessionID = md5(uniqid(time())); 

/* Header */ 
//Sender 
$header = "From: $fullName <$emailAdd> \n"; 
$header .= "Reply-To: $emailAdd \n"; 
//MIME 
$header .= "MIME-Version: 1.0 \n"; 
$header .= "Content-type: multipart/mixed; boundary=\"".$sessionID."\"\n\n"; 
$header .= "This is a multi-part message in MIME format. \n"; 

$header .= "-- $sessionID \n"; 
$header .= "Content-type: text/html; charset=utf-8 \n"; 
$header .= "Content-transfer-encoding: 7bit \n\n"; 
$header .= "$message \n\n"; 

/* Attachment */ 
if($_FILES["Attachment"]["name"] != "") //If there is attachment 
{ 
    //Variables 
    $fileName = $_FILES["Attachment"]["name"]; 
    $fileContent = chunk_split(base64_encode(file_get_contents($_FILES["Attachment"]["tmp_name"]))); 

    //Adding attachment to header 
    $header .= "-- $sessionID \n\n"; 
    $header .= "Content-Type: application/octet-stream; name=\"".$fileName."\"\n"; 
    $header .= "Content-Transfer-Encoding: base64 \n"; 
    $header .= "Content-Disposition: attachment; filename=\"".$fileName."\"\n\n"; 
    $header .= $fileContent."\n\n"; 
} 

/* Send Mail */ 
// Recipient 
$sendTo = "[email protected]"; 
$mailSubject = "The Form - $fullName"; 
$sendMail = mail($sendTo, $mailSubject, null, $header); 

if($sendMail) 
{ 
    echo "Thank you!"; 
} 
else 
{ 
    echo "An error occured."; 
} 
?> 
+0

您正試圖在標題中嵌入電子郵件的內容。電子郵件的格式不完整,行結束是錯誤的....省去很多痛苦,並找到一個工具爲你做這件事(phpmailer或swiftmailer) – symcbean 2012-04-18 12:26:45

回答

1

我沒有檢查你所有的代碼..但...

mail( $to,   $subject,  $message,  $headers); 
    mail( $sendTo, $mailSubject,  null,  $header); 

http://php.net/manual/en/function.mail.php

+0

感謝您的及時迴應。正如我從我參考的網站了解到的,$消息被設置爲「空」,因爲消息內容已經包含在$頭中。這是錯的嗎? – user1340749 2012-04-18 08:21:50

+0

我一直在努力,但我可以推薦你使用一個類(如http://phpmailer.worxware.com/),它將設置所有正確的標題等。 – SativaNL 2012-04-18 08:27:21

+0

是的,你可以做到這一點,原文是mail(string $ to,string $ subject,string $ message [,string $ additional_headers [,string $ additional_parameters]])......檢查手冊和例子;現在,你說它失敗了發送,什麼是味精錯誤? – F3rr31r4 2012-04-18 09:00:50

0

@ F3rr31r4它顯示警告:郵件()[function.mail]:參數不正確的郵件()函數,郵件未發送。它指向$ sendMail = mail($ sendTo,$ mailSubject,null,$ header)行;還有文字「發生錯誤」,這是我在代碼結尾處放入IF語句中的那個。 - user1340749

1. Check your PHP version 
2. print("<pre>" . $header . "</pre>"); and check your msg 
+0

我已經測試過,但是如何在這裏顯示輸出?有很多行,因爲它包含圖像附件。 – user1340749 2012-04-18 10:15:01

+0

我看到你刪除了其他味精,你糾正了你的問題? – F3rr31r4 2012-04-19 08:11:59