php邮件功能在if条件下无效

时间:2014-06-21 10:43:11

标签: php

这是php中的邮件功能,但无法正常使用。 请帮我解决我的问题。

<?php

if($_POST && array_key_exists('submit_contact',$_POST)){
$to = "info@example.com";
$subject="Contact Form";
$fname =$_POST['fname'];
$email=$_POST['email'];
$telno=$_POST['telno'];
$mess=$_POST['your-message'];
$body = "<html><body><div>";
$body .= "<h1><b><u>Contact Form Details</u></b></h1>";
$body .= "<h3>Full Name:\t". $fname."</h3>";
$body .= "<h3>E-mail:\t". $email."</h3>";
$body .= "<h3>Telephone:\t". $telno."</h3>";
$body .= "<h3>Message:</h3>\t<p>". $mess."</p>";
 $body .= "</div></body></html>";
$headers = "From:". $email. "\r\n";
 $headers .= "MIME-Version: 1.0" . "\r\n";
 $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
  $headers .= "Reply-To:".$email."\r\n"."Return-Path:". $email . "\r\n". "X-Mailer: PHP/" . phpversion();
   $subjectr="Confirmation Notice";
  $messages="<html><body>";
      $messages .="<div style=''><a href='index.html'><img src='http://example.com/images/logo.png' alt='logo'/></a></div>";
   $messages .="<table style=''width='600px'><tr><td bgcolor='black'height='30px'><font style='padding-left:10px;color:#d8b53e;font-size:20px;'>Reference # 786733</font></td> </tr>";
    $messages .="<td><br/>Dear Concerned,<br/>Thank you for considering</td></tr>";
  $messages .="<tr><td>&#169; 2014</td></tr></table>";
 $messages .="</body></html>";
  $headerss = "From:".$to. "\r\n";
  $headerss .= "MIME-Version: 1.0" . "\r\n";
   $headerss .= "Content-type:text/html;charset=UTF-8" . "\r\n";
    $headerss .= "Reply-To:".$to."\r\n"."Return-Path:".$to. "\r\n". "X-Mailer: PHP/" . phpversion();
 if(mail($to,$subject,$body,$headers)){
    mail($email,$subjectr,$messages,$headerss);
      echo "<script> window.alert('E-mail successfully Sent!');</script>";
 }
else{
echo "<script> window.alert('Error Try Again Please');</script>";
}
}
?>

1 个答案:

答案 0 :(得分:1)

首先,如果将函数放在if条件中,它将自动调用。 所以,如果你写

if(mail($to, $subject, $body, $headers) {
    echo "Send!";
} else {
    echo "Error";
}

电子邮件将获得发送,并在成功时返回true,否则为false。