接触表格PHP和HTML错误



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

第二:我打开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">
	First Name:<input type="text" name="fname"><span style="color: red" >*</span><br><br>
	Last Name:<input type="text" name="lname"><span style="color: red" >*</span><br><br>
	E-mail:<input type="text" name="email"><span style="color: red" >*</span><br><br>
	Telephone:<input type="text" name="tel"><br><br>
	Designation:<select name="design">
  		<option value="Architectural Engineer">Architectural Engineer</option>
  		<option value="Structural Engineer">Structural Engineer</option>
  		<option value="Draughts-man">Draughts-man</option>
  		<option value="Receptionist">Receptionist</option>
  		<option value="Secertary">Secertary</option>
	  </select><br><br>
	  Country Applied From:<select name="country">
		<option value="">Country...</option>
		<option value="Afganistan">Afghanistan</option>
		<option value="Albania">Albania</option>
		<option value="Algeria">Algeria</option>
		<option value="American Samoa">American Samoa</option>
</select><br><br>
	Message:<textarea name="message"></textarea> <br><br>
	Upload Your Resume:<span style="color: red" >*</span><input type="file" name="uploaded_file"><br><br>	
	<input type="submit" name="submit" value="Submit">
	<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 = 'eng.bolaraafat@hotmail.com';
$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";
	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);
}
?>

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

$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "rn" .
    'Reply-To: webmaster@example.com' . "rn" .
    '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...";
}

阅读文档以获取更多详细信息。

它看起来不起作用的原因是您在PHP邮件函数中混合了消息和标头订单。

您已经写了:

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

正确格式:邮件($ to,$ object,$ body_message,$ header);

php mail()

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

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

最新更新