Php contact form, worked until migration. Warning: mail() [function.mail]: Bad parameters to mail() function, mail not sent

StackOverflow https://stackoverflow.com/questions/20864757

  •  23-09-2022
  •  | 
  •  

سؤال

So heres the issue, I have been on this for awhile building a custom contact form. Its works the way its suppose to on my development (Php 5.3.10) server. Issue occurs when I upload the form to godaddy host and test. I get the following:

Warning: mail() [function.mail]: Bad parameters to mail() function, mail not sent. in index.php on line 146 

this line states:

if (mail($mailto, $subject, "", $headers)) {

Here is the complete script in question:

    <!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>

I am guessing that this is a godaddy issue since it works flawlessly on my development server. Client didnt give me access to any godaddy information and only a single directory access on the FTP. How I love paranoid clients!!!

Any help on this would be greatly appreciated! Thanks Lotz in advance!!!

هل كانت مفيدة؟

المحلول

Two possibilities.

First, the easy one: Some of your EOLs look wrong. The main header should be:

$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;

There are supposed to be two EOLs between the main header and the body. Content-Transfer-Encoding should be part of the main header, This is a MIME encoded message is the beginning of the body.


The more complicated one: Maybe the version of PHP that GoDaddy is running doesn't allow you to put the message content in the $additional_headers parameter. The message body should be sent in the third argument to mail(). Try this:

<?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";
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top