Send html table in email body in PHP

im trying to send mail in php,so that mail receiver receives mail with html table, im receiving mail with table but data is missing in mail… im recieving variable instead of data from html form.

Name: $name

Email: $email

Phone: $phone

Services: $services

Address: $address

Subject: $subject

Message: $message

The code i write is below

$name = $email = $address = $phone = $service = $subject =  $message ="";
$contactErr = "";
$contactsuccess = "";

    if (isset($_POST['submit'])) {
        $fm_name = $_POST['name'];
        $fm_email = $_POST['email'];
        $fm_address = $_POST['address'];
        $fm_phone = $_POST['phone'];
        $fm_service = $_POST['service'];
        $fm_subject = $_POST['subject'];
        $fm_message = $_POST['message'];

        $name = contact_input($fm_name);
        $email = contact_input($fm_email);
        $address = contact_input($fm_address);
        $phone = contact_input($fm_phone);
        $service = contact_input($fm_service);
        $subject = contact_input($fm_subject);
        $message = contact_input($fm_message);

        if (empty($fm_name)) {
            $contactErr = "Name is Required.";
        elseif (empty($fm_email)) {
            $contactErr = "Email. is Required.";
        elseif (!filter_var($fm_email, FILTER_VALIDATE_EMAIL)) {
            $contactErr = "Invalid email format";
        elseif (empty($fm_address)) {
            $contactErr = "Enter you address.";
        elseif (empty($fm_phone)) {
            $contactErr = "Phone No. is Required.";
        elseif (empty($fm_service)) {
            $contactErr = "Select your desired service !";
        elseif (empty($fm_subject)) {
            $contactErr = "Subject is Required.";
        elseif (empty($fm_message)) {
            $contactErr = "Enter your message!";
        $info = '<html>
                        <title>Birthday Reminders for August</title>
                        <table  border="1" cellspacing="3" width="60%">
            $headers  = 'MIME-Version: 1.0' . "\r\n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
            $mailto = "";
            $sub = "Get In Touch With Us";
            $contactsuccess = "Your message has been sent successfully! We will contact you shortly.";
            $name = $email = $address = $phone = $service = $subject =  $message ="";

function contact_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;

Allow me to refer you to the free manual on PHP Strings and single and double quotes where you will find your answer.

And stop creating variables for nothing.

You are also overwriting your errors which you don’t even do anything with.

And stop using mail(). Use PHPMailer instead.

This might be simpler than you think…
use $info = " … "; (double quotes instead of single quotes) or better yet use a HEREDOC…

hope that helps

This whole block is needless, you can apply the contact_input() function directly to the $_POST values:-

$name = contact_input($_POST['name']);
// Etc...

You could shorten things further by adding the empty() check to the function, after the trim():-

function contact_input($data) {
    $data = trim($data);
    if(empty($data)){ return false ;}
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;

Now you will check the return value of the function:-

if(!$name = contact_input($_POST['name'])){
     $errorArray[] = "Name is Required.";

Notice I’m using an array to collect the errors throughout the validation, not a single value variable that may only hold one error.
At the end of validation you can use count() to check if there were any errors.

if(!count($errorArray)){   // No errors, all is good
     // Send the mail
else{   // There were errors
    // Display the form again, listing the errors made

Be sure it initialise the $errorArray before validation, or it may not be set for the above test:-

if($_SERVER['REQUEST_METHOD'] === 'POST'){ // Use this test to check for form submission, not isset() some random field
    $errorArray = [] ; // Initialise the error array
    // Do validation now...
    if(!$name = contact_input($_POST['name'])){ // Etc...
1 Like

that was helpful… i just forgot to concatenate the variable