Fix: Make code so much simpler and solve a lot of problem with new version.

This commit is contained in:
Laurent Destailleur 2009-05-13 14:49:30 +00:00
parent a123706521
commit 02a0ee7d9e
2 changed files with 188 additions and 339 deletions

View File

@ -407,9 +407,6 @@ class SMTPs
*/ */
var $_smtpsBoundary = null; var $_smtpsBoundary = null;
// DOL_CHANGE LDR
var $_smtpsRelatedBoundary = null;
/** /**
* Property private int var $_transportType * Property private int var $_transportType
* *
@ -1942,10 +1939,6 @@ class SMTPs
// Generate a new Boundary string // Generate a new Boundary string
$this->_setBoundary(); $this->_setBoundary();
// DOL_CHANGE LDR
// Generate a new Related Boundary string
$this->_setRelatedBoundary();
// What type[s] of content do we have // What type[s] of content do we have
$_types = array_keys ( $this->_msgContent ); $_types = array_keys ( $this->_msgContent );
@ -1977,40 +1970,6 @@ class SMTPs
// If we have more than ONE, we use the multi-part format // If we have more than ONE, we use the multi-part format
else if( $keyCount > 1 ) else if( $keyCount > 1 )
{ {
// DOL_CHANGE LDR
$image=0;
$attachment=0;
foreach ($_types as $type)
{
if ($type == 'image')
{
$content = 'Content-Type: multipart/related;' . "\r\n"
. ' boundary="' . $this->_getBoundary() . '"' . "\r\n"
. "\r\n"
. 'This is a multi-part message in MIME format.' . "\r\n";
$image=1;
}
else if ($type == 'attachment' && $image)
{
$content = 'Content-Type: multipart/mixed;' . "\r\n"
. ' boundary="' . $this->_getBoundary() . '"' . "\r\n"
. "\r\n"
. 'This is a multi-part message in MIME format.' . "\r\n"
. "\r\n--" . $this->_getBoundary() . "\r\n"
. 'Content-Type: multipart/related;' . "\r\n"
. ' boundary="' . $this->_getRelatedBoundary() . '"' . "\r\n";
$attachment=1;
}
else
{
$content = 'Content-Type: multipart/mixed;' . "\r\n"
. ' boundary="' . $this->_getBoundary() . '"' . "\r\n"
. "\r\n"
. 'This is a multi-part message in MIME format.' . "\r\n";
}
}
// END DOL_CHANGE LDR
// Since this is an actual multi-part message // Since this is an actual multi-part message
// We need to define a content message Boundary // We need to define a content message Boundary
// NOTE: This was 'multipart/alternative', but Windows based // NOTE: This was 'multipart/alternative', but Windows based
@ -2018,12 +1977,10 @@ class SMTPs
/* /*
* @TODO Investigate "nested" boundary message parts * @TODO Investigate "nested" boundary message parts
*/ */
// DOL_CHANGE LDR $content = 'Content-Type: multipart/mixed;' . "\r\n"
//$content = 'Content-Type: multipart/mixed;' . "\r\n" . ' boundary="' . $this->_getBoundary() . '"' . "\r\n"
// . ' boundary="' . $this->_getBoundary() . '"' . "\r\n" . "\r\n"
// . "\r\n" . 'This is a multi-part message in MIME format.' . "\r\n";
// . 'This is a multi-part message in MIME format.' . "\r\n";
// END DOL_CHANGE LDR
// Loop through message content array // Loop through message content array
foreach ($this->_msgContent as $type => $_content ) foreach ($this->_msgContent as $type => $_content )
@ -2047,57 +2004,17 @@ class SMTPs
. $_data['data'] . "\r\n"; . $_data['data'] . "\r\n";
} }
} }
// DOL_CHANGE LDR
else if ( $type == 'image' )
{
// loop through all images
foreach ( $_content as $_image => $_data )
{
if ($attachment && $image)
{
$content .= "\r\n--" . $this->_getRelatedBoundary() . "\r\n";
}
else else
{ {
$content .= "\r\n--" . $this->_getBoundary() . "\r\n"; $content .= "\r\n--" . $this->_getBoundary() . "\r\n"
} . 'Content-Type: ' . $_content['mimeType'] . '; '
$content .= 'Content-Type: ' . $_data['mimeType'] . '; name="' . $_data['imageName'] . '"' . "\r\n"
. 'Content-Transfer-Encoding: base64' . "\r\n"
. 'Content-Disposition: inline; filename="' . $_data['imageName'] . '"' . "\r\n"
. 'Content-ID: <' . $_data['cid'] . '> ' . "\r\n";
if ( $this->getMD5flag() )
$content .= 'Content-MD5: ' . $_data['md5'] . "\r\n";
$content .= "\r\n"
. $_data['data'] . "\r\n";
if ($attachment && $image) $content .= "\r\n--" . $this->_getRelatedBoundary() . '--' . "\r\n" ;
}
}
else
{
if ($attachment && $image)
{
$content .= "\r\n--" . $this->_getRelatedBoundary() . "\r\n";
}
else
{
$content .= "\r\n--" . $this->_getBoundary() . "\r\n";
}
//$content .= "\r\n--" . $this->_getBoundary() . "\r\n"
// . 'Content-Type: ' . $_content['mimeType'] . '; '
$content .= 'Content-Type: ' . $_content['mimeType'] . '; '
// END DOL_CHANGE LDR
. 'charset="' . $this->getCharSet() . '"'; . 'charset="' . $this->getCharSet() . '"';
//$content .= ( $type == 'html') ? '; name="HTML Part"' : ''; // DOL_CHANGE LDR $content .= ( $type == 'html') ? '; name="HTML Part"' : '';
$content .= "\r\n"; $content .= "\r\n";
$content .= 'Content-Transfer-Encoding: '; $content .= 'Content-Transfer-Encoding: ';
//$content .= ( $type == 'html') ? 'quoted-printable' : $this->getTransEncodeType(); // DOL_CHANGE LDR $content .= ( $type == 'html') ? 'quoted-printable' : $this->getTransEncodeType();
$content .= ( $type == 'html') ? '8bit' : $this->getTransEncodeType();
$content .= "\r\n" $content .= "\r\n"
// . 'Content-Disposition: inline' . "\r\n" // DOL_CHANGE LDR . 'Content-Disposition: inline' . "\r\n"
. 'Content-Description: ' . $type . ' message' . "\r\n"; . 'Content-Description: ' . $type . ' message' . "\r\n";
if ( $this->getMD5flag() ) if ( $this->getMD5flag() )
@ -2105,17 +2022,6 @@ class SMTPs
$content .= "\r\n" $content .= "\r\n"
. $_content['data'] . "\r\n"; . $_content['data'] . "\r\n";
// DOL_CHANGE LDR
// . "\r\n--" . $this->_getBoundary() . "\r\n";
if ($attachment && $image)
{
$content .= "\r\n--" . $this->_getRelatedBoundary() . "\r\n";
}
else
{
$content .= "\r\n--" . $this->_getBoundary() . "\r\n";
}
// END DOL_CHANGE LDR
} }
} }
@ -2160,35 +2066,6 @@ class SMTPs
} }
} }
// DOL_CHANGE LDR
/**
* Method public void setImage( string )
*
* Image attachments are added to the content array as sub-arrays,
* allowing for multiple images for each outbound email
*
* @param string $strContent Image data to attach to message
* @param string $strImageName Image Name to give to attachment
* @param string $strMimeType Image Mime Type of attachment
* @return void
*
*/
function setImage ( $strContent, $strImageName = 'unknown', $strMimeType = 'unknown', $strImageCid = 'unknown' )
{
if ( $strContent )
{
$this->_msgContent['image'][$strImageName]['mimeType'] = $strMimeType;
$this->_msgContent['image'][$strImageName]['imageName'] = $strImageName;
$this->_msgContent['image'][$strImageName]['cid'] = $strImageCid;
$this->_msgContent['image'][$strImageName]['data'] = $strContent;
if ( $this->getMD5flag() )
$this->_msgContent['image'][$strFileName]['md5'] = md5($strContent);
}
}
// END DOL_CHANGE LDR
/** /**
* Method public void setSensitivity( string ) * Method public void setSensitivity( string )
* *
@ -2422,12 +2299,6 @@ class SMTPs
$this->_smtpsBoundary = "multipart_x." . time() . ".x_boundary"; $this->_smtpsBoundary = "multipart_x." . time() . ".x_boundary";
} }
// DOL_CHANGE LDR
function _setRelatedBoundary()
{
$this->_smtpsRelatedBoundary = "multipart_x." . time() . ".x_related_boundary";
}
/** /**
* Method private string _getBoundary( void ) * Method private string _getBoundary( void )
* *
@ -2450,12 +2321,6 @@ class SMTPs
return $this->_smtpsBoundary; return $this->_smtpsBoundary;
} }
// DOL_CHANGE LDR
function _getRelatedBoundary()
{
return $this->_smtpsRelatedBoundary;
}
// This function has been modified as provided // This function has been modified as provided
// by SirSir to allow multiline responses when // by SirSir to allow multiline responses when
// using SMTP Extensions // using SMTP Extensions
@ -2570,22 +2435,8 @@ class SMTPs
/** /**
* $Log$ * $Log$
* Revision 1.6 2009/05/12 11:44:59 hregis * Revision 1.7 2009/05/13 14:49:30 eldy
* Add: possibilité d'envoyer un fichier attaché avec du html contenant des images avec * Fix: Make code so much simpler and solve a lot of problem with new version.
* la classe SMTPS
*
* Revision 1.5 2009/05/12 10:12:02 hregis
* Add: possibilité d'envoyer un fichier attaché avec du html contenant des images avec
* la classe SMTPS
* Fix: 'quoted-printable' truncated html code
*
* Revision 1.4 2009/05/12 08:39:40 hregis
* Add: possibilité d'envoyer un fichier attaché avec du html contenant des images avec
* la classe SMTPS
*
* Revision 1.3 2009/05/11 17:13:57 hregis
* Add: possibilité d'uploader une image et de l'envoyer dans un mailing (finalisé et fonctionnel)
* Add: modification classe smtps.php pour l'envoi d'images
* *
* Revision 1.2 2009/02/09 00:04:35 eldy * Revision 1.2 2009/02/09 00:04:35 eldy
* Added support for SMTPS protocol * Added support for SMTPS protocol

View File

@ -92,8 +92,8 @@ class CMailFile
// Evite caractere bizarre avec les accents // Evite caractere bizarre avec les accents
//Todo l'envoi par mailing donne des caractères bizarre, //Todo l'envoi par mailing donne des caractères bizarre,
// alors que l'envoi d'un document facture ou autre est correcte // alors que l'envoi d'un document facture ou autre est correcte
$subject = utf8_decode($subject); $subject = $subject;
$from = utf8_decode($from); $from = $from;
// If ending method not defined // If ending method not defined
if (empty($conf->global->MAIN_MAIL_SENDMODE)) $conf->global->MAIN_MAIL_SENDMODE='mail'; if (empty($conf->global->MAIN_MAIL_SENDMODE)) $conf->global->MAIN_MAIL_SENDMODE='mail';
@ -177,17 +177,17 @@ class CMailFile
$smtp_headers = $this->write_smtpheaders(); $smtp_headers = $this->write_smtpheaders();
// En-tete suite dans $mime_headers // En-tete suite dans $mime_headers
if ($this->atleastonefile || $this->atleastoneimage) // if ($this->atleastonefile || $this->atleastoneimage)
{ // {
$mime_headers = $this->write_mimeheaders($filename_list, $mimefilename_list); $mime_headers = $this->write_mimeheaders($filename_list, $mimefilename_list);
} // }
// On encode les images // On encode les images
if ($this->atleastoneimage) // if ($this->atleastoneimage)
{ // {
$images_encoded = $this->write_images($this->images_encoded); $images_encoded = $this->write_images($this->images_encoded);
$msg = $this->html; if (! empty($this->html)) $msg = $this->html;
} // }
// Corps message dans $text_body // Corps message dans $text_body
$text_body = $this->write_body($msg, $filename_list); $text_body = $this->write_body($msg, $filename_list);
@ -200,7 +200,9 @@ class CMailFile
// On defini $this->headers et $this->message // On defini $this->headers et $this->message
$this->headers = $smtp_headers . $mime_headers; $this->headers = $smtp_headers . $mime_headers;
$this->message = $text_body . $images_encoded . $text_encoded; $this->message = $text_body . $images_encoded . $text_encoded;
$this->message.= "--" . $this->mime_boundary . "--" . $this->eol;
// On nettoie le header pour qu'il ne se termine pas par un retour chariot. // On nettoie le header pour qu'il ne se termine pas par un retour chariot.
// Ceci evite aussi les lignes vides en fin qui peuvent etre interpretees // Ceci evite aussi les lignes vides en fin qui peuvent etre interpretees
@ -215,11 +217,18 @@ class CMailFile
require_once(DOL_DOCUMENT_ROOT."/includes/smtps/SMTPs.php"); require_once(DOL_DOCUMENT_ROOT."/includes/smtps/SMTPs.php");
$smtps = new SMTPs(); $smtps = new SMTPs();
$smtps->setCharSet($conf->file->character_set_client); $smtps->setCharSet($conf->file->character_set_client);
$smtps->setSubject($subject);
$smtps->setSubject($this->encodetorfc2822($subject));
$smtps->setTO($to); $smtps->setTO($to);
$smtps->setFrom($from); $smtps->setFrom($from);
if ($this->atleastoneimage) $msg = $this->html;
//if ($this->atleastoneimage) $msg = $this->html;
if (! empty($this->html))
{
$msg = $this->html;
$msg = $this->checkIfHTML($msg); $msg = $this->checkIfHTML($msg);
}
if ($this->msgishtml) $smtps->setBodyContent($msg,'html'); if ($this->msgishtml) $smtps->setBodyContent($msg,'html');
else $smtps->setBodyContent($msg,'plain'); else $smtps->setBodyContent($msg,'plain');
@ -318,7 +327,7 @@ class CMailFile
$bounce = $this->addr_from != '' ? "-f {$this->addr_from}" : ""; $bounce = $this->addr_from != '' ? "-f {$this->addr_from}" : "";
} }
$res = mail($dest,$this->subject,stripslashes($this->message),$this->headers, $bounce); $res = mail($dest,$this->encodetorfc2822($this->subject),stripslashes($this->message),$this->headers, $bounce);
if (! $res) if (! $res)
{ {
@ -400,6 +409,12 @@ class CMailFile
} }
// Encode subject according to RFC 2822 - http://en.wikipedia.org/wiki/MIME#Encoded-Word
function encodetorfc2822($stringtoencode)
{
global $conf;
return '=?'.$conf->file->character_set_client.'?B?'.base64_encode($stringtoencode).'?=';
}
/** /**
* \brief Permet d'encoder un fichier * \brief Permet d'encoder un fichier
@ -479,22 +494,23 @@ class CMailFile
$out.= "X-Mailer: Dolibarr version " . DOL_VERSION ." (using php mail)".$this->eol; $out.= "X-Mailer: Dolibarr version " . DOL_VERSION ." (using php mail)".$this->eol;
$out.= "MIME-Version: 1.0".$this->eol; $out.= "MIME-Version: 1.0".$this->eol;
if ($this->atleastoneimage) // if ($this->atleastoneimage)
{ // {
//if (! $this->atleastonefile)
$out.= "Content-Type: multipart/related; boundary=\"".$this->mime_boundary."\"".$this->eol;
$out.= "Content-Transfer-Encoding: 8bit".$this->eol; $out.= "Content-Transfer-Encoding: 8bit".$this->eol;
if (! $this->atleastonefile) $out.= "Content-Type: multipart/related; boundary=\"".$this->mime_boundary."\"".$this->eol; /* }
}
else if ($this->msgishtml) else if ($this->msgishtml)
{ {
$out.= "Content-Transfer-Encoding: 8bit".$this->eol;
if (! $this->atleastonefile) $out.= "Content-Type: text/html; boundary=\"".$this->mime_boundary."\"".$this->eol; if (! $this->atleastonefile) $out.= "Content-Type: text/html; boundary=\"".$this->mime_boundary."\"".$this->eol;
$out.= "Content-Transfer-Encoding: 8bit".$this->eol;
} }
else else
{ {
$out.= "Content-Transfer-Encoding: 8bit".$this->eol;
if (! $this->atleastonefile) $out.= "Content-Type: text/plain; boundary=\"".$this->mime_boundary."\"".$this->eol; if (! $this->atleastonefile) $out.= "Content-Type: text/plain; boundary=\"".$this->mime_boundary."\"".$this->eol;
$out.= "Content-Transfer-Encoding: 8bit".$this->eol;
} }
*/
dol_syslog("CMailFile::write_smtpheaders smtp_header=\n".$out, LOG_DEBUG); dol_syslog("CMailFile::write_smtpheaders smtp_header=\n".$out, LOG_DEBUG);
return $out; return $out;
} }
@ -516,20 +532,20 @@ class CMailFile
{ {
if ($filename_list[$i]) if ($filename_list[$i])
{ {
if (! $mimedone) //if (! $mimedone)
{ //{
$out.= "Content-Type: multipart/mixed; boundary=\"".$this->mime_boundary."\"".$this->eol; // $out.= "Content-Type: multipart/mixed; boundary=\"".$this->mime_boundary."\"".$this->eol;
$mimedone=1; // $mimedone=1;
} //}
if ($mimefilename_list[$i]) $filename_list[$i] = $mimefilename_list[$i]; if ($mimefilename_list[$i]) $filename_list[$i] = $mimefilename_list[$i];
$out.= "X-attachments: $filename_list[$i]".$this->eol; $out.= "X-attachments: $filename_list[$i]".$this->eol;
if ($mimedone!=2 && $this->atleastoneimage) //if ($mimedone!=2 && $this->atleastoneimage)
{ //{
$out.= "--" . $this->mime_boundary . $this->eol; // $out.= "--" . $this->mime_boundary . $this->eol;
$out.= "Content-Type: multipart/related; boundary=\"".$this->related_boundary."\"".$this->eol; // $out.= "Content-Type: multipart/related; boundary=\"".$this->related_boundary."\"".$this->eol;
$mimedone=2; // $mimedone=2;
} // }
} }
} }
} }
@ -550,18 +566,11 @@ class CMailFile
$out=''; $out='';
if ($this->atleastonefile || $this->atleastoneimage) // if ($this->atleastonefile || $this->atleastoneimage)
{ // {
if ($this->msgishtml) if ($this->msgishtml)
{
if ($this->atleastonefile && $this->atleastoneimage)
{
$out.= "--" . $this->related_boundary . $this->eol;
}
else
{ {
$out.= "--" . $this->mime_boundary . $this->eol; $out.= "--" . $this->mime_boundary . $this->eol;
}
$out.= "Content-Type: text/html; charset=".$conf->file->character_set_client.$this->eol; $out.= "Content-Type: text/html; charset=".$conf->file->character_set_client.$this->eol;
} }
else else
@ -570,13 +579,13 @@ class CMailFile
$out.= "Content-Type: text/plain; charset=".$conf->file->character_set_client.$this->eol; $out.= "Content-Type: text/plain; charset=".$conf->file->character_set_client.$this->eol;
} }
$out.= $this->eol; $out.= $this->eol;
} // }
if ($this->msgishtml) if ($this->msgishtml)
{ {
// Check if html header already in message // Check if html header already in message
$out.= $this->checkIfHTML($msgtext); $out.= $this->checkIfHTML($msgtext);
if ($this->atleastonefile || $this->atleastoneimage) /*if ($this->atleastonefile || $this->atleastoneimage)
{ {
if ($this->atleastonefile && $this->atleastoneimage) if ($this->atleastonefile && $this->atleastoneimage)
{ {
@ -586,15 +595,15 @@ class CMailFile
{ {
$out.= $this->eol . "--" . $this->mime_boundary . $this->eol; $out.= $this->eol . "--" . $this->mime_boundary . $this->eol;
} }
} }*/
} }
else else
{ {
$out.= $msgtext; $out.= $msgtext.$this->eol;
if ($this->atleastonefile) /* if ($this->atleastonefile || $this->atleastoneimage)
{ {
$out.= $this->eol . "--" . $this->mime_boundary . $this->eol; $out.= $this->eol . "--" . $this->mime_boundary . $this->eol;
} }*/
} }
return $out; return $out;
@ -610,9 +619,12 @@ class CMailFile
} }
else else
{ {
// $out.= $this->eol . "--" . $this->mime_boundary . $this->eol;
$out = $msg; $out = $msg;
} }
$out.=$this->eol;
return $out; return $out;
} }
@ -638,7 +650,7 @@ class CMailFile
if ($mimefilename_list[$i]) $filename_list[$i] = $mimefilename_list[$i]; if ($mimefilename_list[$i]) $filename_list[$i] = $mimefilename_list[$i];
if (! $mimetype_list[$i]) { $mimetype_list[$i] = "application/octet-stream"; } if (! $mimetype_list[$i]) { $mimetype_list[$i] = "application/octet-stream"; }
$out = $out . "--" . $this->mime_boundary . $this->eol; $out.= "--" . $this->mime_boundary . $this->eol;
$out.= "Content-Type: " . $mimetype_list[$i] . "; name=\"".$filename_list[$i]."\"".$this->eol; $out.= "Content-Type: " . $mimetype_list[$i] . "; name=\"".$filename_list[$i]."\"".$this->eol;
$out.= "Content-Transfer-Encoding: base64".$this->eol; $out.= "Content-Transfer-Encoding: base64".$this->eol;
$out.= "Content-Disposition: attachment; filename=\"".$filename_list[$i]."\"".$this->eol; $out.= "Content-Disposition: attachment; filename=\"".$filename_list[$i]."\"".$this->eol;
@ -655,7 +667,7 @@ class CMailFile
} }
// Fin de tous les attachements // Fin de tous les attachements
$out.= "--" . $this->mime_boundary . "--" . $this->eol; // $out.= "--" . $this->mime_boundary . "--" . $this->eol;
return $out; return $out;
} }
@ -820,14 +832,7 @@ class CMailFile
{ {
dol_syslog("CMailFile::write_images: i=$i"); dol_syslog("CMailFile::write_images: i=$i");
if (! $this->atleastonefile)
{
$out.= "--" . $this->mime_boundary . $this->eol; $out.= "--" . $this->mime_boundary . $this->eol;
}
else
{
$out.= "--" . $this->related_boundary . $this->eol;
}
$out.= "Content-Type: " . $img["content_type"] . "; name=\"".$img["name"]."\"".$this->eol; $out.= "Content-Type: " . $img["content_type"] . "; name=\"".$img["name"]."\"".$this->eol;
$out.= "Content-Transfer-Encoding: base64".$this->eol; $out.= "Content-Transfer-Encoding: base64".$this->eol;
$out.= "Content-Disposition: inline; filename=\"".$img["name"]."\"".$this->eol; $out.= "Content-Disposition: inline; filename=\"".$img["name"]."\"".$this->eol;
@ -837,24 +842,17 @@ class CMailFile
$out.= $this->eol; $out.= $this->eol;
} }
} }
else /* else
{ {
return 0; return 0;
} }
*/
// Fin de tous les attachements // Fin de tous les attachements
if (! $this->atleastonefile) // $out.= "--" . $this->mime_boundary . "--" . $this->eol;
{
$out.= "--" . $this->mime_boundary . "--" . $this->eol;
}
else
{
$out.= "--" . $this->related_boundary . "--" . $this->eol;
}
return $out; return $out;
} }
} }