Home > Mobile >  Send html table in email body in PHP
Send html table in email body in PHP

Time:05-05

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

<?php
$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!";
        }
        else{
            
        $info = '<html>
                    <head>
                        <title>Birthday Reminders for August</title>
                    </head>
                    <body>
                        <table  border="1" cellspacing="3" width="60%">
                            <tr>
                                <td>Name:</td>
                                <td>$name</td>
                            </tr>
                            <tr>
                                <td>Email:</td>
                                <td>$email</td>
                            </tr>
                            <tr>
                                <td>Address:</td>
                                <td>$address</td>
                            </tr>
                            <tr>
                                <td>Phone:</td>
                                <td>$phone</td>
                            </tr>
                            <tr>
                                <td>Subject:</td>
                                <td>$subject</td>
                            </tr>
                            <tr>
                                <td>Services:</td>
                                <td>$service</td>
                            </tr>
                            <tr>
                                <td>Message:</td>
                                <td>$message</td>
                            </tr>
                        </table>
                    </body>
                </html>';
            
            
            $headers  = 'MIME-Version: 1.0' . "\r\n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
            $mailto = "[email protected]";
            $sub = "Get In Touch With Us";
            mail($mailto,$sub,$info,$headers);
            $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;
}
?>

 

CodePudding user response:

You have all of your variables just written out in the string. You're going to need to concatenate them.

instead of <td>$message</td>

it should be <td>.$message.</td>

CodePudding user response:

Just change the ' to " at the start and end of $info variable string declaration (you then need to escape every " character in your string or change it to ') OR change your variables to, for example, '. $name .'.

In single quotes variables are not interpreted as they are in double quoted strings.

Read answer to this question about the difference between single and double quotes in PHP strings to get what you are doing wrong:

https://stackoverflow.com/a/3446286/15182618

  •  Tags:  
  • php
  • Related