开发者

Header in email not working

开发者 https://www.devze.com 2023-04-10 11:09 出处:网络
I am using开发者_Go百科 the following script below.The email gets sent correctly except the html tags don\'t work.What is wrong here?

I am using开发者_Go百科 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>";
   }


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)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号