Skip to content
Advertisement

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

I’m using PHP mailer to my html contact form using action=”mail.php”. Well, I took the code from a youtube tutorial, my only problem is with this line of code <?php print r($message);?> in order for this line of code to work, the index file must be.php so I must use form method="post" enctype="multipart/form-data" instead of action=”mail.php” I tested it and it worked perfectly; I received a print message at the top of the contact form after pressing the send button. However, if I separate the PHP configuration and link it to my html contact form, the form will still perform and I will receive the email, but the print message will not appear because it is html. Please find the following code.

          <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>';
}}
?>

Advertisement

Answer

Put the message in session and after submit the form redirect to the curent page and show the message saved in the session and after that clear that session message (a flash message).

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement