0

so i want to send an email after submitting a HTML form, using PhpMailer. But when i click submit, my page simply refresh and there is no email in my inbox. What am i missing? also, what value should i assign to the action attribute?

This is my form

            <form action="" method="POST" enctype="text/plain" id="contact-form">
              <span class="contacts-text" id="first-contacts-text">compile this form.</span>
              <span class="contacts-text">I will reply as soon as possible. Thank you!</span>
              <ul class="form-content">
                  <li class="form-input">
                      <label for="name" class="label">Full name</label>
                      <input class="input" id="name" type="text" name="name" required>
                  </li>
                  <li class="form-input">
                      <label for="email" class="label">E-mail</label>
                      <input class="input" id="mail" type="text" name="email" required>
                  </li>
                  <li class="form-input">
                      <label for="msg" class="label">Insert text</label>
                      <textarea class="input" id="comment" type="text" name="msg" cols="30" rows="10" style="resize: none;" required></textarea>
                  </li>
                  <li class="form-input" id="last-input">
                      <input class="input" id="form-button" type="submit" value="submit" name="submit">
                  </li>
              </ul>
            </form>

and this is my php code


  $result="";

  if(isset($_POST['submit'])){
    require 'phpMailer/PHPMailerAutoload.php';
    $mail = new PHPMailer;

    $mail->Host='smtp.gmail.com';
    $mail->Port=587;
    $mail->SMTPAuth=true;
    $mail->SMTPSecure='tls';
    $mail->Username='my.own.business@gmail.com';
    $mail->Password='myPassword';

    $mail->setFrom($_POST['email'],$_POST['name']);
    $mail->addAddress('my.own.business@gmail.com');
    $mail->addReplyTo($_POST['email'],$_POST['name']);

    $mail->isHTML(true);
    $mail->Subject='Form Submission: '.$_POST['subject'];
    $mail->Body='<h2 align=center> :'.$_POST['name'].'<br>Email: '.$_POST['email'].'<br>Message: '.$_POST['msg'].'</h2>';

    if(!$mail->send()){
      $result="Something went wrong. Please try again.";
    }
    else {
      $result="Thanks ".$_POST['name']." for contacting me. I will get you back soon!";
    }
  }

?>
Elle
  • 43
  • 6

1 Answers1

1

What am i missing?

You set the enctype attribute to text/plain which is not machine processable. Don't do that.

also, what value should i assign to the action attribute?

The URL of the PHP program.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • can i get rid of the "enctype" attribute or do i have assign a new value? – Elle Apr 10 '21 at 14:40
  • Get rid of it. The default value is fine. – Quentin Apr 10 '21 at 14:41
  • Thanks a lot. After getting rid of it, when i submit the form i get this error ----> Fatal error: __autoload() is no longer supported, use spl_autoload_register() instead in C:\xampp\htdocs\phpProjects\Portfolio-elle\phpMailer\PHPMailerAutoload.php on line 45. – Elle Apr 10 '21 at 14:45
  • I've never come across that problem before… but if I copy/paste the error into Google it finds https://stackoverflow.com/questions/65346342/autoload-is-no-longer-supported-use-spl-autoload-register-instead-in-c-x in seconds. – Quentin Apr 10 '21 at 14:47
  • Yep, i'm reading the convo right now. Thanks a lot for your time, have a nice one – Elle Apr 10 '21 at 14:48