Php联系人表单,一直工作到迁移。警告:mail()[function.mail]:mail()函数的参数错误,邮件未发送
问题就在这里,我已经在这上面做了一段时间了,建立一个自定义的联系表单。它在我的开发(PHP5.3.10)服务器上的工作方式与设想的相同。当我将表单上传到godaddy主机并进行测试时,出现问题。我得到以下信息:Php联系人表单,一直工作到迁移。警告:mail()[function.mail]:mail()函数的参数错误,邮件未发送,php,forms,email,contact,Php,Forms,Email,Contact,问题就在这里,我已经在这上面做了一段时间了,建立一个自定义的联系表单。它在我的开发(PHP5.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,
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>
.错误{color:#FF0000;}
*必填字段。
有两种可能性
首先,简单的一点:你的一些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<代码>内容传输编码
应该是主标题的一部分,这是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";
}
开发服务器上是否启用了错误和警告?如果输入非空消息会发生什么情况?错误是您迁移到的当前主机不支持您要传递的头,或者至少不支持您要传递的变量中的某些数据。最常见的情况是试图在标题中使用\r\n
。此外,如果将$attch1
更改为something@your-在godaddy.com上的域
并将回复
设置为$\u POST['email']
?我在开发服务器上启用了错误和警告。还在那里工作。将尝试从标题中删除\r\n。G'rrr尝试并加载空白页。仍然没有显示错误。当我启用Php时,Php没有向我显示任何问题,这是怎么回事?谢谢你帮我。这是个语法错误。缺少标题的内容类型
行中的code>谢谢迈克!感谢Barmar为我提供了更好的格式:D。这在开发者服务器上似乎很有效。迁移到戈达迪并不顺利。同样的issue@MarvilJoy在最后一个分隔符之前添加另一个分隔符、附件标题和内容。如果您尝试了,但没有成功,请用您的代码发布一个问题。