dsgwdigu84950 2016-07-04 17:44
浏览 29

简单的联系表单不发送邮件与PHP mail()函数[重复]

This question already has an answer here:

HTML:

<form action="php/send-contact.php" class="contact-form" name="contact-form" method="post">
  <div class="row">
    <div class="col-sm-6 cols">
      <input type="text" name="name" required="required" placeholder="Name*">
    </div>
    <div class="col-sm-6 cols">
      <input type="email" name="email" required="required" placeholder="Email*">
    </div>
    <div class="col-sm-6 cols">
      <input type="text" name="subject" required="required" placeholder="Subject*">
    </div>
    <div class="col-sm-12 cols">
      <textarea name="message" required="required" cols="30" rows="5" placeholder="Message*"></textarea>
    </div>
    <div class="col-sm-12 cols">
      <input type="submit" name="submit" value="Send Message" class="btn btn-send">
    </div>
  </div>
</form>

php/send-contact.php:

<?php
$name = @trim(stripslashes($_POST['name'])); 
$email = @trim(stripslashes($_POST['email']));
$subject = @trim(stripslashes($_POST['subject']));
$message = @trim(stripslashes($_POST['message'])); 
$email_from = $email;
$email_to = 'hello@domain.co.uk';//replace with your email
$body = 'Name: ' . $name . "

" . 'Email: ' . $email . "

" . 'Subject: ' . $subject . "

" . 'Message: ' . $message;
$success = @mail($email_to, $body, 'Name: ' . $name . "

" . 'Email: ' . $email . "

" . 'Subject: ' . $subject . "

" . 'Message: ' . $message);
?>
<!DOCTYPE HTML>
<html lang="en-US">

  <head>
    <script>
      alert("Thank you for getting in touch. We will contact you as soon as possible.");

    </script>
    <meta HTTP-EQUIV="REFRESH" content="0; url=../index.html">
  </head>

The HTML alert activates and refreshes as it should, but it doesnt send any email...

I have tried numerous email address recipients.

Also, are there any special measures I should take into account (regarding the PHP elements) when adding Google ReCaptcha to this form?

</div>
  • 写回答

1条回答 默认 最新

  • drt41563 2016-07-04 19:39
    关注

    So I have tested this, and I think it is doing what you want.

    $name = htmlentities($_POST['name']);
    $email_from = htmlentities($_POST['email']);  
    $subject = htmlentities($_POST['subject']); 
    $message = htmlentities($_POST['message']); 
    
    $email_to = 'Admin@Domain.com';//replace with your email
    
    $headers = "From: webmaster@example.com" . "
    " . "CC: ". $email_from; //This adds a from field, as well as CC's the person submitting the request.
    
    
    //Build the body of the eamil
    $body = 'Name: ' . $name . "
    
    " . 'Email: ' . $email_from . "
    
    " . 'Subject: ' . $subject . "
    
    " . 'message: ' . $message;
    
    $success = mail($email_to, "Contact from site X, regarding: ".$subject, $body,$headers);
    ?>
    <!DOCTYPE HTML>
    <html lang="en-US">
    
      <head>
      <?php if($success){  //If the email was sent correctly?> 
        <script>
    
          alert("Thank you for getting in touch. We will contact you as soon as possible.");
    
        </script>
    
        <?php header('Location: ../index.html'); }else{?>
            <script>
    
                alert("There was an error when sending the email, please try again later.");
    
        </script>
    
    <?php header('Location: ../index.html');  } //If the email falied?>
      </head>
    

    The other file (the html) remains the same. Simply replace your code in php/send-contact.php with this.

    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序