通过 PHP 发送包含 CSS 样式表的 HTML 电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20637548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:22:02  来源:igfitidea点击:

Send HTML email including CSS style sheet via PHP

phphtmlcssemail

提问by Wijden

I want to send a HTML page that contains CSS stylesheet to an email account. The problem is that the CSS style sheet is not visible at all. Do I have to inline every CSS styling? It seems to be highly inefficient method. Any others ideas ?

我想将包含 CSS 样式表的 HTML 页面发送到电子邮件帐户。问题是 CSS 样式表根本不可见。我是否必须内联每个 CSS 样式?这似乎是一种非常低效的方法。任何其他想法?

Below is the PHP code for sending the email :

以下是用于发送电子邮件的 PHP 代码:

<?php
$to = "[email protected]";

// subject
$subject = "Test mail";

// message
$message = file_get_contents("index.html");

// from
$from = "[email protected]";

// 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 .= "From:" . $from;

// Mail it
mail($to,$subject,$message,$headers);

echo "Mail Sent.";
?>

I would greatly appreciate your help. Thanks in advance.

我将不胜感激您的帮助。提前致谢。

回答by Виктор Иванов

It would be best to just put a <style>block with all the CSS inside it somewhere in the index.htmlbody. Some email clients/portals cut all the CSS embedded in the header with <link rel="stylesheet" type="text/css" href="..." >. As a result not using inline CSS may cause your emails to appear 'broken' or simply look bad on accounts with @gmail.comfor example.

最好将一个<style>包含所有 CSS的块放在index.htmlbody 的某个地方。一些电子邮件客户端/门户使用<link rel="stylesheet" type="text/css" href="..." >. 因此,不使用内联 CSS 可能会导致您的电子邮件出现“损坏”或只是在帐户上看起来很糟糕@gmail.com,例如。

回答by JFly28

Google, Outlook, Yahoo all the major email service providers do not allow CSS external files. It may sound antiquated that they have opted for inline styles and tables to render a proper email message with any styling.

Google、Outlook、Yahoo 所有主要的电子邮件服务提供商都不允许 CSS 外部文件。他们选择内联样式和表格来呈现具有任何样式的正确电子邮件消息,这听起来可能已经过时了。

It may be tedious to code but companies like https://litmus.com/and envato will help with some templates and formatting issues.

编码可能很乏味,但像https://litmus.com/和 envato这样的公司会帮助解决一些模板和格式问题。

my 2¢'s: email providers can't trust all sources of information being sent and so this is a way of keeping the malicious code at bay.

我的 2¢'s: 电子邮件提供商不能信任发送的所有信息来源,因此这是一种阻止恶意代码的方法。

回答by Kyle Papili

Within your index.html file, include a <style>tag as below.

在您的 index.html 文件中,包含<style>如下标记。

<style>
.class{
style: attribute; 
}
</style>";

That should work, if not then I may recommend hosting your stylesheet on a website and including a <link>tag similar to the one below in the head of your index file

那应该可行,如果不行,那么我可能会建议将您的样式表托管在网站上,并<link>在索引文件的头部包含一个类似于以下标签的标签

<link rel='stylesheet' type='text/css' href='http://www.yourstylesheet.com'>";

Either of those methods should help with your problem.

这些方法中的任何一种都应该有助于解决您的问题。

回答by Roman

Just embed the css-code into the <head>of that html-email.

只需将 css 代码嵌入到该<head>html 电子邮件中。