如何设置php电子邮件表单的样式?

时间:2014-09-02 07:25:39

标签: php html css email

我正在尝试创建一个php表单。当我在电子邮件中收到提交的表格时,我应该看起来很好看。

如果输入以下代码,我会得到这个![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>";

当我获取php表单时,如何获得一个好的scc表或div像html文档?

3 个答案:

答案 0 :(得分: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/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); 
?>

答案 1 :(得分:1)

请试试这个

<?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 } ?

&GT;

答案 2 :(得分:-2)

在为电子邮件表单的输出设置样式时,您无需使用<html><body>

您只需要从table元素开始,并使用一些内联样式。下面的代码只是给出一个简单的表格,名称和电子邮件标签以粗体显示,每个单元格周围都有一个小填充。

$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>";

您还需要确保为邮件标题设置了正确的MIME设置,否则不会显示HTML格式,只会输出。

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