AAA
AAA

Reputation: 3168

Header in email not working

I am using the following script below. The email gets sent correctly except the html tags don't work. What is wrong here?

  function spamcheck($field)
  {
 //filter_var() sanitizes the e-mail
 //address using FILTER_SANITIZE_EMAIL
 $field=filter_var($field, FILTER_SANITIZE_EMAIL);

 //filter_var() validates the e-mail
 //address using FILTER_VALIDATE_EMAIL
 if(filter_var($field, FILTER_VALIDATE_EMAIL))
   {
  return TRUE;
  }
 else
  {
  return FALSE;
   }
   }

 if (isset($_REQUEST['email']))
 {//if "email" is filled out, proceed

  //check if the email address is invalid
    $mailcheck = spamcheck($_REQUEST['email']);
  if ($mailcheck==FALSE)
   {
   echo "Invalid input";
    }
   else
   {//send email
    $headers = "MIME-Version: 1.0\r\n";
     $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
      $email = $_REQUEST['email'];
      $subject = "My Email";
     $message = "<html><body>";
     $message .= "<h1>Hello</h1>";
      $message .= "Let us know if you have any questions.";
      $message .= "</body></html>";
      mail("$email", "Subject: $subject",
$message, "From: $usermail", "$headers" );
    echo "Thank you for using our mail form";
    }
    }
   else
   {
  //if "email" is not filled out, display the form
  echo "<form method='post' action='mailform.php'>
  Email: <input name='email' type='text' /><br />
  Subject: <input name='subject' type='text' /><br />
   Message:<br />
  <textarea name='message' rows='15' cols='40'>
  </textarea><br />
  <input type='submit' />
  </form>";
   }

Upvotes: 0

Views: 461

Answers (2)

GolezTrol
GolezTrol

Reputation: 116180

The headers should be the fourth parameter. Now you pass the 'FROM' header there, and pass the MIME headers as the fifth. That's why the MIME header is not used as a header. Combine the headers in the fourth parameter like so:

$headers = "MIME-Version: 1.0rn"; 
$headers .= "Content-type: text/html; charset=iso-8859-1rn"; 
$headers  .= "From: $usermail\r\n"; 

// now lets send the email. 
mail($to, $subject, $message, $headers); 

(gratefully borrowed from http://www.webhostingtalk.com/showthread.php?t=416467)

Upvotes: 2

steveo225
steveo225

Reputation: 11892

Take a look at PHP's mail function. You are calling it as such:

mail("$email", "Subject: $subject", $message, "From: $usermail", "$headers" );

The "From: $usermail" is being interpreted as the additional_headers, and your headers are being interpreted as additional_parameters. Thus, your custom headers are not being set.

Upvotes: 1

Related Questions