0

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?

Riffaz Starr
  • 611
  • 2
  • 20
  • 37
  • You no need to add or tags in the message body just write normal html code with inline css. – Srinivasa Reddy Sep 02 '14 at 07:31
  • you can just add inline css – black Sep 02 '14 at 07:32
  • 1
    Keep in mind not everyone will see the HTML layout. People can decide to use plain text for all mail. – RST Sep 02 '14 at 07:33
  • can u send the mail without adding css ? – black Sep 02 '14 at 07:35
  • yeah.. actually this for my email. I want to get the details of the form in a nice look. so can read it clearly and arrange the things according the HTML mail I get. If just add the CSS only then how can I make it in 2 row. eg: I need to get the name label in left side and it's input in right side. – Riffaz Starr Sep 02 '14 at 07:38
  • possible duplicate of [Sending HTML email from PHP](http://stackoverflow.com/questions/3058897/sending-html-email-from-php) – Archlight Sep 02 '14 at 07:48
  • A lot of people use a library. Examples: http://phpmailer.worxware.com and http://swiftmailer.org – KIKO Software Sep 02 '14 at 08:00
  • Give the full error message with line number and then indictae that line number of your script! **Nobody here is clairvoyant you know.** – RiggsFolly Sep 02 '14 at 10:11
  • Now it's fine. I missed some semicolons there. Now the problem is **adding MIME VERSION** header. If I use `$mail_sent = mail($recipient, $subject, $mailBody, "From: $sender <$senderEmail>");` then it's fine. It sends mail to the email. If I use `$mail_sent = mail($recipient, $subject, $mailBody, "From: $sender <$senderEmail>", $headers);` then it's not sending mail. May be the reason is I am using this on a wordpress site side bar. Someone who edited my question deleted the word wordpress. Any suggestions? – Riffaz Starr Sep 02 '14 at 10:19

3 Answers3

2
<?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/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); 
?>
black
  • 729
  • 5
  • 13
  • 28
  • Please, at least add the headers to the mail() function. And all those `$msg .= ` look really ugly... but would function. – KIKO Software Sep 02 '14 at 07:54
  • thanks for that but it says syntax error. unexpected T-variable. could you please check my edited answer? @black – Riffaz Starr Sep 02 '14 at 07:58
  • I've found. I missed some semicolons ; Any how my result is http://tinypic.com/r/6nqqo3/8 – Riffaz Starr Sep 02 '14 at 08:05
  • sorry 4 the late reply @RiffazStarr i have edited my answer.try it out! cheers – black Sep 02 '14 at 09:21
  • Thanks for that. But if I remove `$headers` from `$mail_sent = mail($recipient, $subject, $mailBody, "From: $sender <$senderEmail>", $headers);` then it's sending mails to email as HTML code as previuos. not a nice visual look. If use `$mail_sent = mail($recipient, $subject, $mailBody, "From: $sender <$senderEmail>", $headers);` it's not sending the mail. – Riffaz Starr Sep 02 '14 at 09:31
  • add $msg line i have added inline css for styling to ur code it will be nicer! – black Sep 02 '14 at 09:35
  • Thanks for that.. Im gonna try that shortly.. But I suspect the mime type issue arise because I use this in a wordpress side bar. Some one who edited my title he removed the word wordpress. – Riffaz Starr Sep 02 '14 at 10:00
1

Please try this

<?php

    if($_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 } ?

>

  • I get **Parse error: syntax error, unexpected T_VARIABLE in** – Riffaz Starr Sep 02 '14 at 08:19
  • I've found the reason. It's because of the right above the . it's not closed. any how where to put the $headers? It's just a variable for now. Not printing anywhere – Riffaz Starr Sep 02 '14 at 09:28
  • Ok Update your answer. Thank you. – Roshan Dandgavhal Sep 02 '14 at 10:40
  • Now the problem is adding MIME VERSION header. If I use `$mail_sent = mail($recipient, $subject, $mailBody, "From: $sender <$senderEmail>");` then it's fine. It sends mail to the email. If I use `$mail_sent = mail($recipient, $subject, $mailBody, "From: $sender <$senderEmail>", $headers);` then it's not sending mail. May be the reason is I am using this on a wordpress site side bar. Someone who edited my question deleted the word wordpress. Any suggestions? – Riffaz Starr Sep 02 '14 at 10:45
  • Hello Riffaz The way you are follow to send mail is right. but my suggestion use SMTP authentication instead of this. because if you sent mail trough php mail function this would be receive in spam. I have sent you link. which may help you lot in future. http://asif18.com/7/php/send-mails-using-smtp-in-php-by-gmail-server-or-own-domain-server/ – Roshan Dandgavhal Sep 02 '14 at 11:39
-2

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";
Lee
  • 4,187
  • 6
  • 25
  • 71
  • Sorry I don't understand what's wrong with this. It's addressing what the OP has asked. Nothing too complicated, and would work in the way that he or she wants. So if you think there's something vitally wrong with my answer, would you mind making me aware please? – Lee Sep 02 '14 at 07:58
  • Your answer is a tip, and not a very good one. You don't address the fact that Riffaz Starr didn't use the correct mime encapsulation or headers, and that's the reason he sees raw HTML in his email. I do appreciate your input, but you asked the question, I answered it, nothing more. – KIKO Software Sep 02 '14 at 08:03