我是 PHP 编程新手。我试图制作一个包含文件附件的表格。当我测试它时,它无法将表格发送到我的电子邮件。如果我遗漏了什么,请帮我看看我的 PHP 代码。提前感谢您的指导。我从http://www.shotdev.com/php/php-mail/php-send-email-upload-form-attachment-file/引用了这个编码
错误消息显示:警告:mail() [function.mail]: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 = "mytestmail@gmail.com";
$mailSubject = "The Form - $fullName";
$sendMail = mail($sendTo, $mailSubject, null, $header);
if($sendMail)
{
echo "Thank you!";
}
else
{
echo "An error occured.";
}
?>