0

I've updated this code, using PHPMailer. The code now works and sends through the form to any email address I wish as long as I have the correct host setup in the PHP. This is GREAT. But, it doesn't direct me to my thank you page: header("Location: http://strawberryletting.co.uk/new_form/thank_you.html"); it sends the email fine, along with the correct information, but once the user selects the submit button they stay on the PHP page with a lot of written jargon. Please see updated PHP below:

PHP:

    <?php

error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");  // Look for errors and report errors - if there are any

$name = $_POST["name"];  // Declare variables - inputted from HTML form 
$email = $_POST["email"];
$tel = $_POST["tel"];
$msg = $_POST["msg"];


require("PHPMailer/src/PHPMailer.php"); // Call files required for the phpmailer code from our folder directory
require("PHPMailer/src/Exception.php");
require("PHPMailer/src/SMTP.php");
$mail = new PHPMailer\PHPMailer\PHPMailer(true);
$mail->isSMTP(); // Simple mail transfer protocol
$mail->SMTPDebug = 2; // 1 - 4 use 2 - most useful response
$mail->SMTPAuth = true; // enable the SMTP authentication
$mail->Host = 'smtp.live.com'; // SMTP server and backup server  
$mail->Username = 'billy.farroll@hotmail.com';
$mail->Password = 'PASSWORD';
$mail->SMTPSecure = 'tls'; // enable encryption - accepts TLS also  
$mail->Port = 587; // port to connect - SSL 465 TLS TLS 587 - likely port numbers - 25, 465 or 587
$mail->setFrom('billy.farroll@hotmail.com', 'Billy Farroll'); // This is where the email will come from 
$mail->addAddress('billy@strawberrymarketing.com','Billy Farroll'); // This is where the email will be sent to
$mail->addReplyTo("$email","$name"); 
$mail->isHTML(true);
$mail->Subject = 'TEST1';
$mail->Body = "$msg"; 
if (!$mail->send()){ // Send the message 

    echo 'Message Failed';


    } else {


        header("Location: http://strawberryletting.co.uk/new_form/thank_you.html"); // Take me to Thank you page 

    }

?>

HTML:

<div class="container">
  <div class="row">
    <div class="col-lg-12">

<section id="contact">
    <form action="enquiry_new.php" method="post">
          <div class="field name-box">
                <input type="text" id="name" name="name" placeholder="Who Are You?" tabindex="1" required>
                <label for="name">Name</label>
                <span>Done</span>
          </div>

          <div class="field email-box">
                <input type="text" id="email" name="email" placeholder="name@email.com" tabindex="2" required>
                <label for="email">Email</label>
                <span>Done</span>
          </div>

      <div class="field tel-box">
                <input type="text" id="tel" name="tel" placeholder="Telephone Number" tabindex="3" required>
                <label for="tel">Mobile</label>
                <span>Done</span>
          </div>

          <div class="field msg-box">
                <textarea id="msg" name="msg" rows="4" placeholder="What's Up?" tabindex="4" required></textarea>
                <label for="msg">Message</label>
                <span>Done</span>
          </div>

          <input class="button" type="submit" value="Send" />
  </form>
</section>
    </div>
  </div>
</div>
billy.farroll
  • 1,903
  • 2
  • 15
  • 23
  • Use one of the tried and tested mail libraries like PHPMailer, SwiftMailer or similar instead of using the low level `mail()`-function. That will not just give you a way more verbose API, it will also be a breeze to send the email through your email providers SMTP server instead. The script will also be more portable that way (since it won't depend on your web servers configuration) – M. Eriksson May 29 '18 at 12:59
  • Magnus Eriksson Have updated with the PHPMailer library, would you possibly be able to see my new PHP and see the problem? Would greatly appreciate it. – billy.farroll May 30 '18 at 14:05
  • Have you gone through all the points in the duplicate question? – M. Eriksson May 30 '18 at 14:09
  • One thing I can say is that your code should spit out a bunch of "undefined index"-notices. Your using `$_POST['$email']`, `$_POST['$name']` etc when it should be `$_POST['email']`, `$_POST['name']` and so on (without the `$` in front of the keys). You've even used the correct names in the beginning of the file and stored the data in variables, so I'm not sure why you're using the `$_POST`-variable at all later in the script. – M. Eriksson May 30 '18 at 14:13
  • Yes I have been through majority of the other points in the other question. So you would have the $mail->addReplyTo($_POST['$email'], $_POST['$name']); and $mail->Body = $_POST['$msg']; like so: $mail->addReplyTo($_POST['email'], $_POST['name']); and $mail->Body = $_POST['msg']; ? – billy.farroll May 30 '18 at 14:17
  • Or just remove the $_POST variable completely? – billy.farroll May 30 '18 at 14:18
  • Just use the variables you've defined in the beginning instead – M. Eriksson May 30 '18 at 14:20
  • Thanks, will do a couple more tests. – billy.farroll May 30 '18 at 14:24
  • Sorry to keep pestering you Magnus. I've updated the code to the latest PHPMailer and it works. But, doesn't direct me to the thank you page. – billy.farroll May 31 '18 at 07:53
  • FIXED IT FINALLY. – billy.farroll May 31 '18 at 08:00

0 Answers0