php - 向 php html 电子邮件添加样式

标签 php html css email phpmailer

我四处寻找这个问题,我发现的只是添加以下内容:

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

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

我想发送一封时事通讯类型的电子邮件,因此样式对此非常重要。我看的所有视频都只是制作 html 表,所以我真的不明白。我想为我的电子邮件中的内容设置样式。

我现在有这个:

$to = $newsletter_email;
        $subject = 'Thank you for subscribing';
        $message = '
            <html>
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                <title></title>
            <style>
                #email-wrap {
                background: #151515;
                color: #FFF;
                }
            </style>
            </head>
            <body>
                <div id="email-wrap">
                <p>Hi,</p><br>
                <p>Thank you.</p><br>
                <p>Thank you,</p>
                <p>Administration</p>
                </div>
            </body>
            </html>
                ';

                $from = "newsletter@example.com";
                //$Bcc = "example@example.com";

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

                // Additional headers
                $headers .= 'To: ' .$to. "\r\n";
                $headers .= 'From: ' .$from. "\r\n";
            //  $headers .= 'Bcc: '.$Bcc. "\r\n";

                // Send the email
                mail($to,$subject,$message,$headers);

我已经尝试从这个消息变量中取出样式并将这个文件变成一个 html 样式的文件,在 php 之外:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Newsletter</title>
<style>
#email-wrap {
    background: #151515;
    color: #FFF;
}
</style>
</head>

等等

电子邮件实际上已发送,但我不知道如何为其添加样式。我做错了什么??

            $email_from = "newsletter@example.com";

            $full_name = 'Company Name';
            //$from_mail = $full_name.'<'.$email_from.'>';
            $from = $from_mail;

            //$from = "newsletter@example.com";
            //$Bcc = "example@example.com";

            // To send HTML mail, the Content-type header must be set
            $headers .= "From: ".$full_name." <".$email_from.">\r\n";
            and $headers .= "Return-Path: ".$full_name." <".$email_from.">\r\n";
            /*$headers = "" .
                       "Reply-To:" . $from . "\r\n" .
                       "X-Mailer: PHP/" . phpversion();*/
            $headers  = 'MIME-Version: 1.0' . "\r\n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

            // Additional headers
            $headers .= 'To: ' .$to. "\r\n";
            $headers .= 'From: ' .$from_email. "\r\n";
        //  $headers .= 'Bcc: '.$Bcc. "\r\n";

            // Send the email
            mail($to,$subject,$message,$headers);

enter image description here

最佳答案

您需要使用内联样式才能在您的电子邮件中使用

    $to = $newsletter_email;
    $subject = 'Thank you for subscribing';
    $message = '
        <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <title></title>
        </head>
        <body>
            <div id="email-wrap" style='background: #151515;color: #FFF;'>
            <p>Hi,</p><br>
            <p>Thank you.</p><br>
            <p>Thank you,</p>
            <p>Administration</p>
            </div>
        </body>
        </html>
            ';

            $from = "newsletter@example.com";
            //$Bcc = "example@example.com";

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

            // Additional headers
            $headers .= 'To: ' .$to. "\r\n";
            $headers .= 'From: ' .$from. "\r\n";
        //  $headers .= 'Bcc: '.$Bcc. "\r\n";

            // Send the email
            mail($to,$subject,$message,$headers);

对于你问题的第二部分,你可以使用这样的东西

$to = 'test@server.com';
$email_from = "best.buy@yahoo.com";

$full_name = 'Best Buy';
$from_mail = $full_name.'<'.$email_from.'>';
$from = $from_mail;
$headers = "" .
           "Reply-To:" . $from . "\r\n" .
           "X-Mailer: PHP/" . phpversion();
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
$headers .= 'From: ' . $from_email . "\r\n";       
mail($to,$subject,$message,$headers);

关于php - 向 php html 电子邮件添加样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35667790/

相关文章:

javascript - 如果网页的背景很重要怎么办

php - Laravel 5.1 如何将对象的 id 传递到另一个 View 以用作另一个表中的外键?

c# - 如何编辑所有特殊 html 标签的属性(如 a)

php - PHP中的静态类初始化器

javascript - 目标 ID 而不是 <li>

html - 官方标题使用原则?

asp.net - 当窗口变小时,如何防止面板被强制置于另外两个面板下方?

css - 有没有办法使用 Javascript/css 在 vw 和 vh(视口(viewport)宽度/高度)之间动态切换?

php - 用户登录后立即更改 wordpress 站点中的 css 值

php - OpenTBS Word 文档隐藏表格