Randomly broken html content
Posted: Tue May 22, 2007 11:16 am
I have a weird problem with swiftmailer. When I batchsend a mail, sometimes, it seems that the decorator plugin screw up the html content. The code stop with : swift-11337913384653146cdd13e9.42341019_
The alt/text part of the mail is correct, seems logical cause this part do not go in the decorator.
I found that if I send exactly the same content a second time to my recipient list, the mail is good... Maybe a cache problem, it's weird. Here is the code, if someone has an idea...
Note that the file I created when I send the mail is correct, so the problem really seems to come from the decorator plugin...
The alt/text part of the mail is correct, seems logical cause this part do not go in the decorator.
I found that if I send exactly the same content a second time to my recipient list, the mail is good... Maybe a cache problem, it's weird. Here is the code, if someone has an idea...
Code: Select all
require_once('../Class/Courriel.php');
require_once('../Class/CourrielCollection.php');
require_once('../Class/Template.php');
require_once('../Class/LongDate.php');
//Instanciation de l'objet swift pour envoyer les mails
require_once('../Class/Swift.php');
require_once('../Class/Swift/Plugin/Decorator.php');
require_once('../Class/Swift/Plugin/AntiFlood.php');
//require_once('../Class/Swift/Plugin/Throttler.php');
//$throttler =& new Swift_Plugin_Throttler();
//$throttler->setEmailsPerMinute(120); //Max of 2 email every 1 seconds
require_once ('../Class/Swift/Plugin/BandwidthMonitor.php');
//require_once "../Class/Swift/Plugin/VerboseSending.php";
if ($objconf->GetSmtp_mode() == 1)
{
require_once('../Class/Swift/Connection/SMTP.php');
$swift =& new Swift(new Swift_Connection_SMTP($objconf->GetSmtp_host(),$objconf->GetSmtp_port()));
}
else
{
require_once('../Class/Swift/Connection/NativeMail.php');
$swift =& new Swift(new Swift_Connection_NativeMail());
}
//Création du plugin de monitorage de bande passante
$bandwidth =& new Swift_Plugin_BandwidthMonitor();
//Activation des logs pour afficher les problèmes sur certaines adresses.
$swift->log->enable();
//Création du message
$message =& new Swift_Message(htmlspecialchars($_POST['sujet']));
//Création des parties du message multipart
//Partie text ->
$text_part = str_replace('</p>','\n',$_POST['fr_texte']);
$text_part = strip_tags($text_part);
//Création de la date au format long...
$date = new LongDate(date("Y-m-d"));
//Partie html ->
//Remplissage du template
$template = new template('Modules/Mail/Templates/'.$objconf->GetTemplate());
//Travail sur les décorations header et footer
if ($objconf->GetDecorationStatut())
{
if ($_POST['imageselect'] != '')
{
$arimg = explode(';',$_POST['imageselect']);
$headerimg = $arimg[0];
$footerimg = $arimg[1];
$header = $objconf->GetBase_url().'/uploads/mail/headfoot/'.$headerimg;
$footer = $objconf->GetBase_url().'/uploads/mail/headfoot/'.$footerimg;
}
}
//Récupére le texte du footer sélectionné
if ($objconf->GetFooterTextstatut())
{
$pied = new MailFooter($_POST['footerselect']);
$footer_texte_fr = $pied->GetTexte('fr');
}
//Création du nom de fichier en prévision de l'archivage
$nomfichier = 'b'.date("dmYGis").'.html';
//Remplace titre, header, footer et texte dans le template
$template->assign('TITRE',$_POST['sujet']);
$template->assign('DATE',$date->GetLongDate());
$template->assign('TEXTE',$_POST['fr_texte']);
if ($objconf->GetDecorationStatut())
{
$template->assign('IMG_HEADER',$header);
$template->assign('IMG_FOOTER',$footer);
}
if ($objconf->GetFooterTextstatut())$template->assign('FOOTER',$footer_texte_fr);
$template->assign('FICHIER','Si vous avez des problèmes pour visualiser ce message, <a href="'.$objconf->GetBase_url().'/uploads/mail/arch/'.$nomfichier.'">cliquez ici</a> pour le voir.');
//On récupère le template dans une variable
$html_part = $template->__toString();
//Enregistre le contenu dans un fichier html pour visualisation future
if($fp = fopen('../uploads/mail/arch/'.$nomfichier,"w"))
{ /* ouverture du fichier en écriture */
fputs($fp, "\n"); // on va a la ligne
fputs($fp, "$html_part"); // on écrit le nom et email dans le fichier
fclose($fp);
}
else
{ // L'ouverture a échouée
echo "Ouverture du fichier impossible";
exit();
}
//On attache les partie texte brut et html au message
$message->attach(new Swift_Message_Part($text_part));
$message->attach(new Swift_Message_Part($html_part, "text/html"));
//Création de la liste d'envoie
$recipients =& new Swift_RecipientList();
//Création d'un array pour remplacer COURRIEL et URL_UNSUSCRIBE dans le corps
$arReplace = array();
//Gestion du champ courriel
if ($_POST['courriel'] != '')
{
$arcourriel = explode(';',$_POST['courriel']);
for ($i=0;$i<sizeof($arcourriel);$i++)
{
$recipients->addTo($arcourriel[$i]);
$arTemp = array($arcourriel[$i] => array("{COURRIEL}" => $arcourriel[$i]));
$arReplace = array_merge($arReplace,$arTemp);
}
}
//Gestion des groupes
if (isset($_POST['groupes'])){$check = $check + 1;}
//Gestion des langues
if (isset($_POST['langues'])){$check = $check + 2;}
//Récupération des courriel en fonction de ce qui est coché et ajout à la liste d'envoie
switch ($check) {
case 1:$arobjcourriel = CourrielCollection::GetAllbyGroupe($_POST['groupes']);break;
case 2:$arobjcourriel = CourrielCollection::GetAllbyLang($_POST['langues']);break;
case 3:$arobjcourriel = CourrielCollection::GetAllbyGroupeLang($_POST['groupes'],$_POST['langues']);break;
}
if (!empty($arobjcourriel))
{
foreach ($arobjcourriel as $courriel)
{
$recipients->addTo($courriel->GetCourriel(),$courriel->GetPrenom()." ".$courriel->GetNom());
$arTemp = array($courriel->GetCourriel() => array("{COURRIEL}" => $courriel->GetId()));
$arReplace = array_merge($arReplace,$arTemp);
}
}
//Ajout d'un return_path
$message->setReturnPath($objconf->GetReturn_path());
//Plugin antiflood pour eviter le débordement du serveur
//Reconnect after 100 emails, but wait for 10 seconds first
$swift->attachPlugin(new Swift_Plugin_AntiFlood(100, 10), "anti-flood");
//Plugin pour la bandepassante
$swift->attachPlugin($bandwidth, "bwmon");
//Load the plugin with these replacements
$swift->attachPlugin(new Swift_Plugin_Decorator($arReplace), "decorator");
//Envoie des courriels et retour du nombre envoyé
$number_sent = $swift->batchSend($message, $recipients, new Swift_Address($objconf->GetFrom_email(), $objconf->GetFrom_name()));
$courriel_erreur = implode(" ,", $swift->log->getFailedRecipients());
$fr_mess_erreur .= 'Nombre de courriels envoyés : '.$number_sent.'<br/>';
$fr_mess_erreur .= 'Octets envoyés : ' . $bandwidth->getBytesOut() . '<br />';
$fr_mess_erreur .= 'Octets reçus : ' . $bandwidth->getBytesIn() . '<br />';
$swift->log->dump();
if ($courriel_erreur != ''){$fr_mess_erreur .= 'Courriel qui n\'ont pas fonctionnés'.$courriel_erreur.'<br/>';}
unset($recipients);unset($arReplace);