PHP电子邮件格式化内容


PHP email formatted content

我正在为我的网站创建一个电子邮件表单,这将允许用户创建电子邮件,似乎是从他们希望的任何电子邮件地址发送。即davidcameron@downingstreet.com。

表单工作得很好,直到我做了一个小的改变。我以前允许用户提交电子邮件的内容,但发现很难格式化内容(例如,将区域加粗,使用表格等)。

(现在的形式代码)

<html>
<body>
<form method="GET" action="send.php">
<p>To: <input type="text" name="to" /></p>
<p>From-Name: <input type="text" name="name" /></p>
<p>From-Email: <input type="text" name="from" /></p>
<p>Subject: <input type="text" name="subject" /></p>

<input type="submit" value="Send E-Mail" ></p>
</form>
</body>
</html>

所以我认为它会更容易提交的内容自己在我的'send.php'代码,实际发送电子邮件消息。如下所示:

 <html>
    <body>
    <?php
    $to =$_REQUEST['to'];
    $subject = $_REQUEST['subject'];
    $name =$_REQUEST['name'];
    $from = $_REQUEST['from'];
    $content = ?><font face="arial"><b>Your question has been received/b>
    Your booking has been confirmed with the supplier.
    Please visit the <a href="questionstory.co.uk"> Question Portal</a> for more information.
    <table>
    <tr>
    <td>Question subject</td>
    <td> Room</td>
    </tr>
    <tr>
    <td>Traveller</td>
    <td>Maria Smith</td>
    </tr>
    <td>Requester</td>
    <td>Tony Smith</td>
    </tr>
    </table>
    <br/>

     </font>
    <?
    $header="From: $from"."<$sender_email>'r'n";
    mail($to,$subject,$content,$header);
    echo 'sent successfully';
    ?>
    </body>
    </html>

但是,当我单击submit时,服务器发现了一个错误。我只能假设这是由于我的HTML元素,因为没有它之前不会发生这个错误。

有没有人能建议我如何解决这个问题/如果你能建议一个更简单的方法来格式化我想要的电子邮件内容?

多谢

设置你的内容值为

$content = ''

由于你的$content是未定义的,这就是为什么你得到错误。

试试这个

<?php
    $to = $_REQUEST['to'];
    $subject = $_REQUEST['subject'];
    $name = $_REQUEST['name'];
    $from = $_REQUEST['from'];
    $content =
        '<span style="font-family: arial"><b>Your question has been received</b>
            <br>
            Your booking has been confirmed with the supplier.
            Please visit the <a href="questionstory.co.uk"> Question Portal</a> for more information.
            <table>
                <tr>
                    <td>Question subject</td>
                    <td> Room</td>
                </tr>
                <tr>
                    <td>Traveller</td>
                    <td>Maria Smith</td>
                </tr>
                <td>Requester</td>
                <td>Tony Smith</td>
                </tr>
            </table>
            <br/>
            </span>';
    $header = "From:".$from;
    $result = mail($to,$subject,$content,$header);
    if(isset($result))
    {
        echo 'sent successfully';
    }
    else
    {
        echo 'Failed';
    }

?>

注意:HTML5不支持<font>标签

编辑01

也包括这几行

$header .= 'MIME-Version: 1.0';
$header .= 'Content-Type: text/html; charset="ISO-8859-1';