Search code examples
phphtmlphpmailer

Setup PHP mailer, but still not working. What did I do wrong?


I am trying to setup php mailer to send me an email when someone fills out my form with the information. Currently its a one page site on the file "index.php"

Here is my php:

  <?php

  include("includes/class.phpmailer.php");

  $mail = new PHPMailer;

  $mail->setFrom($email, $name);
  $mail->addAddress("[email protected]");     // Add a recipient
  $mail->addReplyTo($email, $name);

  $mail->isHTML(true); // Set email format to HTML

  $mail->Subject = 'Message From ' . $name;

  $mail->Body    = "
  <h2>Message Details</h2>              
  <p>Name: {$name} </p>
  <p>Email: {$email} </p>
  <p>Phone: {$phone} </p>
  ";
   ?>

Please note I removed my email for privacy.

Below is my form on the same "index.php" file

    <form id="contact" method="post" action="index.php">
    <ul>
    <h2>Contact</h2>
   <li><input type="text" id="name" name="name" placeholder="Your Name" value="<?php echo $name; ?>"></li>
    <li><input type="email" id="email" name="email" placeholder="Email Address"  value="<?php echo $email; ?>"></li>
    <li><input type="phone" id="phone" name="phone" placeholder="Phone Number" value="<?php echo $phone; ?>"></li>
    <li><textarea name="message" id="message" placeholder="Your Message">    </textarea></li>
    <li><input type="submit" id="submit" name="submit"></li>
    </ul>
    </form>

For the life of me, I cannot figure out why this isnt working, and would appreciate any help. Thank you


Solution

  • In order for this form to work you would need to add

    $mail->send()
    

    There are many form templates available if you simple google, or use these forums plenty of simalar questions about forms etc.

    The simplest way to parse your fields would be, you can read up about $_POST here http://php.net/manual/en/reserved.variables.post.php

    $name = $_POST['name'];
    

    You may find this topic useful to have a read through aswell, will give you more clarity on sending form data via phpmailer.

    PHPmailer: Send from form