0

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!

Suly
  • 1
  • 4
  • That suggests that you have a syntax error in the email code. Turn on PHP error reporting and see what shows up there. – Sloan Thrasher Apr 10 '17 at 16:41
  • Also, your code is susceptible to SQL Injection. Please use prepared statements with mysqli – Sloan Thrasher Apr 10 '17 at 16:42
  • [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)*** Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Apr 10 '17 at 16:45
  • Have you checked your error logs? – Jay Blanchard Apr 10 '17 at 16:46
  • That isn't how `mail()` works http://php.net/manual/en/function.mail.php Assign a variable for all the variables to be sent and set it as the "body" argument and use the headers from the manual link I gave you. – Funk Forty Niner Apr 10 '17 at 16:53

1 Answers1

-1

As per this line:

mail("example@example.com", $course, $firstname, $surname, $DOB, $gender, $address1, $address2, $city, $postcode, $mobile, $home, $email, $residency, $learning, $qualifications);

That isn't how mail() works http://php.net/manual/en/function.mail.php

The syntax is:

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

  • You are using too many arguments for mail(), there should only be four.

Assign a variable for all the variables to be sent and set it as the "body" argument and use the headers from the manual.

I.e.:

$to = "example@example.com";
$subject = "Mail form submitted";

$headers = 'From:'. $email . "\r\n"; // Sender's Email
$headers .= 'Cc:'. $email . "\r\n"; // Carbon copy to Sender

$body = "

$course \n $firstname \n $surname \n $DOB \n $gender \n 
$address1 \n $address2 \n   $city \n $postcode \n $mobile \n 
$home \n $email \n $residency \n $learning \n $qualifications

";

if(mail($to, $subject, $body, $headers)){

    echo "Mail sent";
}
else {
    echo "Error, check your logs";
    // header("Location: apply.php"); exit;
    // use echo OR header, not both and uncomment/comment out the one you want.
}

Your code is also open to an SQL injection, use a prepared statement:

Also make sure that all POST arrays are correct and hold value.

Error reporting will be of help, should there be any errors in your PHP:

You may be outputting before header also.

Either you remove the echo and replace it with the header, or vice-versa; you can't use both.

If you are still having problems sending mail, consult the following Q&A:

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141