为电子邮件模板中动态添加的 html 代码设置样式

Set styles for dynamically added html code in email template

我目前正在开发响应式电子邮件模板,但遇到了如何设置动态添加的 html 代码样式的问题。

由于大多数电子邮件客户端不支持嵌入式 CSS 我添加了内联样式,因为模板的一部分是动态加载的,所以我无法为该部分添加内联样式。

示例:我需要将以下 html 示例代码动态加载到我的电子邮件模板中。

<p>bla bla bla .....</p>
<a href=""><img src="" class="" /></a>
<p>bla bla bla .....</p>

在这个例子中,应该如何为 img 元素设置一个 max-width,使其适用于大多数电子邮件客户端?

请注意,我使用 PHP 作为编程语言。

提前致谢。

如果你可以设置 classsrc,你可以这样做:

 $class='something';

 $class= $class. '"' . ' style="max-wdth:100px;';

那么你将拥有:

 <img src="" class="something" style="max-wdth:100px;" />

我使用 html dom parser 对 html 代码进行如下更改,

function add_inline_styles($string){
    $string = str_get_html($string);
    foreach($string->find('img') as $key => $img_tags) {
        $img_tags->setAttribute("style", max-width: 570px;");
    }
    return $string;
}

为了使用 dom 解析器,您应该包含 simple_html_dom.php and for more information please follow the documentation.