我如何使用HTML标记来样式化PHP代码? [关闭]

问题描述 投票:0回答:1

单击提交按钮后,我将触发一封电子邮件,其中包含填写好的表格中的数据。电子邮件成功发送了正确的数据,但是,我希望能够对电子邮件应用一些基本样式,例如某些<h3><strong>标签。我可以在php $message变量中执行此操作吗?如果可以,那会是什么样?

<?php 
    $to = "[email protected]"; // this is your Email address
    $from = "[email protected]"; // this is the sender's Email address

    $company_name = $_POST['company_name'];
    $rep_name = $_POST['rep_name'];
    $prod_type = $_POST['prod_type'];
    $address = $_POST['address'];
    $city = $_POST['city'];
    $state = $_POST['state'];
    $zip = $_POST['zip'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];

    $subject = "New Form Submission";

    $message =  "New Form Submission" . "\n" . "\n" .
                "Company Name: " . $company_name . "\n" .
                "Representative Name: " . $rep_name . "\n" .
                "Product Type: " . $prod_type . "\n" .
                "Address: " . $address . "\n" .
                "City: " . $city . "\n" .
                "State: " . $state . "\n" .
                "Zip: " . $zip . "\n" .
                "Phone: " . $phone . "\n" .
                "Email: " . $email . "\n";

    $headers = "From:" . $from;
    mail($to,$subject,$message,$headers);
?>

编辑:我尝试使用在此链接Defining html code inside PHP variables上找到的ob_start()和ob_get_clean(),但没有成功

php html html-email
1个回答
3
投票

设置Content-Type: text/html标头以启用HTML电子邮件;

$headers = "From:" . $from;
$headers .= "Content-Type: text/html";

向电子邮件中添加一些HTML元素;

$message = <<<EOL
    <h1>Hi!</h1>
    <h2>HTML Emails are awesome!</h2>
EOL;
mail($to, $subject, $message, $headers);


小例子
<?php

$to = '----';
$subject = 'SO test Mail';

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

$message = <<<EOL
<html>
    <body>
        <h1>Hi!</h1>
        <h2>HTML Emails are awesome!</h2>
    </body>
</html>
EOL;

if ($res = (mail($to, $subject, $message, $headers))) {
    echo 'OK';
} else {
    echo 'Error';
    var_dump($res);
}

enter image description here


-1
投票

您可以在php中插入html代码。对于样式,您应使用CSS并以这种方式编辑元素。如果将这些语言分开对待并将它们合并到html文件中,这也可以帮助您获得良好的概述。

© www.soinside.com 2019 - 2024. All rights reserved.