doukun0888 2015-09-07 05:20
浏览 27
已采纳

PHP电子邮件功能不起作用[重复]

This question already has an answer here:

Below is the code for the basic email function.script. It actually uses a form on a website to set the variables in the script above to send an email. But its not working perfectly, please help me to fix it!

 <?php
//if "email" variable is filled out, send email
  if (isset($_REQUEST['email']))  {

  //Email information
  $admin_email = "someone@example.com";
  $email = $_REQUEST['email'];
  $subject = $_REQUEST['subject'];
  $comment = $_REQUEST['comment'];

  //send email
  mail($admin_email, "$subject", $comment, "From:" . $email);

  //Email response
  echo "Thank you for contacting us!";
  }

  //if "email" variable is not filled out, display the form
  else  {
?>

 <form method="post">
  Email: <input name="email" type="text" /><br />
  Subject: <input name="subject" type="text" /><br />
  Message:<br />
  <textarea name="comment" rows="15" cols="40"></textarea><br />
  <input type="submit" value="Submit" />
  </form>

<?php
  }
?>
</div>
  • 写回答

4条回答 默认 最新

  • duandi2853 2015-09-07 05:49
    关注

    Try this - Add header in your code.

    //if "email" variable is filled out, send email
    if (isset($_REQUEST['email']))  {
    
     //Email information
     $admin_email = "someone@example.com";
     $email = $_REQUEST['email'];
     $subject = $_REQUEST['subject'];
     $comment = trim($_REQUEST['comment']);
    
     $headers  = 'From:' .$email. "
    " .
                                        'Reply-To: info@domain.com' . "
    " .
                                        'MIME-Version: 1.0' . "
    " .
                                        'Content-type: text/html; charset=iso-   8859-1' . "
    " .
                                        'X-Mailer: PHP/' . phpversion();
    
      //send email
      $result = mail($admin_email, $subject, $comment, $headers);
    
      //Email response
       if($result)
        echo "Thank you for contacting us!";
       else
         echo "Something went wrong.";
       }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?