如何在使用 PHP 邮件程序成功发送电子邮件后打印 html 中的消息?

How to Print a Message in html After Successfully Sending an Email Using a PHP mailer?

我正在使用 PHP 邮件发送我的 html 联系表,使用 action="mail.php"。好吧,我从 youtube 教程中获取了代码,我唯一的问题是这行代码 <?php print r($message);?> 为了使这行代码工作,索引文件必须 be.php 所以我必须使用 form method="post" enctype="multipart/form-data" 而不是 action="mail.php" 我测试了它,它工作得很好;按下发送按钮后,我在联系表的顶部收到了一条打印消息。但是,如果我将 PHP 配置和 link 分开到我的 html 联系表格中,该表格仍会执行并且我会收到电子邮件,但打印消息不会出现,因为它是 html。请找到以下代码。

          <form method="post" action="mail.php" role="form">
                 <div class="row">
                    <div class="col-md-6">
                       <div class="form-group">
                          <label>Full Name</label>
                          <input type="text" name="name" placeholder="Full Name" class="form- 
             control" required />
                       </div>
                       <div class="form-group">
                          <label>Select whether you are a supplier or a restaurant</label>
                          <select name="restosupp" class="form-control" required>
                             <option value="">I am a *</option>
                             <option value="Supplier">Supplier</option>
                             <option value="Restaurant">Restaurant</option>
                          </select>
                       </div>
                       <div class="form-group">
                          <label>Enter Email Address</label>
                          <input type="email" name="email" class="form-control" 
       placeholder="Enter Email Address" required />
                       </div> 
                    </div>
                    <div class="col-md-6">
                       <div class="form-group">
                          <label>Company Name</label>
                          <input type="text" name="name2" placeholder="Company Name" 
           class="form-control" required />
                       </div>
                       <div class="form-group">
                          <label>Enter Address</label>
                          <textarea name="address" placeholder="Enter Address" class="form- 
              control" required rows="1"></textarea>
                       </div>
                       <div class="form-group">
                          <label>Enter Mobile Number</label>
                          <input type="text" name="mobile" placeholder="Enter Mobile Number" 
              class="form-control" pattern="\d*" required />
                       </div>
                    </div>
                    <div class="col-md-12">
                       <div class="form-group">
                          <label>Message</label>
                          <textarea name="additional_information" placeholder="Enter Your 
                    Message" class="form-control" required rows="2"></textarea>
                       </div>
                    </div>
                 </div>
                 <div class="form-group pt-4" text-center>
                    <input type="submit" name="submit" class="interested-btn btn-send" 
        value="Send message"/>
                 </div>
              </form>


              

     <?php

     $message = '';

   function clean_text($string)
 {
$string = trim($string);
$string = stripslashes($string);
$string = htmlspecialchars($string);
return $string;
 }

   if (isset($_POST["submit"])) {
  $programming_languages = '';
     foreach ($_POST["programming_languages"] as $row) {
    $programming_languages .= $row . ', ';
    }
      $programming_languages = substr($programming_languages, 0, -2);
      $message               = '
    <h3 align="center">I am interested form </h3>
    <table border="1" width="100%" cellpadding="5" cellspacing="5">
        <tr>
            <td width="30%">Name</td>
            <td width="70%">' . $_POST["name"] . '</td>
        </tr>
        <tr>
            <td width="30%">Company name</td>
            <td width="70%">' . $_POST["name2"] . '</td>
        </tr>
        <tr>
            <td width="30%">I am a</td>
            <td width="70%">' . $_POST["restosupp"] . '</td>
        </tr>
        <tr>
            <td width="30%">Address</td>
            <td width="70%">' . $_POST["address"] . '</td>
        </tr>
        <tr>
            <td width="30%">Email Address</td>
            <td width="70%">' . $_POST["email"] . '</td>
        </tr>
        <tr>
            <td width="30%">Phone Number</td>
            <td width="70%">' . $_POST["mobile"] . '</td>
        </tr>
        <tr>
            <td width="30%">The Message</td>
            <td width="70%">' . $_POST["additional_information"] . '</td>
        </tr>
      </table>
      ';

require 'class/class.phpmailer.php';
$mail = new PHPMailer;
$mail->IsSMTP(); //Sets Mailer to send message using SMTP
$mail->Host       = 'smtp.gmail.com'; //Sets the SMTP hosts of your Email hosting, this for Godaddy
$mail->Port       = '465'; //Sets the default SMTP server port
$mail->SMTPAuth   = true; //Sets SMTP authentication. Utilizes the Username and Password variables
$mail->Username   = 'email@gmail.com'; //Sets SMTP username
$mail->Password   = 'password'; //Sets SMTP password
$mail->SMTPSecure = 'ssl'; //Sets connection prefix. Options are "", "ssl" or "tls"
$mail->From       = $_POST["email"]; //Sets the From email address for the message
$mail->FromName   = $_POST["name"]; //Sets the From name of the message
$mail->AddAddress('email1', 'email2'); //Adds a "To" address
$mail->WordWrap = 50; //Sets word wrapping on the body of the message to a given number of characters
$mail->IsHTML(true); //Sets message type to HTML
$mail->AddAttachment($path); //Adds an attachment from a path on the filesystem
$mail->Subject = 'Application from i am interested form'; //Sets the Subject of the message
$mail->Body    = $message; //An HTML or plain text message body
if ($mail->Send()) //Send an Email. Return true on success or false on error
    {
    $message = '<div class="alert alert-success">Application Successfully Submitted</div>';
    unlink($path);
} else {
    $message = '<div class="alert alert-danger">There is an Error</div>';
}}
?>

将消息放入会话中,提交表单后重定向到当前页面并显示保存在会话中的消息,然后清除该会话消息(即闪消息)。