Search code examples
phpformsmessage

Adding PHP to form email response


I want the 'name' from the form input (http://besthomebuildercoloradosprings.com/) to display in the auto response email (after the word 'Congratulations'). It currently doesn't display within my html despite inserting <?php echo $name; ?>. I've looked all over the forums and experimented but nothing works.

Here are the contents to my webformmailer.php file:

          <?php
      $name = $_POST['name'];
      $email = $_POST['email'];
      $phone = $_POST['phone'];
      $bonus = $_POST['bonus'];
      $formcontent=" Hello, \n \nThis email has been sent to you from the Challenger Homes BestHomeBuilderColoradoSprings.com contact form. \n \n From: $name \n Phone: $phone \n Email: $email \n Bonus: $bonus \n";
      $recipient = "[email protected]";
      $subject = "New lead from Challenger Homes!";
      $mailheader = "From: [email protected] \r\n";
      mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
      header("Location: http://www.besthomebuildercoloradosprings.com/thankyou.html");
      $headers  = 'MIME-Version: 1.0' . "\r\n";
      $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

      @mail($email, $email_subject, $replymsg, $headers);


      $replymsg = "
      <html>
      <head>
      <title>Your Challenger Certificate</title>
      </head>
      <body>


      <table style='cell-padding:0px; cell-spacing:0px; border-collapse:collapse; border:0px; border-spacing:0px; padding:0px; margin: 0 auto;'>

      <tr>
      <td  style='border:0px; padding:0px;'>
      <img src='http://www.besthomebuildercoloradosprings.com/images/cert1.jpg'>
      </td>
      <td  style='border:0px; padding:0px;'>
      <img src='http://www.besthomebuildercoloradosprings.com/images/cert2.jpg'>
      </td>
      <td  style='border:0px; padding:0px;'>
      <img src='http://www.besthomebuildercoloradosprings.com/images/cert3.jpg'>
      </td>
      </tr>

      <tr>
      <td  style='border:0px; padding:0px;'>
      <img src='http://www.besthomebuildercoloradosprings.com/images/cert4.jpg'>
      </td>
      <td width='434px' style='background-color:#f4f4f2; border:0px; padding:0px;'>
      <strong>Congratulations <?php echo $name; ?> ! </strong>
      <br><br>
      Thank you for registering with Challenger Homes. You are eligible for incredible and exclusive internet savings. Present this certificate at any Challenger Model Home to get started! Or call today to schedule an appointment: 719-602-5824.
      <br><br>
      We are here to serve you and help make life better for you in a brand-new Challenger Home!
      </td>
      <td  style='border:0px; padding:0px;'>
      <img src='http://www.besthomebuildercoloradosprings.com/images/cert6.jpg' style='display:block;'>
      </td>
      </tr>

      <tr>
      <td  style='border:0px; padding:0px;'>
      <img src='http://www.besthomebuildercoloradosprings.com/images/cert7.jpg' style='display:block;'>
      </td>
      <td  style='border:0px; padding:0px;'>
      <img src='http://www.besthomebuildercoloradosprings.com/images/cert8.jpg' style='display:block;'>
      </td>
      <td  style='border:0px; padding:0px;'>
      <img src='http://www.besthomebuildercoloradosprings.com/images/cert9.jpg' style='display:block;'>
      </td>
      </tr>

      </table>




      </body>
      </html>
      "

      ;
      $email_subject = "Your Special Internet Savings from Challenger Homes";

      mail($email, $email_subject, $replymsg, $headers);

      ?>

Solution

  • In your case <?php echo $name; ?> is using PHP inside PHP. This doesn't make sense:

    <?php
    $name = "test";
    $replymsg = "<?php echo $name; ?>";
    

    Better to use simply {$name}:

    <?php
    $name = "test";
    $replymsg = "Blah blah {$name} and more.";
    

    Or use string concatenation:

    <?php
    $name = "test";
    $replymsg = "Blah blah " . $name . " and more.";