Jump to content

Create Php inquiry form with file upload and send to email

Recommended Posts

I tried to create the inquiry form fronted on html and backhand on php.

the purpose of the form use can fill their personal details and attach their image after submit the all data with picture will be email to admin.


i wrote this code but fail to execute successfully.


$phone = "test number"; // check line 161
$_POST['title'] =""; // somehow its turning into an array

if(!$_POST) exit;

// Email address verification, do not edit.
function isEmail($email) {

if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
//$file_name = $_FILES['file_name'];
$file_name =$_FILES["file_name"]["tmp_name"];
$file_data = base64_encode( file_get_contents($file_name));

$title = $_POST['title'];
$gender = $_POST['gender'];
$maritalStatus = $_POST['maritalStatus'];
$name = $_POST['name'];
$fatherName = $_POST['fatherName'];
$dob = $_POST['dob'];
$email = $_POST['email'];
$postalAddress = $_POST['postalAddress'];
$PTCLno = $_POST['PTCLno'];
$mobileNumber1 = $_POST['mobileNumber1'];

$matricBoard = $_POST['matricBoard'];
$intermediateBoard = $_POST['intermediateBoard'];
$group = $_POST['group'];
$totalMarksMatric = $_POST['totalMarksMatric'];
$marksObtnMatric = $_POST['marksObtnMatric'];
$examTypeMatric = $_POST['examTypeMatric'];
$passingYear = $_POST['passingYear'];
$percentageMatric = $_POST['percentageMatric'];

$nameOfCollege = $_POST['nameOfCollege'];
$collegeEmailAddress = $_POST['collegeEmailAddress'];
$postelAddressOfCollege = $_POST['postelAddressOfCollege'];
$telephoneNumberOfCollege = $_POST['telephoneNumberOfCollege'];
$collegePrincipleName = $_POST['collegePrincipleName'];
$collegePrincipleCellNo = $_POST['collegePrincipleCellNo'];

$referenceName = $_POST['referenceName'];
$reference1InstituteName = $_POST['reference1InstituteName'];
$collegePrincipleCellNo = $_POST['reference1InstituteName'];
$reference1CellNo = $_POST['reference1CellNo'];
$reference1Address = $_POST['reference1Address'];

$reference2InstituteName = $_POST['reference2InstituteName'];
$reference2CellNo = $_POST['reference2CellNo'];
$reference2Address = $_POST['reference2Address'];

$question1 = $_POST['question1'];
$question2 = $_POST['question2'];
$question3 = $_POST['question3'];
$question4 = $_POST['question4'];
$question5 = $_POST['question5'];

$accountTitle = $_POST['accountTitle'];
$accountNo = $_POST['accountNo'];
$nameOfBank = $_POST['nameOfBank'];
$branchAddress = $_POST['branchAddress'];
$CNICofAccHolder = $_POST['CNICofAccHolder'];
$cellNoOfAccHolder = $_POST['cellNoOfAccHolder'];

$examCity = $_POST['examCity'];
$terms = $_POST['terms'];

if(trim($name) == '') {
echo '<div class="alert alert-error">You must enter your name.</div>';
} else if(trim($email) == '') {
echo '<div class="alert alert-error">You must enter email address.</div>';
} else if(!isEmail($email)) {
echo '<div class="alert alert-error">You must enter a valid email address.</div>';

if(get_magic_quotes_gpc()) {
$comments = stripslashes($comments);

// Configuration option.
// Enter the email address that you want to emails to be sent to.
// Example $address = "joe.doe@yourdomain.com";

//$address = "hashir_252@hotmail.com";
$address = "hashir_252@hotmail.com";

// Configuration option.
// i.e. The standard subject will appear as, "You've been contacted by John Doe."

// Example, $e_subject = '$name . ' has contacted you via Your Website.';
$e_subject = 'Contact Form';

// Configuration option.
// You can change this if you feel that you need to.
// Developers, you may wish to add more fields to the form, in which case you must be sure to add them here.

$e_body = "You have been contacted by $name, their additional message is as follows." . PHP_EOL . PHP_EOL;
$e_content = "\"
Picture: "
.'<img src="data:image/png;base64,'.$file_data.'" alt="beastie.png">'." \n
Personal Information \n
Title: $title \n
Gender: $gender \n
Martial Status: $maritalStatus \n
Name: $name \n
Father Name: $fatherName \n
Date of Birth: $dob \n
Email ID: $email \n
Postal Address: $postalAddress \n
Ptcl: $PTCLno \n
Mobile Number: $mobileNumber1 \n
Academic Information \n
Matric Board: $matricBoard \n
Intermediate Board: $intermediateBoard \n
Group: $group \n
Total Marks Matric: $totalMarksMatric \n
Marks Obtain: $marksObtnMatric \n
Exam Type: $examTypeMatric \n
Passing Year: $passingYear \n
Percentage Matric: $percentageMatric \n
College Information \n
Name of College: $nameOfCollege \n
College Email Address: $collegeEmailAddress \n
Postal Address College: $postelAddressOfCollege \n
Telephone Number: $telephoneNumberOfCollege \n
Principle Name: $collegePrincipleName \n
Principle Cell Number: $collegePrincipleCellNo \n
References \n
Refrence Name: $referenceName \n
Refrence Institute Name: $reference1InstituteName \n
Refrence Cell Number: $reference1CellNo \n
Refrence Address: $reference1Address \n
Refrence 2 Name: $reference2InstituteName \n
Refrence Cell Number: $reference2CellNo \n
Refrence Address: $reference2Address \n
Questions and Answers \n
Question1: $question1 \n
Question2: $question2 \n
Question3: $question3 \n
Question4: $question4 \n
Question5: $question5 \n
Banking Information \n
Account Title: $accountTitle \n
Account Number: $accountNo \n
Bank Name: $nameOfBank \n
Branch Address: $branchAddress \n
Cnic#: $CNICofAccHolder \n
Cell Number: $cellNoOfAccHolder \n
Select the city where you would like to take the exam \n
Exam City: $examCity \n
Terms: $terms\""

$e_reply = "You can contact $name via email: $email or via phone: $phone";
$msg = wordwrap( $e_body . $e_content . $e_reply, 70 );
$headers = "From: $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;

$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
// //////////////////////////////////////////////////// REMOVE THESE LINES
echo $msg;
///////////////////////////////////////////////////// REMOVE THESE LINES
if(mail($address, $e_subject, $msg, $headers)) {

// Email has sent successfully, echo a success page.

echo "<div class='alert alert-success'>";
echo "<h3>Email Sent Successfully.</h3><br>";
echo "<p>Thank you <strong>$name</strong>, your message has been submitted to us.</p>";
echo "</div>";

} else {

echo 'ERROR!';


Link to post
Share on other sites

i'd suggest displaying errors


you should be using this code and put it at the top of you php while developing

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

what errors are you getting?


ps. please use the code tags so your code is more visible

Link to post
Share on other sites

if you are just learning, start with one or two form fields and get the code to work so that you are learning and understanding how the code works.


if you are doing this for real and you have more than about three form fields, you should NOT write out lines of code for each possible form field. you would instead dynamically process the form data, by defining the expected fields in a data structure (array or database table.) you would then loop over the defining data structure and dynamically reference the data, to first validate it, then dynamically produce the email message body, all without writing out line after line of code that only differs in the form field it corresponds to.


next, any data you put into the email message body needs to be passed through htmlentities() so that any html/javascript/css in the submitted data won't be operated on by the receiving email client. any data you put into the email header must be validated that it is exactly and only an expected value.


lastly, the email is not being sent FROM the email address that someone entered in the form. the email is being sent from the mail server at your web host. the From: email address should be a valid email address at your web host, so that the From: email address corresponds to the sending mail server and so that bounce messages from the receiving mail server have a place to go to.

Link to post
Share on other sites
This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.