电子邮件中的标题无效

时间:2011-10-04 20:12:07

标签: php email

我正在使用以下脚本。除了html标记不起作用外,电子邮件才能正确发送。这有什么不对?

  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>";
   }

2 个答案:

答案 0 :(得分:2)

标题应该是第四个参数。现在,您在那里传递'FROM'标头,并将MIME标头作为第五个传递。这就是MIME标头不用作标头的原因。在第四个参数中组合标题,如下所示:

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

(感激地借鉴http://www.webhostingtalk.com/showthread.php?t=416467

答案 1 :(得分:1)

看一下PHP的mail函数。你这样称呼它:

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

"From: $usermail"被解释为additional_headers,您的标题被解释为additional_parameters。因此,您的自定义标头未设置。