添加样式到php html电子邮件

时间:2016-02-27 09:02:39

标签: 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

4 个答案:

答案 0 :(得分:3)

您需要使用内联样式才能使其适用于您的电子邮件

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

答案 1 :(得分:0)

实际上,您发送的电子邮件将以不同的方式打开,可以通过安装在机器上的电子邮件客户端来集成HTML呈现引擎,也可以是在线电子邮件客户端,这将使您的电子邮件变得相当特殊roping和撤销一些HTML属性。 Campaign Monitor提出的表格可以让您快速了解损坏的程度,我们注意到,根据电子邮件客户端,规则完全不同。
链接:https://www.campaignmonitor.com/css/
但是有一些工具,比如inline-gulp-css,可以让你的HTML风格变成“在线” 链接:https://www.npmjs.com/package/gulp-inline-css

答案 2 :(得分:0)

试着让它像模板一样。

这里的一些例子可能会有所帮助。

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

<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 :(得分:-1)

TRY

<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>