I am trying to send the data from the form input fields on submit to an email address, while at the same time the form data is inserted into a database table. I have successfully managed to get the form data to submit into the database, I am now struggling to get the form data to be sent as an email. Here is my following PHP code. All the PHP code is in one file. The email address I am trying to get the data sent to, is not a gmail, hotmail etc. email, it is a company's own mailing address.
<?php
$conn = mysqli_connect("", "", "", ""); --> deleted this because of this post
if (!$conn) {
die ("Connection failed: ". mysqli_connect_error());
}
$course = $_POST ['course'];
$firstname = $_POST ['firstname'];
$surname = $_POST ['surname'];
$DOB = $_POST ['DOB'];
$gender = $_POST ['gender'];
$address1 = $_POST ['address1'];
$address2 = $_POST ['address2'];
$city = $_POST ['city'];
$postcode = $_POST ['postcode'];
$mobile = $_POST ['mobile'];
$home = $_POST ['home'];
$email = $_POST ['email'];
$residency = $_POST ['residency'];
$learning = $_POST ['learning'];
$qualifications = $_POST ['qualifications'];
$sql = "INSERT INTO form (course, firstname, surname, DOB, gender, address1, address2, city, postcode, mobile, home, email, residency, learning, qualifications) VALUES ('$course', '$firstname', '$surname', '$DOB', '$gender', '$address1', '$address2', '$city', '$postcode', '$mobile', '$home', '$email', '$residency', '$learning', '$qualifications')";
$result = mysqli_query($conn, $sql);
The above is the code to get the form data into a database. The code below is my attempt to send the information to an email address.
if(isset($_POST["submit"])){
// Checking For Blank Fields..
if(
$_POST["course"]==""|| $_POST["firstname"]==""||
$_POST["surname"]==""||
$_POST["DOB"]==""||
$_POST["gender"]==""||
$_POST["address1"]==""||
$_POST["address2"]==""||
$_POST["city"]==""||
$_POST["postcode"]==""||
$_POST["mobile"]==""||
$_POST["home"]==""||
$_POST["email"]==""||
$_POST["residency"]==""||
$_POST["learning"]==""||
$_POST["qualifications"]=="")
{
echo "Please ensure all fields are filled in.";
} else {
// Check if the "Sender's Email" input field is filled out
$email=$_POST['email'];
// Sanitize E-mail Address
$email =filter_var($email, FILTER_SANITIZE_EMAIL);
// Validate E-mail Address
$email= filter_var($email, FILTER_VALIDATE_EMAIL);
if (!$email){
echo "Invalid Email";
}
else{
$course = $_POST['course'];
$firstname = $_POST['firstname'];
$surname = $_POST['surname'];
$DOB = $_POST['DOB'];
$gender = $_POST['gender'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$postcode = $_POST['postcode'];
$mobile = $_POST['mobile'];
$home = $_POST['home'];
$email = $_POST['email'];
$residency = $_POST['residency'];
$learning = $_POST['learning'];
$qualifications = $_POST['qualifications'];
$headers = 'From:'. $email . "\r\n"; // Sender's Email
$headers .= 'Cc:'. $email . "\r\n"; // Carbon copy to Sender
// Send Mail By PHP Mail Function
mail("example@example.com", $course, $firstname, $surname, $DOB, $gender, $address1, $address2, $city, $postcode, $mobile, $home, $email, $residency, $learning, $qualifications);
echo "Your mail has been sent successfuly! We will get back to as soon as we can!";
}
}
}
header("Location: apply.php");
The following is just the first portion of the tags in my hmtl:
<form class="form" id="contact_form" action="submitApplication.php" method="post">
The submit button:
<button type="submit" name="submit" class="btn btn-primary btn-lg outline hvr-grow">Submit Application</button>
Something to add, when I try to submit the application with all of the code above in one file, on submission no data is sent to the database with the code for the email in the same file. If I remove the code for the email in that same file, the application form is submitted to the database. I have to be as concise as I can and hope someone can help me! Thank you!