Riffaz Starr
Riffaz Starr

Reputation: 611

How to style a php email form body?

I am trying to create a php form. When I get the submitted form on email I should get it visually nice look.

If put the following code I get this ![email body][1]

$mailBody=" <html>
                <body>
                    <table border=\"1\" style=\"width:100%\">
                        Name: $name_title $sender\n
                        Email: $senderEmail\n\n
                        $message
                    </table>        
                </body>
            </html>";

How can I get a nice scc table or div like a html document when I get my php form?

Upvotes: 0

Views: 4474

Answers (3)

Roshan Dandgavhal
Roshan Dandgavhal

Reputation: 559

Please try this

<?php

    if($_POST["submit"]) {
    $recipient="[email protected]";
    $subject="Arugambay Bookings : New reservations request";
    $sender=$_POST["sendername"];
    $senderEmail=$_POST["senderEmail"];
    $message=$_POST["message"];
    $name_title=$_POST["name_title"];   

    $mailBody = "<table style='width:100%' cellpadding='5'>";
    $mailBody .= "<tr>";
    $mailBody .= "<th>Item</th>";
    $mailBody .= "<th>Description</th>";
    $mailBody .= "</tr>";
    $mailBody .= "<tr>";
    $mailBody .= "<td>Name</td>";
    $mailBody .= "<td>$name_title $sender</td>";
    $mailBody .= "</tr>";
    $mailBody .= "<tr>";
    $mailBody .= "<td>Email</td>";
    $mailBody .= "<td>$senderEmail</td>";
    $mailBody .= "</tr>";
    $mailBody .= "<tr>";
    $mailBody .= "<td>Special req</td>";
    $mailBody .= "<td>$message</td>";
    $mailBody .= "</tr>";
    $mailBody .= "</table>";

    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $mail_sent = mail($recipient, $subject, $mailBody, "From: $sender <$senderEmail>");
    }

    if ($mail_sent) {
?>


    <p>Mail sent</p>
 <?php } ?

>

Upvotes: 1

black
black

Reputation: 745

<?php

$to  = "[email protected]@exmple.com,[email protected]";
$subject = 'Hello World';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// More headers
$headers .= 'From: <[email protected]>' . "\r\n";

$msg =   '<html>';
$msg .= '<head>
   <h2 style="font-family:verdana;text-align:center;background-color:red">Hello World</h2>
   </head>
  <table border="3" cellspacing="2">
  <tr style="font-family:verdana;background-color:#6d6d6d">
       <th>col1</th>
       <th>col2</th>
       <th>Col3</th>
       <th>Col4</th>
   </tr>';
//do something here fetch or send static body

$msg .= "<tr>";
$msg .="<td>" . fetch from db/or static body. "</td>";
$msg .= "<td>" .fetch/ static. "</td>";
$msg .= "<td>" .fetch/static  . "</td>";
$msg .= "</tr>";

$msg .= '</table>';      
$msg .= '</html>'; 

mail($to, $subject, $msg, $headers); 
?>

Upvotes: 2

Lee
Lee

Reputation: 4323

You don't need to use <html> or <body> when styling the output of your email form.

All you need is to start from the table element, and use some inline styling. This code below will just give a simple table, with the Name and Email labels in bold, with a small padding around each cell.

$mailBody = "<table style='width:100%' cellpadding='5'>
                 <tr>
                     <td style='font-weight:bold;'>Name:</td><td>$name_title $sender</td>
                 </tr>
                 <tr>
                     <td style='font-weight:bold;'>Email:</td><td>$senderEmail</td>
                <tr>
                     <td colspan=2>$message</td>
                </tr>
            </table>";

You also need to make sure the correct MIME settings are set for the headers of the mail, or else no HTML formatting will be shown, but only outputted.

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

Upvotes: -2

Related Questions