Browse Source

Le reglage configurable "Transformer les styles contenus entre <head> </head> en des styles en ligne" n'a pas de sens en tant que filtre systematique applique a tous les mails :

- il casse les mails faits mains sur mesure (comme le wrapper html des emails texte du plugin) car incapable de gerer les @media
- il peut etre utile sur certains mails html simples
- il est a utiliser au cas par cas, et c'est le webmestre qui code son mail html qui peut le savoir, pas l'admin qui configure le site et utilise simplement

En consequence, le reglage disparait de la configuration et n'est plus pris en compte
Par compatibilite la methode ConvertirStylesEnligne de la classe Facteur reste disponible, mais tout son code est deporte dans le filtre facteur_convertir_styles_inline
que l'on appellera au cas par cas dans les squelettes HTML par un appel #FILTRE{facteur_convertir_styles_inline}

Ainsi on preserve son usage quand necessaire, sans plus risque de casser les emails HTML bien fichus
On incremente la version en 2.2.0 pour marquer la difference fonctionnelle
v2
cedric@yterium.com 11 years ago
parent
commit
baf543ec3a
  1. 167
      classes/facteur.php
  2. 164
      facteur_fonctions.php
  3. 5
      formulaires/configurer_facteur.html
  4. 2
      formulaires/configurer_facteur.php
  5. 2
      paquet.xml

167
classes/facteur.php

