如何将CSS附加到PHP中的mail()方法


How do I attach CSS to the mail() method in PHP?

当用户忘记密码时,我收到了一封电子邮件,其中包含以下php代码:

<?php
//OTHER CODE BEFORE ALL THIS
$to = "$useremail";
$from = "forgotpassword@mysite.com";
$headers ="From: $from'n";
$headers .= "MIME-Version: 1.0'n";
$headers .= "Content-type: text/html; charset=iso-8859-1 'n";
$subject = 'Temporary Password';
$msg = '<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Message</title>
</head>
<body>
<div>
  <div>Temporary Password</div>
  <p>Hello<b> '.$f.'</b>,</p>
  <p>You indicated that you forgot your login password. We can generate a temporary password for you to log in with, then once logged in you can change your password to anything you like.</p>
  <p>After you click the link below your password to login will be:</p>
  <p><b>'.$tempPass.'</b></p>
  <div style="margin: 0px auto;
text-align: center;">
    <p><a style="   background: #f3c1e6;
    background: -webkit-gradient(linear, 0 0, 0 bottom, from(#f3c1e6), to(#de66c0));
    background: -moz-linear-gradient(#f3c1e6, #de66c0);
    background: linear-gradient(#f3c1e6, #de66c0);
    border: solid 1px #cd5daf;
    border-bottom: solid 3px #ce5eb0;
    box-shadow: inset 0 0 0 1px #e998d3;
    color: #fff;
    text-shadow: 0 1px 0 #ce5eb0;
    zoom: 1;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    -ms-border-radius: 5px;
    -o-border-radius: 5px;
    border-radius: 5px;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    -webkit-box-shadow: 0 -1px 0 rgba(255,255,255,0.1) inset;
    -moz-box-shadow: 0 -1px 0 rgba(255,255,255,0.1) inset;
    box-shadow: 0 -1px 0 rgba(255,255,255,0.1) inset;
    der-radius: 5px;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -moz-inline-stack;
    display: inline-block;
    vertical-align: middle;
    font-weight: bold;
    padding: 12px 15px;
    font-size: 14px;
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    cursor: pointer;" href="http://www.bragmouth.com/forgot_pass.php?u='.$u.'&p='.$hashTempPass.'">Click Here to Apply</a></p>
  </div>
</div>
</body>
</html>
';
if(mail($to,$subject,$msg,$headers)) {
echo 'true';
exit();
} else {
echo 'false';
exit();
}
    } else {
        echo 'false';
    }
    exit();
}
?>

我的问题是,我无法通过内部或外部样式表的方式向此电子邮件添加任何CSS。但是,我可以将内联样式添加到HTML中的任何标记中。我需要将内容类型更改为text/css还是可以有两种内容类型?

此外,我无法在雅虎邮件中显示渐变(这可能是他们的错)。我可以让它们出现在谷歌邮件中,除了链接(这就是我发布链接风格的原因)。这也是谷歌的错吗?这一切可以通过附加样式表来解决吗?

我建议不要在您的电子邮件html中使用css样式。Outlook对他们的CSS做了一些奇怪的事情(他们甚至改变了你的HTML来添加他们的自定义类),我相信Gmail去掉了head标签。

我用一个工具将CSS转换为内联样式:

Css Inliner

您不应该自己构建邮件头,因为有很多小事情可能会出错。只需使用像Swiftmailer这样广泛使用并经过测试的邮件即可。然后,您可以将css文件作为附件添加到邮件中,并直接在邮件中使用它,请参阅此处。

编辑:好吧,我错了,这对CSS文件不起作用,但你可以将普通CSS的HTML文件转换为内联CSS的HTML文件,请参阅此处