1

I have a php code in my website to send a mail for contact Us section. I want to show user's input in tabular format in mail body.

php code

<?php
try{
        $name = $_POST['name'];
        $phone = $_POST['phone'];
        $email = $_POST['email'];
        $comments = $_POST['comments'];
        $to = "[email protected]";
        $response = mail($to, 'Query from website',
        "<table>
        <tr>
          <th>Name</th>
          <th>Contact Number</th> 
          <th>Email-Id</th>
          <th>Comments</th>
        </tr>
        <tr>
          <td>'.$name'</td>
          <td>'.$phone'</td>
          <td>'.$email'</td>
          <td>.'$comments'</td>
        </tr>
      </table>
      ");
      print_r($response); 
    } catch(Exception $e){
        print_r($e);
    }
?>

The issue is whole html code appears in mail body.

3 Answers 3

2

Refer below example as described in the PHP manual page.

<?php
// Multiple recipients
$to = '[email protected], [email protected]'; // note the comma

// Subject
$subject = 'Birthday Reminders for August';

// Message
$message = '
<html>
<head>
  <title>Birthday Reminders for August</title>
</head>
<body>
  <p>Here are the birthdays upcoming in August!</p>
  <table>
    <tr>
      <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
    </tr>
    <tr>
      <td>Johny</td><td>10th</td><td>August</td><td>1970</td>
    </tr>
    <tr>
      <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
    </tr>
  </table>
</body>
</html>
';

// To send HTML mail, the Content-type header must be set
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';

// Additional headers
$headers[] = 'To: Mary <[email protected]>, Kelly <[email protected]>';
$headers[] = 'From: Birthday Reminder <[email protected]>';
$headers[] = 'Cc: [email protected]';
$headers[] = 'Bcc: [email protected]';

// Mail it
mail($to, $subject, $message, implode("\r\n", $headers));
?>
Sign up to request clarification or add additional context in comments.

Comments

1

I have altered your code a bit. Your variable names didn't get the values as they were inside qoutation marks(') and also they weren't preceded and succeeded by (.). You didn't use content-type header to use these HTML tags to get rendered properly. Please refer the below code. Also, there must be an implode function to return these headers as they are stored in array.

<?php
try{
        $name = $_POST['name'];
        $phone = $_POST['phone'];
        $email = $_POST['email'];
        $comments = $_POST['comments'];
        $to = "[email protected]";
        $headers[] = 'MIME-Version: 1.0';
        $headers[] = 'Content-type: text/html; charset=iso-8859-1';
        $message = '<html><body>';
        $message .= '<table border="1">
        <tr>
          <th>Name</th>
          <th>Contact Number</th> 
          <th>Email-Id</th>
          <th>Comments</th>
        </tr>
        <tr>
          <td>'.$name.'</td>
          <td>'.$phone.'</td>
          <td>'.$email.'</td>
          <td>'.$comments.'</td>
        </tr>
      </table>';
        $response = mail($to, 'Query from website', $message,implode("\r\n",$headers));
      print_r($response); 
    } catch(Exception $e){
        print_r($e);
    }
?>

Comments

1

You'll have to tell mail what the content-type of your message is, by providing it with headers. The mail function's signature is:

mail($to, $subject, $message, $headers);

Create a content-type header and pass it to mail:

$headers = "Content-Type: text/html; charset=UTF-8";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.