user10361589
user10361589

Reputation:

PHP Send Email From HTML Form

I want to get an email from an html form. I was following a tutorial that used PHP, which I have never used before, and it doesn't seem to work. After I hit submit, the php page opens, but an email is not sent to me. Can someone please help me.

HTML(A couple inputs like name and email, etc. and a submit button at the bottom):

<form action="contact_form_process.php" method="POST">
    <section id="contact" class="py-3">
      <div class="container">
        <div class="row">
          <div class="col-md-9 mx-auto">
            <div class="card p-4">
              <div class="card-body">
                <h3 class="text-center lato-font">Please Fill Out This Form To Contact Us</h3>
                <hr>
                <div class="row">
                  <div class="col-md-6">
                    <div class="form-group">
                      <input id="first_name" type="text" class="form-control" placeholder="First Name">
                    </div>
                  </div>
                  <div class="col-md-6">
                    <div class="form-group">
                      <input id="last_name" type="text" class="form-control" placeholder="Last Name">
                    </div>
                  </div>
                  <div class="col-md-6">
                    <div class="form-group">
                      <input id="email" type="text" class="form-control" placeholder="Email">
                    </div>
                  </div>
                  <div class="col-md-6">
                    <div class="form-group">
                      <input id="phone_num" type="text" class="form-control" placeholder="Phone Number">
                    </div>
                  </div>
                  <div class="col-md-12">
                    <div class="form-group">
                      <textarea class="form-control" name="" id="message" rows="10" placeholder="Message"></textarea>
                    </div>
                  </div>
                </div>
              </div>
              <div class="card-footer">
                <input type="submit" class="btn btn-outline-success btn-block" value="Send">
                <button type="submit" class="btn btn-outline-success btn-block">Send</button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>
  </form>

Here is my PHP:

<?php

  // Subject and Email Variables
  $emailSubject = 'Test Email';
  $webMaster = '[email protected]';

  // Gathering Data Variables
  $first_nameField = $_POST['first_name'];
  $last_nameField = $_POST['last_name'];
  $emailField = $_POST['email'];
  $phone_numField = $_POST['phone_num'];
  $messageField = $_POST['message'];

  $body = <<<EOD
<br><hr><br>
Email: $emailField <br>
Name: $first_nameField <br>
Phone Number: $phone_numField <br>
Message: $message <br>
EOD;

  $headers = "From: $emailField\r\n";
  $headers .= "Content-type: text/html\r\n";
  $success = mail($webMaster, $emailSubject, $body, $headers);



?>

Thank you!

Upvotes: 0

Views: 151

Answers (2)

i0N77
i0N77

Reputation: 43

 $webMaster = '[email protected]';

I assume this is the address where the emails are sent to. From what I've heard, PHP mail() function doesn't work for gmail(if it's on the receiver end).

You can just use another email provider like hotmail or any and forward emails from it to your gmail

Watch this video if you need more info on it, How to create a PHP Contact Form Tutorial

Upvotes: 0

Arjun Venkatraman
Arjun Venkatraman

Reputation: 239

mail() needs to be enabled as @esqew points out in his comment - this would look something like this (this is on my BigRock environment YMMV) -

ini_set("include_path", '<path to php>' . ini_get("include_path") );
require_once "Mail.php";

Also, if you are using GMail, you also need to set up GMail to allow sending mail through SMTP - https://support.google.com/mail/answer/7126229?visit_id=636741342414654323-2069917091&hl=en&rd=1

Upvotes: 1

Related Questions