PHP邮件表单无法发送

时间:2012-04-18 08:10:15

标签: php

我是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.";
}
?>

2 个答案:

答案 0 :(得分:1)

我没有检查你所有的代码......但是......

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

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

答案 1 :(得分:0)

@ F3rr31r4显示警告:mail()[function.mail]:mail()函数参数错误,邮件未发送。它指向行$ sendMail = mail($ sendTo,$ mailSubject,null,$ header);还有一个文本“发生错误。”,这是我在IF语句中放在代码末尾的文本。 - user1340749

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