0

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

2 回答 2

1

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

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

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

于 2012-04-18T08:14:52.343 回答
0

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

 1. Check your PHP version
 2. print("<pre>" . $header . "</pre>");  and check your msg
于 2012-04-18T09:40:56.313 回答