使用PHP表单作为HTML发送邮件(不是纯文本)

时间:2015-05-26 07:25:25

标签: php html htmlspecialchars

我需要从网络表单发送电子邮件。 收到的电子邮件必须显示为HTML,而不是文本。

 function createMailBodyLine ( $title, $value ) {
            $value = nl2br(htmlspecialchars($value));
            return "<tr><td style='vertical-align:top; text-align:right; color:#666666; padding-right:10px;'>{$title}:</td><td>{$value}</td></tr>";
        }

        $from = ins_lgrb('contacts_email', null, true);

        $to   = 'xx@xxv.lv;xx@xxv.lv';

        $subj = ' sssssssss';

           $body  = "<html><head><title>sssssssss</title></head><body>";
        $body .= "Jauna ziņa <br/><br/>";
        $body .= "<table cellspacing='0' cellpadding='0'>";
        $body .= createMailBodyLine('Vārds, uzvārds', $_POST['name_surname']);
        $body .= createMailBodyLine('E-pasta adrese', $_POST['mail']);
        $body .= createMailBodyLine('Pasta adrese'  , $_POST['address']);
        $body .= createMailBodyLine('Sūtītāja IP'   , $_SERVER['REMOTE_ADDR']);
        $body .= createMailBodyLine('Laiks'         , date('d.m.Y, H:i:s', $_SERVER['REQUEST_TIME']));
        $body .= createMailBodyLine('Jautājums'     , $_POST['question']);
        $body .= "</table></body></html>";

        mb_send_mail($to, $subj, $body, "From: {$from}, nMIME-Version: 1.0nContent-type: text/html; charset=utf-8n");

        reload();
    }
    savePost();

在电子邮件中显示所有html代码,如下所示:

<html><head><title>Jauna ziņa </title></head><body>Jauna ziņa<br/><br/><table cellspacing='0' cellpadding='0'><tr><td style='vertical-align:top; text-align:right; color:#666666; padding-right:10px;'>Vārds, uzvārds:</td><td></td></tr><tr><td style='vertical-align:top; text-align:right; color:#6666.....d></tr></table></body></html>

出现问题:htmlspecialchars

3 个答案:

答案 0 :(得分:0)

看起来像标题错误。删除&#34; n&#34;之前&#34; MIME&#34;并看看它是否有效。

http://php.net/manual/en/function.mb-send-mail.php

答案 1 :(得分:0)

$headers来电的mb_send_mail参数中,您不会将Content-type标题与其他所有标题分开。试试这个:

"From: {$from}, \r\nMIME-Version: 1.0\r\nContent-type: text/html; charset=utf-8"

答案 2 :(得分:0)

你可以切换到php邮件https://github.com/PHPMailer/PHPMailer phpmailer具有所有与电子邮件相关的readymate功能,可将电子邮件设置为html 你可以使用$ mail-&gt; IsHTML(true); 阅读Add HTML formatting in phpmailer