Skip to content Skip to sidebar Skip to footer

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='

Solution 1:

<?php$to  = "some@example.comsome1@exmple.com,some3@example.com";
$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: <me@some.com>' . "\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/orstatic body. "</td>";
$msg .= "<td>" .fetch/ static. "</td>";
$msg .= "<td>" .fetch/static  . "</td>";
$msg .= "</tr>";

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

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

Solution 2:

Please try this

<?phpif($_POST["submit"]) {
    $recipient="am.riparz@gmail.com";
    $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 } ?

>

Solution 3:

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 = "<tablestyle='width:100%'cellpadding='5'><tr><tdstyle='font-weight:bold;'>Name:</td><td>$name_title $sender</td></tr><tr><tdstyle='font-weight:bold;'>Email:</td><td>$senderEmail</td><tr><tdcolspan=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";

Post a Comment for "How To Style A Php Email Form Body?"