Php联系表,一直工作到迁移。警告:mail()[function.mail]:mail()函数参数错误,邮件未发送

时间:2014-01-01 01:14:27

标签: php forms email contact

所以继续这个问题,我已经有一段时间建立一个自定义联系表格。它的工作方式与我的开发(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上的单个目录访问。我多么喜欢偏执的客户!!!

对此的任何帮助将不胜感激!谢谢Lotz提前!!!

1 个答案:

答案 0 :(得分:3)

两种可能性。

首先,简单的一个:你的一些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应该是主标题的一部分,This is a MIME encoded message是正文的开头。


更复杂的一个:也许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";
}