所以这就是问题,我已经有一段时间在构建自定义联系表单了.它的工作方式与我在开发(Php 5.3.10)服务器上的假定方式相同.当我将表格上传到Godaddy主机并进行测试时,会发生问题.我得到以下内容:
Warning: mail() [function.mail]: Bad parameters to mail() function, mail not sent. in index.php on line 146
此行指出:
if (mail($mailto, $subject, "", $headers)) {
这是有问题的完整脚本:
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
// define variables and set to empty values
$nameErr = $emailErr = $commentErr = $companyErr = $phoneErr = "";
$name = $email = $company = $comment = $phone = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["name"]))
{$nameErr = "Name is required";}
else
{
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name))
{
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["company"]))
{$companyErr = "company is required";}
else
{$company = test_input($_POST["company"]);}
if (empty($_POST["email"]))
{$emailErr = "Email is required";}
else
{
$email = test_input($_POST["email"]);
// check if e-mail address syntax is valid
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
{
$emailErr = "Invalid email format";
}
}
if (empty($_POST["phone"]))
{$phoneErr = "phone is required";}
else
{$phone = test_input($_POST["phone"]);}
if (empty($_POST["comment"]))
{$commentErr = "comment is required";}
else
{$comment = test_input($_POST["comment"]);}
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<table>
<tr>
<span class="error">* required field.</span>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<td>Name:</td>
<td><input type="text" name="name"><span class="error">*</td>
<td><?php echo $nameErr;?></span></td>
</tr><tr>
<td>Company:</td>
<td><input type="text" name="company"><span class="error">*</td>
<td><?php echo $companyErr;?></span></td>
</tr><tr>
<td>E-mail:</td>
<td><input type="text" name="email"><span class="error">*</td>
<td><?php echo $emailErr;?></span></td>
</tr><tr>
<td>Phone Number:</td>
<td><input type="text" name="phone"><span class="error">*</td>
<td><?php echo $phoneErr;?></span></td>
</tr><tr>
<td>What type of<br /> Audit are you<br /> seeking?:</td>
<td><textarea name="comment" rows="5" cols="40"></textarea><span class="error">*</td>
<td><?php echo $commentErr;?></span></td>
</tr><tr>
<td></td><td><input type="submit" name="submit" value="Submit"></td>
</tr>
</form>
</table>
</form>
<?php //Writing out form to contacts/$email.txt
$fh = fopen("contacts/$email.txt", "w");
fwrite($fh, $name.PHP_EOL.$company.PHP_EOL.$email.PHP_EOL.$phone.PHP_EOL.$comment);
fclose($fh);
?>
<?php
if ('POST' === $_SERVER['REQUEST_METHOD']) // wait for form submission
{
$attch1 = $_POST['email']; // files are named after email address
$file = "contacts/$attch1.txt"; // file location
$subject = "New Contact from form w/ attachment"; // subject to email
$message = "You have a new contact from your form. The information is found as an attachment as well as on your server at http://yourhostname.com/contacts/$attch1.txt"; // email (body) message
$file_size = filesize($file); // file size
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$mailto = 'SENDTO.com'; // Mail to address
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// main header (multipart mandatory)
$headers = "From: $attch1" . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$headers .= "This is a MIME encoded message." . $eol . $eol;
// message
$headers .= "--" . $separator . $eol;
$headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$headers .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;
$headers .= $message . $eol . $eol;
// attachment
$headers .= "--" . $separator . $eol;
$headers .= "Content-Type: application/octet-stream; name=\"" . $file . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: base64" . $eol;
$headers .= "Content-Disposition: attachment" . $eol . $eol;
$headers .= $content . $eol . $eol;
$headers .= "--" . $separator . "--";
//SEND Mail
if (mail($mailto, $subject, "", $headers)) {
echo "Thank you for submitting our form successfully"; // or use booleans here
} else {
echo "Submission ERROR! Please try again";
}
}
?>
</body>
</html>
我猜这是一个问题,因为它在我的开发服务器上可以正常工作.客户端没有给我访问任何Godaddy信息的权限,并且只允许我对FTP进行单个目录访问.我多么爱偏执的客户!!!
任何帮助,将不胜感激!提前感谢洛兹!!!
解决方法:
两种可能性.
首先,简单一点:您的某些EOL看起来不对.主标题应为:
$headers = "From: $attch1" . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol . $eol;
$headers .= "This is a MIME encoded message." . $eol . $eol;
在主标题和主体之间应该有两个EOL. Content-Transfer-Encoding应该是主标头的一部分,这是MIME编码的消息的主体.
更为复杂的一个:也许GoDaddy运行的PHP版本不允许您将消息内容放在$additional_headers参数中.消息正文应在第三个参数中发送到mail().尝试这个:
<?php
// main header (multipart mandatory)
$headers = "From: $attch1" . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol
$headers .= "Content-Transfer-Encoding: 7bit";
// message
$body = "This is a MIME encoded message." . $eol . $eol;
$body .= "--" . $separator . $eol;
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;
$body .= $message . $eol . $eol;
// attachment
$body .= "--" . $separator . $eol;
$body .= "Content-Type: application/octet-stream; name=\"" . $file . "\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol;
$body .= "Content-Disposition: attachment" . $eol . $eol;
$body .= $content . $eol . $eol;
$body .= "--" . $separator . "--";
//SEND Mail
if (mail($mailto, $subject, $body, $headers)) {
echo "Thank you for submitting our form successfully"; // or use booleans here
} else {
echo "Submission ERROR! Please try again";
}