2017-03-06 97 views
-1

我创建了一个使用HTML和PHP的联系表格,但我有两个代码问题: 第一个:当我按下提交没有消息发送到我的邮件,我收到此错误[第25行错误是:邮件($ to,$ subject,$ header,$ body_message);] [1]。联系表格PHP和HTML错误

二:我得到一个错误,当我打开contact.php文件,如图片中[相关附件这些错误] [2]

最后,我想请请,如果任何人都可以给我如何我可以发送附加文件的引用,电子邮件作为我没有发现任何帮扶为

<!doctype html> 
 
<html> 
 
<head> 
 
<meta charset="utf-8"> 
 
<title>Untitled Document</title> 
 
</head> 
 

 
<body> 
 
<h2>Contact Form</h2> 
 
<p><span style="color: red" >*Required field</span></p> 
 
<form action="contact.php" method="post" enctype="multipart/form-data"> 
 
\t First Name:<input type="text" name="fname"><span style="color: red" >*</span><br><br> 
 
\t Last Name:<input type="text" name="lname"><span style="color: red" >*</span><br><br> 
 
\t E-mail:<input type="text" name="email"><span style="color: red" >*</span><br><br> 
 
\t Telephone:<input type="text" name="tel"><br><br> 
 
\t Designation:<select name="design"> 
 
    \t \t <option value="Architectural Engineer">Architectural Engineer</option> 
 
    \t \t <option value="Structural Engineer">Structural Engineer</option> 
 
    \t \t <option value="Draughts-man">Draughts-man</option> 
 
    \t \t <option value="Receptionist">Receptionist</option> 
 
    \t \t <option value="Secertary">Secertary</option> 
 
\t </select><br><br> 
 
\t Country Applied From:<select name="country"> 
 
\t \t <option value="">Country...</option> 
 
\t \t <option value="Afganistan">Afghanistan</option> 
 
\t \t <option value="Albania">Albania</option> 
 
\t \t <option value="Algeria">Algeria</option> 
 
\t \t <option value="American Samoa">American Samoa</option> 
 
</select><br><br> 
 
\t Message:<textarea name="message"></textarea> <br><br> 
 
\t Upload Your Resume:<span style="color: red" >*</span><input type="file" name="uploaded_file"><br><br> \t 
 
\t <input type="submit" name="submit" value="Submit"> 
 
\t <input type="reset" value="clear"> 
 
</form> 
 
</body> 
 
</html>

PHP代码补充说:

<?php 
 
if(isset($_POST['submit'])){ 
 
$fname = $_POST['fname']; 
 
$lname = $_POST['lname']; 
 
$email = $_POST['email']; 
 
$tel = $_POST['tel']; 
 
$design = $_POST['design']; 
 
$country = $_POST['country']; 
 
$message = $_POST['message']; 
 

 
$to = '[email protected]'; 
 
$subject = 'Contact Form'.$fname; 
 
$header = "$email"; 
 
$body_message = 'From: '.$fname .$lname."\n"; 
 
$body_message .= 'E-mail: '.$email."\n"; 
 
$body_message .= 'Telephone: '.$tel."\n"; 
 
$body_message .= 'Designation: '.$design."\n"; 
 
$body_message .= 'Country Appled From: '.$country."\n"; 
 
$body_message .= 'Message: '.$message."\n"; 
 
\t mail($to, $subject, $header, $body_message); 
 
} 
 

 
$name_of_uploaded_file = basename($_FILES['uploaded_file']['name']); 
 
$type_of_uploaded_file = substr($name_of_uploaded_file, strrpos($name_of_uploaded_file, '.') + 1); 
 
$size_of_uploaded_file = $_FILES["uploaded_file"]["size"]/1048576;//size in MBs 
 
$max_allowed_file_size = 500; // size in KB 
 
$allowed_extensions = array("jpg", "pdf", "docx", "doc"); 
 

 
if($size_of_uploaded_file > $max_allowed_file_size) { 
 
    $errors .= "\n Size of file should be less than $max_allowed_file_size"; 
 
} 
 
//------ Validate the file extension ----- 
 
$allowed_ext = false; 
 
for($i=0; $i<sizeof($allowed_extensions); $i++) 
 
{ 
 
    if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0) { 
 
     $allowed_ext = true; 
 
    } 
 
} 
 
if(!$allowed_ext) { 
 
    $errors .= "\n The uploaded file is not supported file type. ". 
 
" Only the following file types are supported: ".implode(',',$allowed_extensions); 
 
} 
 
?>

+2

**代码太多**。你需要更好地解决这个问题。我们不是*调试器。您需要**隔离问题**并从那里进行调试。如果您遇到困难,请使用[** Minimal,Complete和Verifiable示例**](http://stackoverflow.com/help/mcve)提供**清楚说明不工作的**。我建议阅读[问]一个好问题和[完美问题](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/)。另外,一定要参加[旅游]。 –

回答

0

它看起来像它不工作的原因是,你有邮件和标题才能在PHP邮件功能混合起来。

您采写:

mail($to, $subject, $header, $body_message); 

正确的格式: 邮件($到,$主题,$ body_message,$头);

PHP邮件()

bool mail (string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]]) 

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

0

您的电子邮件代码应该如下: -

$to  = '[email protected]'; 
$subject = 'the subject'; 
$message = 'hello'; 
$headers = 'From: [email protected]' . "\r\n" . 
    'Reply-To: [email protected]' . "\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 

$isSuccess = mail($to, $subject, $message, $headers); 

if($isSuccess == true) { // if mail is successfully sent 
    echo "Message sent successfully..."; 
}else{ 
    echo "Message could not be sent..."; 
} 

阅读documentation了解更多详情。