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

Posted

技术标签:

【中文标题】PHP 联系表,一直工作到迁移。警告:mail() [function.mail]:mail() 函数的参数错误,邮件未发送【英文标题】:Php contact form, worked until migration. Warning: mail() [function.mail]: Bad parameters to mail() function, mail not sent 【发布时间】:2014-01-18 19:51:09 【问题描述】:

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

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

【问题讨论】:

您的开发服务器上是否启用了错误和警告? 如果输入非空消息会怎样? 您的错误是您迁移到的当前主机不支持您传递的标头,或者至少不支持您传递的变量中的某些数据。最常见的是 \r\n 试图在标题中使用。 另外,如果将$attch1 更改为something@your-domain-on-godaddy.com 并将Reply-To 设置为$_POST['email'] 会发生什么? 我在开发服务器上启用了错误和警告。仍然在那里工作。将尝试从标题中删除 \r\n。 【参考方案1】:

两种可能性。

首先,简单的一个:您的某些 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";

【讨论】:

G'rrr 尝试加载一个空白页面。仍然没有显示错误。当我启用它们时,Php 没有向我显示任何问题是怎么回事?感谢您尝试帮助我。 这是一个语法错误。标题中的Content-Type 行缺少; 谢谢迈克!并感谢 Barmar 给了我更好的格式:D。这似乎在开发人员服务器上工作。迁移到 godaddy 的过程并不顺利。同样的问题 @MarvilJoy 在最后一个分隔符之前添加另一个分隔符、附件标题和内容。如果您尝试过但没有成功,请使用您的代码发布问题。

以上是关于PHP 联系表,一直工作到迁移。警告:mail() [function.mail]:mail() 函数的参数错误,邮件未发送的主要内容,如果未能解决你的问题,请参考以下文章

如何删除 CORB 警告?

最新 955 不加班的公司名单(2022版)

最新 955 不加班的公司名单(2022版)

警告:mail() [function.mail]:无法在“localhost”端口 25 连接到邮件服务器

最新 955 不加班的公司名单(2022版)

最新 955 不加班的公司名单(2022版)