@ -94,8 +94,6 @@ class Facteur extends PHPMailer {
$message_html = unicode_to_utf_8(charset2unicode($message_html,$GLOBALS['meta']['charset']));
$this->Body = $message_html;
$this->IsHTML(true);
if ($GLOBALS['meta']['facteur_filtre_css'])
$this->ConvertirStylesEnligne();
if ($GLOBALS['meta']['facteur_filtre_images'])
$this->JoindreImagesHTML();
$this->UrlsAbsolues();
@ -276,168 +274,11 @@ class Facteur extends PHPMailer {
}
/**
* Compat ascendante, obsolete
*/
function ConvertirStylesEnligne() {
/*
Written by Eric Dols - edols@auditavenue.com
You may freely use or modify this, provided
you leave credits to the original coder.
Feedback about (un)successfull uses, bugs and improvements done
are much appreciated, but don't expect actual support.
PURPOSE OF THIS FUNCTION
It is designed to process html emails relying
on a css stylesheet placed in the <head> for layout in
order to enhance compatibility with email clients,
including webmail services.
Provided you use minimal css, you can keep styling separate
from the content in your email template, and let this function
"inject" those styles inline in your email html tags on-the-fly,
just before sending.
Technically, it grabs the style declarations found in the
<head> section and inserts each declaration inline,
inside the corresponding html tags in the email message.
Supports both HTML and XHTML markup seamlessly. Thus
tolerant to email message writers using non-xhtml tag,
even when template is xhtml compliant (e.g. they would
add <img ...> instead of a xhtml compliant <img ... />).
NEW 10 dec. 2003:
- code revised, including a few regexp bugs fixed.
- multiple class for a tag are now allowed <p class="firstclass secondclass">
- all unsupported css styles are now moved to the body section (not just a:hover etc...)
USE
Add this function to a function library include, like "inline.inc"
and include it near the beginning of your php page:
require ("inline.inc");
load the html source of message into a variable
like $html_source and process it using:
$html_source = sheet2inline($html_source)
STYLE DEFINITIONS SUPPORTED
TAG { ... }
TAG1, TAG2, ... { ... }
TAG.class { ... }
.class { ...)
TAG:pseudo { ... }
CSS definitions may be freely formatted (spaces, tabs, linefeeds...),
they are converted to oneliners before inserting them inline in the html tags.
.class definitions are processed AFTER tag definitions,
thus appended inline after any existing tag styling to
preserve the normal css priority behavior.
Existing style="..." attributes in tags are NOT stripped. However they MUST
be with double quotes. If not, an addtional style="..." attribute will be added
KNOWN LIMITATIONS
- style info should be placed in <head> section. I believe
it shouldnt be too hard to modify to point to an external
stylesheet instead.
- no support (yet?):
* chains like P UL LI { .... } or P UL LI.class { .... }
* #divname p { ... } and <tag id="...">
* a:hover, a:visited {...} multiple class:pseudo
They require a significantly more complicated processing likely
based on stylesheet and document trees parsing.
Many email clients don't handle more than what is supported
by this script anyway.
- pseudo-classes like a:hover {...} can't be inserted inline
in the html tags: they are moved to a <style> declaration in
the <body> instead. This is a limitation from html, not this script.
- It is still up to you to check if target email clients render
your css styled templates correctly, especially webmail services
like Hotmail, in which the email becomes a sub-part of an html page,
with styles already in place.
*/
// variables to be accessed in the callback sub-function too
global $styledefinition, $styletag, $styleclass;
// Let's first load the stylesheet information in a $styles array using a regexp
preg_match_all ( "/^[ \t]*([.]?)([\w, #]+)([.:])?(\S*)\s+{([^}]+)}/mi", $this->Body , $styles);
/*
$styles[1] = . or '' => .class or tag (empty)
$styles[2] = name of class or tag(s)
$styles[3] = : . or '' => followed by pseudo-element, class separator or nothing (empty)
$styles[4] = name of pseudo-element after a tag, if any
$styles[5] = the style definition itself, i.e. what's between the { }
*/
// Now loop through the styles found and act accordingly;
// process TAG {...} & TAG1, TAG2,... {...} definitions only first by order of appearance
foreach ($styles[1] as $i => $type) {
if ($type=="" && $styles[3][$i]=="") {
$styledefinition = trim($styles[5][$i]);
$styletag = preg_replace("/ *, */", "|", trim($styles[2][$i])); //echo $styletag."<br />";
$styleclass = "";
// process TAG {...} and TAG1, TAG2 {...} but not TAG1 TAG2 {...} or #divname styles
if (!preg_match("/ /", $styletag) && !preg_match("/#/", $styletag)) {
$pattern = "!<(".$styletag.")([^>]*(?= /)|[^>]*)( /)?>!mi";
$this->Body = preg_replace_callback ($pattern, 'facteur_addstyle' , $this->Body);
$styles[6][$i]=1; // mark as injected inline
}
}
}
// append additional .CLASS {...} and TAG.CLASS {...} styling by order of appearance
// important to do so after TAG {...} definitions, so that class attributes override TAG styles when needed
foreach ($styles[1] as $i => $type) {
if ($type!="." && $styles[3][$i]=="." ) { // class definition for a specific tag
$styledefinition = trim($styles[5][$i]);
$styletag = trim($styles[2][$i]);
$styleclass = trim($styles[4][$i]);
$pattern = "!<(".$styletag.")([^>]* class\=['\"][^'\"]*".$styleclass."[^'\"]*['\"][^>]*(?= /)|[^>]* class\=['\"][^'\"]*".$styleclass."[^'\"]*['\"][^>]*)( />)?>!mi";
$this->Body = preg_replace_callback ($pattern, 'facteur_addstyle' , $this->Body);
$styles[6][$i]=1; // mark as injected inline
}
elseif ($type=="." && $styles[3][$i]=="" ) { // general class definition for any tag
$styledefinition = trim($styles[5][$i]);
$styletag = "";
$styleclass = trim($styles[2][$i]);
$pattern = "!<(\w+)([^>]* class\=['\"]".$styleclass."['\"][^>]*(?= /)|[^>]* class\=['\"]".$styleclass."['\"][^>]*)( />)?>!mi";
$this->Body = preg_replace_callback ($pattern, 'facteur_addstyle' , $this->Body);
$styles[6][$i]=1; // mark as injected inline
}
}
/* move all style declarations that weren't injected from <head> to a <body> <style> section,
including but not limited to:
- pseudo-classes like a:hover {...} as they can't be set inline
- declaration chains like UL LI {...}
- #divname {...}. These are not supported by email clients like Mac/Entourage anyway, it seems. */
foreach ($styles[1] as $i => $type) {
if ($styles[6][$i]=="") {
// add a <style type="text/css"> section after <body> if there's isn't one yet
if (preg_match ("!<body[^>]*>\s*<style!mi", $this->Body)==0) {
$this->Body = preg_replace ("/(<body[^>]*>)/i", "\n\$1\n".'<style type="text/css">'."\n<!--\n-->\n</style>\n", $this->Body);
}
// append a copy of the pseudo-element declaration to that body style section
$styledefinition = trim($styles[5][$i]);
$styledefinition = preg_replace ("!\s+!mi", " ", $styledefinition ); // convert style definition to a one-liner (optional)
$declaration = $styles[1][$i].trim($styles[2][$i]).$styles[3][$i].trim($styles[4][$i])." { ".$styledefinition." }";
$this->Body = preg_replace ("!(<body[^>]*>\s*<style[^>]*>\s*<\!\-\-[^>]*)"."(\s*\-\->\s*</style>)!si", "\$1".$declaration."\n\$2", $this->Body);
$styles[6][$i]= 2; // mark as moved to <style> section in <body>
}
}
// remove stylesheet declaration(s) from <head> section (comment following line out if not wanted)
//$this->Body = preg_replace ("!(<head>.*)<style type.*</style>(.*</head>)!si", "\$1\$2" , $this->Body);
// check what styles have been injected
# print_r($styles);
$this->Body = facteur_convertir_styles_inline($this->Body);
}

164
facteur_fonctions.php

@ -8,6 +8,170 @@
if (!defined("_ECRIRE_INC_VERSION")) return;
/*
Written by Eric Dols - edols@auditavenue.com
You may freely use or modify this, provided
you leave credits to the original coder.
Feedback about (un)successfull uses, bugs and improvements done
are much appreciated, but don't expect actual support.
PURPOSE OF THIS FUNCTION
It is designed to process html emails relying
on a css stylesheet placed in the <head> for layout in
order to enhance compatibility with email clients,
including webmail services.
Provided you use minimal css, you can keep styling separate
from the content in your email template, and let this function
"inject" those styles inline in your email html tags on-the-fly,
just before sending.
Technically, it grabs the style declarations found in the
<head> section and inserts each declaration inline,
inside the corresponding html tags in the email message.
Supports both HTML and XHTML markup seamlessly. Thus
tolerant to email message writers using non-xhtml tag,
even when template is xhtml compliant (e.g. they would
add <img ...> instead of a xhtml compliant <img ... />).
NEW 10 dec. 2003:
- code revised, including a few regexp bugs fixed.
- multiple class for a tag are now allowed <p class="firstclass secondclass">
- all unsupported css styles are now moved to the body section (not just a:hover etc...)
USE
Add this function to a function library include, like "inline.inc"
and include it near the beginning of your php page:
require ("inline.inc");
load the html source of message into a variable
like $html_source and process it using:
$html_source = sheet2inline($html_source)
STYLE DEFINITIONS SUPPORTED
TAG { ... }
TAG1, TAG2, ... { ... }
TAG.class { ... }
.class { ...)
TAG:pseudo { ... }
CSS definitions may be freely formatted (spaces, tabs, linefeeds...),
they are converted to oneliners before inserting them inline in the html tags.
.class definitions are processed AFTER tag definitions,
thus appended inline after any existing tag styling to
preserve the normal css priority behavior.
Existing style="..." attributes in tags are NOT stripped. However they MUST
be with double quotes. If not, an addtional style="..." attribute will be added
KNOWN LIMITATIONS
- style info should be placed in <head> section. I believe
it shouldnt be too hard to modify to point to an external
stylesheet instead.
- no support (yet?):
* chains like P UL LI { .... } or P UL LI.class { .... }
* #divname p { ... } and <tag id="...">
* a:hover, a:visited {...} multiple class:pseudo
They require a significantly more complicated processing likely
based on stylesheet and document trees parsing.
Many email clients don't handle more than what is supported
by this script anyway.
- pseudo-classes like a:hover {...} can't be inserted inline
in the html tags: they are moved to a <style> declaration in
the <body> instead. This is a limitation from html, not this script.
- It is still up to you to check if target email clients render
your css styled templates correctly, especially webmail services
like Hotmail, in which the email becomes a sub-part of an html page,
with styles already in place.
*/
function facteur_convertir_styles_inline($body){
// variables to be accessed in the callback sub-function too
global $styledefinition, $styletag, $styleclass;
// Let's first load the stylesheet information in a $styles array using a regexp
preg_match_all ( "/^[ \t]*([.]?)([\w, #]+)([.:])?(\S*)\s+{([^}]+)}/mi", $body , $styles);
/*
$styles[1] = . or '' => .class or tag (empty)
$styles[2] = name of class or tag(s)
$styles[3] = : . or '' => followed by pseudo-element, class separator or nothing (empty)
$styles[4] = name of pseudo-element after a tag, if any
$styles[5] = the style definition itself, i.e. what's between the { }
*/
// Now loop through the styles found and act accordingly;
// process TAG {...} & TAG1, TAG2,... {...} definitions only first by order of appearance
foreach ($styles[1] as $i => $type) {
if ($type=="" && $styles[3][$i]=="") {
$styledefinition = trim($styles[5][$i]);
$styletag = preg_replace("/ *, */", "|", trim($styles[2][$i])); //echo $styletag."<br />";
$styleclass = "";
// process TAG {...} and TAG1, TAG2 {...} but not TAG1 TAG2 {...} or #divname styles
if (!preg_match("/ /", $styletag) && !preg_match("/#/", $styletag)) {
$pattern = "!<(".$styletag.")([^>]*(?= /)|[^>]*)( /)?>!mi";
$body = preg_replace_callback ($pattern, 'facteur_addstyle' , $body);
$styles[6][$i]=1; // mark as injected inline
}
}
}
// append additional .CLASS {...} and TAG.CLASS {...} styling by order of appearance
// important to do so after TAG {...} definitions, so that class attributes override TAG styles when needed
foreach ($styles[1] as $i => $type) {
if ($type!="." && $styles[3][$i]=="." ) { // class definition for a specific tag
$styledefinition = trim($styles[5][$i]);
$styletag = trim($styles[2][$i]);
$styleclass = trim($styles[4][$i]);
$pattern = "!<(".$styletag.")([^>]* class\=['\"][^'\"]*".$styleclass."[^'\"]*['\"][^>]*(?= /)|[^>]* class\=['\"][^'\"]*".$styleclass."[^'\"]*['\"][^>]*)( />)?>!mi";
$body = preg_replace_callback ($pattern, 'facteur_addstyle' , $body);
$styles[6][$i]=1; // mark as injected inline
}
elseif ($type=="." && $styles[3][$i]=="" ) { // general class definition for any tag
$styledefinition = trim($styles[5][$i]);
$styletag = "";
$styleclass = trim($styles[2][$i]);
$pattern = "!<(\w+)([^>]* class\=['\"]".$styleclass."['\"][^>]*(?= /)|[^>]* class\=['\"]".$styleclass."['\"][^>]*)( />)?>!mi";
$body = preg_replace_callback ($pattern, 'facteur_addstyle' , $body);
$styles[6][$i]=1; // mark as injected inline
}
}
/* move all style declarations that weren't injected from <head> to a <body> <style> section,
including but not limited to:
- pseudo-classes like a:hover {...} as they can't be set inline
- declaration chains like UL LI {...}
- #divname {...}. These are not supported by email clients like Mac/Entourage anyway, it seems. */
foreach ($styles[1] as $i => $type) {
if ($styles[6][$i]=="") {
// add a <style type="text/css"> section after <body> if there's isn't one yet
if (preg_match ("!<body[^>]*>\s*<style!mi", $body)==0) {
$body = preg_replace ("/(<body[^>]*>)/i", "\n\$1\n".'<style type="text/css">'."\n<!--\n-->\n</style>\n", $body);
}
// append a copy of the pseudo-element declaration to that body style section
$styledefinition = trim($styles[5][$i]);
$styledefinition = preg_replace ("!\s+!mi", " ", $styledefinition ); // convert style definition to a one-liner (optional)
$declaration = $styles[1][$i].trim($styles[2][$i]).$styles[3][$i].trim($styles[4][$i])." { ".$styledefinition." }";
$body = preg_replace ("!(<body[^>]*>\s*<style[^>]*>\s*<\!\-\-[^>]*)"."(\s*\-\->\s*</style>)!si", "\$1".$declaration."\n\$2", $body);
$styles[6][$i]= 2; // mark as moved to <style> section in <body>
}
}
// remove stylesheet declaration(s) from <head> section (comment following line out if not wanted)
//$body = preg_replace ("!(<head>.*)<style type.*</style>(.*</head>)!si", "\$1\$2" , $body);
// check what styles have been injected
# print_r($styles);
return $body;
}
/**
* facteur_addstyle
* @author Eric Dols

5
formulaires/configurer_facteur.html

@ -118,17 +118,12 @@
<legend><:facteur:facteur_filtres:></legend>
<ul>
<li class="editer_facteur_filtres[ (#ENV**{erreurs}|table_valeur{facteur_filtres}|oui)erreur]">
<label><:facteur:facteur_smtp_secure:></label>
[<span class='erreur_message'>(#ENV**{erreurs}|table_valeur{facteur_smtp_secure})</span>]
<p class="explication"><:facteur:facteur_filtres_descriptif:></p>
<div class="choix">
<input type="checkbox" name="facteur_filtre_images" class="checkbox" value="1" id="facteur_filtre_images"[(#ENV{facteur_filtre_images,''}|=={1}|oui)checked="checked"]/>
<label for="facteur_filtre_images"><:facteur:facteur_filtre_images:></label>
</div>
<div class="choix">
<input type="checkbox" name="facteur_filtre_css" class="checkbox" value="1" id="facteur_filtre_css"[(#ENV{facteur_filtre_css,''}|=={1}|oui)checked="checked"]/>
<label for="facteur_filtre_css"><:facteur:facteur_filtre_css:></label>
</div>
<div class="choix">
<input type="checkbox" name="facteur_filtre_iso_8859" class="checkbox" value="1" id="facteur_filtre_iso_8859"[(#ENV{facteur_filtre_iso_8859,''}|=={1}|oui)checked="checked"]/>
<label for="facteur_filtre_iso_8859"><:facteur:facteur_filtre_iso_8859:></label>

2
formulaires/configurer_facteur.php

@ -22,7 +22,6 @@ function formulaires_configurer_facteur_charger_dist(){
'facteur_smtp_secure' => $GLOBALS['meta']['facteur_smtp_secure'],
'facteur_smtp_sender' => $GLOBALS['meta']['facteur_smtp_sender'],
'facteur_filtre_images' => $GLOBALS['meta']['facteur_filtre_images'],
'facteur_filtre_css' => $GLOBALS['meta']['facteur_filtre_css'],
'facteur_filtre_iso_8859' => $GLOBALS['meta']['facteur_filtre_iso_8859'],
'_enable_smtp_secure' => (intval(phpversion()) == 5)?' ':'',
'facteur_cc' => $GLOBALS['meta']['facteur_cc'],
@ -125,7 +124,6 @@ function formulaires_configurer_facteur_traiter_dist(){
ecrire_meta('facteur_smtp_sender', $facteur_smtp_sender);
ecrire_meta('facteur_filtre_images', intval(_request('facteur_filtre_images')));
ecrire_meta('facteur_filtre_css', intval(_request('facteur_filtre_css')));
ecrire_meta('facteur_filtre_iso_8859', intval(_request('facteur_filtre_iso_8859')));
$facteur_cc = _request('facteur_cc');

2
paquet.xml

@ -1,7 +1,7 @@
<paquet
prefix="facteur"
categorie="communication"
version="2.1.10"
version="2.2.0"
etat="stable"
compatibilite="[3.0.0;3.0.*]"
logo="prive/themes/spip/images/facteur-32.png"

Loading…
Cancel
Save