【问题标题】:Adding style to a php html email将样式添加到 php html 电子邮件
【发布时间】:2016-06-10 14:48:57
【问题描述】:

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

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

【问题讨论】:

  • 电子邮件支持内联css。使用内联css进行邮件设计。
  • 请注意,您的代码(以及所有发布的答案)容易受到标头注入攻击,可能还有其他攻击,并且无法正确编码电子邮件内容。不要滚动你自己的电子邮件代码,你会做错的,这一切都证明了;使用像 PHPMailer 这样的库来标记这个问题。
  • 您的字符集应该一致——您在 HTML 中设置了 UTF-8,但在邮件标题中设置了 ISO-8859-1;你不能同时拥有两者 - 一旦你有非 ascii 文本,它就会中断

标签: php html css email phpmailer


【解决方案1】:

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

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

【讨论】:

  • 你知道为什么我的名字在发送这个时会出现在时事通讯中吗?我认为这是因为我的标题。所以我尝试这样做$headers .= 'From: Company'"\r\n";,它破坏了代码
  • @Ralph 你的意思是它显示为newsletter 而不是newsletter@example.com
  • 不,当您进入 gmail 或您使用的任何邮件客户端时,在主题行旁边,它显示电子邮件来自谁,我的说 newsletter。我想让它说我的Company Name。所以如果我是百思买,我希望它说Best Buy
  • 我相信 PHP 标头中的 from 参数正在寻找电子邮件地址。
  • 您需要将变量 $from 更改为 $from = "Best Buy"; 而不是 $from = "newsletter@example.com";
【解决方案2】:

试着让它像模板一样。

这里有一个小例子可能会有所帮助。

创建 my_template.html 文件并在该文件中添加以下代码。

<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>
<body>
<p>Hi {{USERNAME}}</p>
<p>Please click on below link </p><br>
{{LINK}}
</body>
</html>

现在在您想要的地方编写发送电子邮件功能。并按照以下步骤操作:

1) 读取您最近创建的 HTML 文件。

$html = file_get_contents('my_template.html');

2) 替换变量

$link = "<a href='LINK YOU WANR TO PLACE'>Click Here </a>";
$html =  str_replace("{{USERNAME}}",$username,$html);
$html =  str_replace("{{LINK}}",$link,$html);

3) 最后发送电子邮件。

            $from = "newsletter@example.com";
            $headers .= 'To: ' .$to. "\r\n";
            $headers .= 'From: ' .$from. "\r\n";
            $headers .= 'Bcc: '.$Bcc. "\r\n";

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

【讨论】:

    【解决方案3】:

    确实,您发送的电子邮件将以不同的方式打开,或者通过安装在计算机上的电子邮件客户端,该客户端将集成专有的 HTML 渲染引擎,或者通过在线电子邮件客户端将您的电子邮件以相当特殊的方式打开绳索和撤回一些 HTML 属性。 Campaign Monitor 提出的表格可让您快速了解损坏程度,我们注意到,根据电子邮件客户端,规则完全不同。
    链接:https://www.campaignmonitor.com/css/
    但是,有一些工具,例如 inline-gulp-css,可以将您的 HTML 样式转换为“在线”。
    链接:https://www.npmjs.com/package/gulp-inline-css

    【讨论】:

      【解决方案4】:

      试试

      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <title></title>
      
      
      </head>
      <body>
                      <div style="background:#151515; color: #FFF;">
                      <p>Hi,</p><br>
                      <p>Thank you.</p><br>
                      <p>Thank you,</p>
                      <p>Administration</p>
                      </div>
      </body>
      </html>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-19
        • 1970-01-01
        • 2015-07-05
        • 1970-01-01
        • 1970-01-01
        • 2012-03-04
        • 2021-11-06
        • 1970-01-01
        相关资源
        最近更新 更多