2002-04-30 12:44:42 +02:00
< ? php
2005-06-25 15:02:39 +02:00
/* Copyright ( C ) 2000 - 2005 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
* Copyright ( C ) 2003 Jean - Louis Bergamo < jlb @ j1b . org >
2009-05-18 08:25:45 +02:00
* Copyright ( C ) 2004 - 2009 Laurent Destailleur < eldy @ users . sourceforge . net >
* Copyright ( C ) 2005 - 2009 Regis Houssin < regis @ dolibarr . fr >
2005-06-25 15:02:39 +02:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 2 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 59 Temple Place - Suite 330 , Boston , MA 02111 - 1307 , USA .
* or see http :// www . gnu . org /
*
* Lots of code inspired from Dan Potter ' s CMailFile class
*/
/**
2008-11-15 01:52:42 +01:00
* \file htdocs / lib / CMailFile . class . php
* \brief Fichier de la classe permettant d ' envoyer des mail avec attachements
* \version $Id $
* \author Dan Potter .
* \author Eric Seigne
* \author Laurent Destailleur .
*/
2004-07-16 00:17:39 +02:00
2005-06-25 15:02:39 +02:00
/**
2008-11-15 01:52:42 +01:00
* \class CMailFile
* \brief Classe d 'envoi de mails et pieces jointes. Encapsule mail() avec d' eventuels attachements .
* \remarks Usage : $mailfile = new CMailFile ( $subject , $sendto , $replyto , $message , $filepath , $mimetype , $filename , $cc , $ccc , $deliveryreceipt , $msgishtml , $errors_to );
* \remarks $mailfile -> sendfile ();
*/
2004-07-16 00:17:39 +02:00
class CMailFile
2003-07-07 12:04:20 +02:00
{
2008-11-15 01:52:42 +01:00
var $subject ;
var $addr_from ;
var $errors_to ;
var $addr_to ;
var $addr_cc ;
var $addr_bcc ;
var $mime_boundary ;
var $deliveryreceipt ;
var $eol ;
var $atleastonefile = 0 ;
var $error = '' ;
2009-02-09 01:04:34 +01:00
var $smtps ; // Contains SMTPs object (if this method is used)
2009-05-18 00:54:12 +02:00
2009-05-17 16:22:57 +02:00
// simplemail
2009-05-17 17:36:19 +02:00
var $simplemail ; // Contains simplemail object (if this method is used)
2009-05-17 16:22:57 +02:00
var $sName ;
var $sEmail ;
2009-05-19 17:40:25 +02:00
//CSS
var $css ;
var $styleCSS ;
2009-05-24 22:01:25 +02:00
2009-05-17 16:22:57 +02:00
// Image
2009-05-11 19:13:56 +02:00
var $html ;
var $image_boundary ;
var $atleastoneimage = 0 ;
var $html_images = array ();
var $images_encoded = array ();
var $image_types = array ( 'gif' => 'image/gif' ,
'jpg' => 'image/jpeg' ,
'jpeg' => 'image/jpeg' ,
'jpe' => 'image/jpeg' ,
'bmp' => 'image/bmp' ,
'png' => 'image/png' ,
'tif' => 'image/tiff' ,
'tiff' => 'image/tiff' );
2009-02-09 01:04:34 +01:00
2008-11-15 01:52:42 +01:00
/**
2009-02-09 01:04:34 +01:00
* \brief CMailFile
* \param subject Topic / Subject of mail
* \param to Recipients emails ( RFC 2822 : " Nom prenom <email>[, ...] " ou " email[, ...] " ou " <email>[, ...] " )
* \param from Sender email ( RFC 2822 : " Nom prenom <email>[, ...] " ou " email[, ...] " ou " <email>[, ...] " )
* \param msg Message
* \param filename_list List of files to attach ( full path of filename on file system )
* \param mimetype_list List of MIME type of attached files
* \param mimefilename_list List of attached file name in message
* \param addr_cc Email cc
* \param addr_bcc Email bcc
2009-05-19 17:40:25 +02:00
* \param deliveryreceipt Ask a delivery receipt
* \param msgishtml 1 = String IS already html , 0 = String IS NOT html , - 1 = Unknown need autodetection
* \param error_to Email errors
* \param css Css option
2008-11-15 01:52:42 +01:00
*/
function CMailFile ( $subject , $to , $from , $msg ,
2009-05-13 16:49:30 +02:00
$filename_list = array (), $mimetype_list = array (), $mimefilename_list = array (),
2009-05-19 17:40:25 +02:00
$addr_cc = " " , $addr_bcc = " " , $deliveryreceipt = 0 , $msgishtml = 0 , $errors_to = '' , $css = '' )
2008-11-15 01:52:42 +01:00
{
2009-02-09 01:04:34 +01:00
global $conf ;
2009-05-16 20:11:38 +02:00
2009-05-13 21:10:06 +02:00
// On definit fin de ligne
$this -> eol = " \n " ;
if ( eregi ( '^win' , PHP_OS )) $this -> eol = " \r \n " ;
if ( eregi ( '^mac' , PHP_OS )) $this -> eol = " \r " ;
2009-05-18 00:54:12 +02:00
2009-05-17 12:27:13 +02:00
// On defini mime_boundary
$this -> mime_boundary = md5 ( uniqid ( " dolibarr " ));
// On defini related_boundary
$this -> related_boundary = md5 ( uniqid ( " dolibarr " ));
// On defini alternative_boundary
$this -> alternative_boundary = md5 ( uniqid ( " dolibarr " ));
2009-05-13 21:10:06 +02:00
2009-02-09 01:04:34 +01:00
// If ending method not defined
if ( empty ( $conf -> global -> MAIN_MAIL_SENDMODE )) $conf -> global -> MAIN_MAIL_SENDMODE = 'mail' ;
2005-06-25 15:02:39 +02:00
2009-05-08 03:11:04 +02:00
dol_syslog ( " CMailFile::CMailfile: MAIN_MAIL_SENDMODE= " . $conf -> global -> MAIN_MAIL_SENDMODE . " charset= " . $conf -> file -> character_set_client . " from= $from , to= $to , addr_cc= $addr_cc , addr_bcc= $addr_bcc , errors_to= $errors_to " , LOG_DEBUG );
2009-02-20 23:53:15 +01:00
dol_syslog ( " CMailFile::CMailfile: subject= $subject , deliveryreceipt= $deliveryreceipt , msgishtml= $msgishtml " , LOG_DEBUG );
2006-08-06 20:34:37 +02:00
2008-07-17 01:37:34 +02:00
// Detect if message is HTML (use fast method)
2008-03-21 01:27:37 +01:00
if ( $msgishtml == - 1 )
2007-10-04 21:59:21 +02:00
{
$this -> msgishtml = 0 ;
2009-05-11 19:13:56 +02:00
if ( dol_textishtml ( $msg )) $this -> msgishtml = 1 ;
2007-10-04 21:59:21 +02:00
}
else
{
$this -> msgishtml = $msgishtml ;
}
2006-08-06 20:34:37 +02:00
2009-05-11 19:13:56 +02:00
// Detect images
if ( $this -> msgishtml )
{
$this -> html = $msg ;
$findimg = $this -> findHtmlImages ( $conf -> fckeditor -> dir_output );
// Define if there is at least one file
if ( $findimg )
{
foreach ( $this -> html_images as $i => $val )
{
if ( $this -> html_images [ $i ])
{
$this -> atleastoneimage = 1 ;
dol_syslog ( " CMailFile::CMailfile: html_images[ $i ]['name']= " . $this -> html_images [ $i ][ 'name' ], LOG_DEBUG );
}
}
}
}
2009-02-09 01:04:34 +01:00
// Define if there is at least one file
foreach ( $filename_list as $i => $val )
2008-11-15 01:52:42 +01:00
{
2009-02-09 01:04:34 +01:00
if ( $filename_list [ $i ])
{
$this -> atleastonefile = 1 ;
2009-02-20 23:53:15 +01:00
dol_syslog ( " CMailFile::CMailfile: filename_list[ $i ]= " . $filename_list [ $i ] . " , mimetype_list[ $i ]= " . $mimetype_list [ $i ] . " mimefilename_list[ $i ]= " . $mimefilename_list [ $i ], LOG_DEBUG );
2009-02-09 01:04:34 +01:00
}
2006-08-06 20:34:37 +02:00
}
2004-07-16 08:41:58 +02:00
2009-02-09 01:04:34 +01:00
// Action according to choosed sending method
if ( $conf -> global -> MAIN_MAIL_SENDMODE == 'mail' )
2008-11-15 01:52:42 +01:00
{
2009-02-09 01:04:34 +01:00
// Use mail php function (default PHP method)
// ------------------------------------------
$smtp_headers = " " ;
$mime_headers = " " ;
$text_body = " " ;
$text_encoded = " " ;
2009-05-18 00:54:12 +02:00
// Define smtp_headers
2009-05-11 18:26:54 +02:00
$this -> subject = $subject ;
2009-02-09 01:04:34 +01:00
$this -> addr_from = $from ;
$this -> errors_to = $errors_to ;
$this -> addr_to = $to ;
$this -> addr_cc = $addr_cc ;
$this -> addr_bcc = $addr_bcc ;
$this -> deliveryreceipt = $deliveryreceipt ;
$smtp_headers = $this -> write_smtpheaders ();
2009-05-18 00:54:12 +02:00
// Define mime_headers
2009-05-13 16:49:30 +02:00
$mime_headers = $this -> write_mimeheaders ( $filename_list , $mimefilename_list );
if ( ! empty ( $this -> html )) $msg = $this -> html ;
2006-11-01 00:10:46 +01:00
2009-05-18 00:54:12 +02:00
// Define body in text_body
2009-05-13 21:10:06 +02:00
$text_body = $this -> write_body ( $msg );
2009-05-18 00:54:12 +02:00
// Encode images
2009-05-13 21:10:06 +02:00
if ( $this -> atleastoneimage )
{
$images_encoded = $this -> write_images ( $this -> images_encoded );
}
2008-07-17 01:37:34 +02:00
2009-05-18 00:54:12 +02:00
// Add attachments to text_encoded
2009-02-09 01:04:34 +01:00
if ( $this -> atleastonefile )
{
$text_encoded = $this -> write_files ( $filename_list , $mimetype_list , $mimefilename_list );
}
2008-11-15 01:52:42 +01:00
2009-05-18 00:54:12 +02:00
// We now define $this->headers et $this->message
2009-02-09 01:04:34 +01:00
$this -> headers = $smtp_headers . $mime_headers ;
2009-05-13 16:49:30 +02:00
2009-05-11 19:13:56 +02:00
$this -> message = $text_body . $images_encoded . $text_encoded ;
2009-05-13 16:49:30 +02:00
$this -> message .= " -- " . $this -> mime_boundary . " -- " . $this -> eol ;
2008-11-15 01:52:42 +01:00
2009-02-09 01:04:34 +01:00
// 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
// comme des injections mail par les serveurs de messagerie.
$this -> headers = eregi_replace ( " [ \r \n ]+ $ " , " " , $this -> headers );
}
2009-05-16 17:45:26 +02:00
else if ( $conf -> global -> MAIN_MAIL_SENDMODE == 'simplemail' )
{
2009-05-18 08:25:45 +02:00
// Use simplemail library
2009-05-16 17:45:26 +02:00
// ------------------------------------------
2009-05-18 00:54:12 +02:00
2009-05-17 12:19:55 +02:00
require_once ( DOL_DOCUMENT_ROOT . " /includes/simplemail/class.mail.php " );
2009-05-18 00:54:12 +02:00
2009-05-17 12:19:55 +02:00
$mail = new simplemail ();
2009-05-24 22:01:25 +02:00
$mail -> set_mode = 'php' ; // php or socket
2009-05-18 00:54:12 +02:00
2009-05-24 22:01:25 +02:00
// Boundaries
2009-05-17 12:19:55 +02:00
$mail -> B1B = $this -> mime_boundary ;
2009-05-17 12:27:13 +02:00
$mail -> B2B = $this -> related_boundary ;
2009-05-17 12:19:55 +02:00
$mail -> B3B = $this -> alternative_boundary ;
2009-05-18 00:54:12 +02:00
2009-05-17 12:19:55 +02:00
$mail -> XMailer = " Dolibarr version " . DOL_VERSION . " (using simplemail) " ;
2009-05-18 00:54:12 +02:00
2009-05-18 08:25:45 +02:00
// Add from
2009-05-17 17:36:19 +02:00
$this -> addr_from = $from ;
2009-05-18 01:41:33 +02:00
$mail -> hfrom = $this -> getValidAddress ( $this -> addr_from , 0 , 1 );
2009-05-18 00:54:12 +02:00
2009-05-18 08:25:45 +02:00
// Add delivery receipt
2009-05-17 18:00:30 +02:00
if ( $deliveryreceipt )
{
2009-05-18 01:41:33 +02:00
$mail -> deliveryreceipt = $this -> getValidAddress ( $this -> addr_from , 0 , 1 );
2009-05-17 18:00:30 +02:00
}
2009-05-18 00:54:12 +02:00
2009-05-18 08:25:45 +02:00
// Add to
2009-05-17 12:19:55 +02:00
$arrayTo = split ( ',' , $to );
foreach ( $arrayTo as $val )
{
2009-05-18 01:41:33 +02:00
$mail -> recipientlist [] = array ( 'mail' => $this -> getValidAddress ( $val , 2 ), 'nameplusmail' => $this -> getValidAddress ( $val , 0 , 1 ));
2009-05-17 12:19:55 +02:00
}
2009-05-17 17:36:19 +02:00
2009-05-18 08:25:45 +02:00
// Add carbon copy
2009-05-17 17:36:19 +02:00
if ( ! empty ( $addr_cc ))
2009-05-17 12:19:55 +02:00
{
2009-05-17 17:36:19 +02:00
$arrayTocc = split ( ',' , $addr_cc );
2009-05-17 16:22:57 +02:00
foreach ( $arrayTocc as $val )
2009-05-17 12:19:55 +02:00
{
2009-05-18 01:41:33 +02:00
if ( ! empty ( $mail -> hcc )) $mail -> hcc .= " , " ;
$mail -> hcc .= $this -> getValidAddress ( $val , 0 , 1 );
2009-05-17 12:19:55 +02:00
}
}
2009-05-18 00:54:12 +02:00
2009-05-18 08:25:45 +02:00
// Add carbon copy cache
2009-05-17 17:36:19 +02:00
if ( ! empty ( $addr_bcc ))
2009-05-17 12:19:55 +02:00
{
2009-05-17 17:36:19 +02:00
$arrayTobcc = split ( ',' , $addr_bcc );
foreach ( $arrayTobcc as $val )
2009-05-17 12:19:55 +02:00
{
2009-05-18 01:41:33 +02:00
if ( ! empty ( $mail -> hbcc )) $mail -> hbcc .= " , " ;
$mail -> hbcc .= $this -> getValidAddress ( $val , 0 , 1 );
2009-05-17 12:19:55 +02:00
}
}
2009-05-18 00:54:12 +02:00
2009-05-18 08:25:45 +02:00
// Add subject
2009-05-17 17:36:19 +02:00
$mail -> addsubject ( $this -> encodetorfc2822 ( $subject ));
2009-05-18 00:54:12 +02:00
2009-05-18 08:25:45 +02:00
// Add message
2009-05-17 12:19:55 +02:00
if ( $this -> msgishtml )
{
if ( ! empty ( $this -> html ))
2009-05-24 22:01:25 +02:00
{
2009-05-19 17:40:25 +02:00
if ( ! empty ( $css ))
{
$this -> css = $css ;
$this -> styleCSS = $this -> buildCSS ();
}
2009-05-24 22:01:25 +02:00
2009-05-17 12:19:55 +02:00
$msg = $this -> html ;
$msg = $this -> checkIfHTML ( $msg );
2009-05-18 00:54:12 +02:00
2009-05-17 12:19:55 +02:00
// un attachement html ( image jointe afficher ds le html ).
if ( $this -> atleastoneimage )
{
2009-05-17 17:36:19 +02:00
foreach ( $this -> html_images as $i => $val )
2009-05-17 12:19:55 +02:00
{
2009-05-17 17:36:19 +02:00
$mail -> addhtmlattachement ( $this -> html_images [ $i ][ 'fullpath' ], $this -> html_images [ $i ][ 'cid' ], $this -> html_images [ $i ][ 'content_type' ]);
2009-05-17 12:19:55 +02:00
}
}
}
2009-05-18 08:25:45 +02:00
// HTML format
2009-05-17 12:19:55 +02:00
$mail -> html = $msg ;
}
else
{
2009-05-18 08:25:45 +02:00
// Text format
2009-05-17 12:19:55 +02:00
$mail -> text = $msg ;
2009-05-17 10:01:54 +02:00
}
2009-05-18 00:54:12 +02:00
2009-05-18 08:25:45 +02:00
// Attach-files
2009-05-17 12:19:55 +02:00
if ( $this -> atleastonefile )
{
foreach ( $filename_list as $i => $val )
{
$mail -> addattachement ( $filename_list [ $i ]);
}
2009-05-17 10:01:54 +02:00
}
2009-05-17 17:36:19 +02:00
$this -> simplemail = $mail ;
2009-05-16 17:45:26 +02:00
}
2009-02-09 01:04:34 +01:00
else if ( $conf -> global -> MAIN_MAIL_SENDMODE == 'smtps' )
2008-11-15 01:52:42 +01:00
{
2009-02-09 01:04:34 +01:00
// Use SMTPS library
// ------------------------------------------
require_once ( DOL_DOCUMENT_ROOT . " /includes/smtps/SMTPs.php " );
$smtps = new SMTPs ();
2009-05-08 03:11:04 +02:00
$smtps -> setCharSet ( $conf -> file -> character_set_client );
2009-05-13 16:49:30 +02:00
$smtps -> setSubject ( $this -> encodetorfc2822 ( $subject ));
2009-05-18 00:54:12 +02:00
$smtps -> setTO ( $this -> getValidAddress ( $to , 2 ));
2009-02-09 01:04:34 +01:00
$smtps -> setFrom ( $from );
2009-05-13 16:49:30 +02:00
if ( ! empty ( $this -> html ))
{
2009-05-19 17:40:25 +02:00
if ( ! empty ( $css ))
{
$this -> css = $css ;
$this -> styleCSS = $this -> buildCSS ();
}
2009-05-13 16:49:30 +02:00
$msg = $this -> html ;
$msg = $this -> checkIfHTML ( $msg );
}
2009-02-09 01:04:34 +01:00
if ( $this -> msgishtml ) $smtps -> setBodyContent ( $msg , 'html' );
else $smtps -> setBodyContent ( $msg , 'plain' );
2009-05-11 19:13:56 +02:00
if ( $this -> atleastoneimage )
{
foreach ( $this -> images_encoded as $img )
{
2009-05-13 21:10:06 +02:00
$smtps -> setImageInline ( $img [ 'image_encoded' ], $img [ 'name' ], $img [ 'content_type' ], $img [ 'cid' ]);
2009-05-11 19:13:56 +02:00
}
}
2009-05-13 16:49:30 +02:00
2009-02-09 01:04:34 +01:00
if ( $this -> atleastonefile )
{
foreach ( $filename_list as $i => $val )
{
$content = file_get_contents ( $filename_list [ $i ]);
$smtps -> setAttachment ( $content , $mimefilename_list [ $i ], $mimetype_list [ $i ]);
}
}
2009-05-24 23:43:33 +02:00
$smtps -> setCC ( $addr_cc );
$smtps -> setBCC ( $addr_bcc );
2009-02-09 01:04:34 +01:00
$smtps -> setErrorsTo ( $errors_to );
$smtps -> setDeliveryReceipt ( $deliveryreceipt );
$this -> smtps = $smtps ;
2008-11-15 01:52:42 +01:00
}
else
{
2009-02-09 01:04:34 +01:00
// Send mail method not correctly defined
// --------------------------------------
return 'Bad value for MAIN_MAIL_SENDMODE constant' ;
2008-11-15 01:52:42 +01:00
}
2009-02-09 01:04:34 +01:00
2008-11-15 01:52:42 +01:00
}
2009-02-09 01:04:34 +01:00
2008-11-15 01:52:42 +01:00
/**
2009-02-09 01:04:34 +01:00
* \brief Send mail that was prepared by constructor
* \return boolean True if mail sent , false otherwise
2008-11-15 01:52:42 +01:00
*/
2007-06-07 23:24:01 +02:00
function sendfile ()
{
global $conf ;
2008-11-15 01:52:42 +01:00
2007-06-07 23:24:01 +02:00
$errorlevel = error_reporting ();
2007-11-28 23:42:48 +01:00
error_reporting ( $errorlevel ^ E_WARNING ); // Desactive warnings
2006-08-06 20:34:37 +02:00
2007-06-07 23:24:01 +02:00
$res = false ;
2008-11-15 01:52:42 +01:00
2009-02-09 01:04:34 +01:00
if ( empty ( $conf -> global -> MAIN_DISABLE_ALL_MAILS ))
2007-06-07 23:24:01 +02:00
{
2009-02-09 01:04:34 +01:00
// Action according to choosed sending method
2009-05-24 22:01:25 +02:00
if ( $conf -> global -> MAIN_MAIL_SENDMODE == 'mail' )
2007-06-07 23:24:01 +02:00
{
2009-02-09 01:04:34 +01:00
// Use mail php function (default PHP method)
// ------------------------------------------
2009-02-20 23:53:15 +01:00
dol_syslog ( " CMailFile::sendfile addr_to= " . $this -> addr_to . " , subject= " . $this -> subject , LOG_DEBUG );
dol_syslog ( " CMailFile::sendfile header= \n " . $this -> headers , LOG_DEBUG );
//dol_syslog("CMailFile::sendfile message=\n".$message);
2009-02-09 01:04:34 +01:00
2009-05-18 00:54:12 +02:00
// If Windows, sendmail_from must be defined
2009-02-09 01:04:34 +01:00
if ( isset ( $_SERVER [ " WINDIR " ]))
{
if ( empty ( $this -> addr_from )) $this -> addr_from = 'robot@mydomain.com' ;
2009-05-18 00:54:12 +02:00
@ ini_set ( 'sendmail_from' , $this -> getValidAddress ( $this -> addr_from , 2 ));
2009-02-09 01:04:34 +01:00
}
// Forcage parametres
if ( ! empty ( $conf -> global -> MAIN_MAIL_SMTP_SERVER )) ini_set ( 'SMTP' , $conf -> global -> MAIN_MAIL_SMTP_SERVER );
if ( ! empty ( $conf -> global -> MAIN_MAIL_SMTP_PORT )) ini_set ( 'smtp_port' , $conf -> global -> MAIN_MAIL_SMTP_PORT );
2009-05-24 23:30:37 +02:00
$dest = $this -> getValidAddress ( $this -> addr_to , 2 );
if ( ! $dest )
2009-02-09 01:04:34 +01:00
{
$this -> error = " Failed to send mail to SMTP= " . ini_get ( 'SMTP' ) . " , PORT= " . ini_get ( 'smtp_port' ) . " <br>Recipient address ' $dest ' invalid " ;
2009-05-24 22:01:25 +02:00
dol_syslog ( " CMailFile::sendfile: mail end error= " . $this -> error , LOG_ERROR );
2009-02-09 01:04:34 +01:00
}
else
2008-08-20 21:21:16 +02:00
{
2009-02-20 23:53:15 +01:00
dol_syslog ( " CMailFile::sendfile: mail start SMTP= " . ini_get ( 'SMTP' ) . " , PORT= " . ini_get ( 'smtp_port' ), LOG_DEBUG );
2009-02-09 01:04:34 +01:00
$bounce = '' ;
if ( $conf -> global -> MAIN_MAIL_ALLOW_SENDMAIL_F )
{
// le return-path dans les header ne fonctionne pas avec tous les MTA
// Le passage par -f est donc possible si la constante MAIN_MAIL_ALLOW_SENDMAIL_F est definie.
// La variable definie pose des pb avec certains sendmail securisee (option -f refusee car dangereuse)
$bounce = $this -> addr_from != '' ? " -f { $this -> addr_from } " : " " ;
}
2009-05-13 21:10:06 +02:00
$this -> message = stripslashes ( $this -> message );
if ( ! empty ( $conf -> global -> MAIN_MAIL_DEBUG )) $this -> dump_mail ();
2009-05-24 22:01:25 +02:00
$res = mail ( $dest , $this -> encodetorfc2822 ( $this -> subject ), $this -> message , $this -> headers , $bounce );
if ( ! $res )
2009-05-17 17:36:19 +02:00
{
2009-05-24 22:01:25 +02:00
$this -> error = " Failed to send mail to SMTP= " . ini_get ( 'SMTP' ) . " , PORT= " . ini_get ( 'smtp_port' ) . " <br>Check your server logs and your firewalls setup " ;
dol_syslog ( " CMailFile::sendfile: mail end error= " . $this -> error , LOG_ERROR );
2009-05-17 17:36:19 +02:00
}
else
{
2009-05-24 22:01:25 +02:00
dol_syslog ( " CMailFile::sendfile: mail end success " , LOG_DEBUG );
2009-05-18 00:54:12 +02:00
}
2009-05-24 22:01:25 +02:00
}
if ( isset ( $_SERVER [ " WINDIR " ]))
{
@ ini_restore ( 'sendmail_from' );
}
// Forcage parametres
if ( ! empty ( $conf -> global -> MAIN_MAIL_SMTP_SERVER )) ini_restore ( 'SMTP' );
if ( ! empty ( $conf -> global -> MAIN_MAIL_SMTP_PORT )) ini_restore ( 'smtp_port' );
}
else if ( $conf -> global -> MAIN_MAIL_SENDMODE == 'simplemail' )
{
// Use simplemmail function (Simplemail method)
// --------------------------------------------
dol_syslog ( " CMailFile::sendfile addr_to= " . $this -> addr_to . " , subject= " . $this -> subject , LOG_DEBUG );
dol_syslog ( " CMailFile::sendfile header= \n " . $this -> headers , LOG_DEBUG );
//dol_syslog("CMailFile::sendfile message=\n".$message);
// If Windows, sendmail_from must be defined
if ( isset ( $_SERVER [ " WINDIR " ]))
{
if ( empty ( $this -> addr_from )) $this -> addr_from = 'robot@mydomain.com' ;
@ ini_set ( 'sendmail_from' , $this -> getValidAddress ( $this -> addr_from , 2 ));
}
// Forcage parametres
if ( ! empty ( $conf -> global -> MAIN_MAIL_SMTP_SERVER )) ini_set ( 'SMTP' , $conf -> global -> MAIN_MAIL_SMTP_SERVER );
if ( ! empty ( $conf -> global -> MAIN_MAIL_SMTP_PORT )) ini_set ( 'smtp_port' , $conf -> global -> MAIN_MAIL_SMTP_PORT );
2009-05-24 23:30:37 +02:00
dol_syslog ( " CMailFile::sendfile: mail start SMTP= " . ini_get ( 'SMTP' ) . " , PORT= " . ini_get ( 'smtp_port' ), LOG_DEBUG );
2009-05-24 22:01:25 +02:00
2009-05-24 23:30:37 +02:00
$this -> message = stripslashes ( $this -> message );
2009-05-24 22:01:25 +02:00
2009-05-24 23:30:37 +02:00
if ( ! empty ( $conf -> global -> MAIN_MAIL_DEBUG )) $this -> dump_mail ();
2009-05-24 22:01:25 +02:00
2009-05-24 23:30:37 +02:00
$res = $this -> simplemail -> sendmail ();
2009-02-09 01:04:34 +01:00
2009-05-24 23:30:37 +02:00
if ( ! $res )
{
$this -> error = " Failed to send mail to SMTP= " . ini_get ( 'SMTP' ) . " , PORT= " . ini_get ( 'smtp_port' ) . " <br>Check your server logs and your firewalls setup " ;
dol_syslog ( " CMailFile::sendfile: mail end error " . $this -> error , LOG_ERR );
dol_syslog ( " CMailFile::sendfile: " . $this -> simplemail -> error_log , LOG_ERR );
}
else
{
dol_syslog ( " CMailFile::sendfile: mail end success " , LOG_DEBUG );
dol_syslog ( " CMailFile::sendfile: " . $this -> simplemail -> error_log , LOG_DEBUG );
2008-08-20 21:21:16 +02:00
}
2008-11-15 01:52:42 +01:00
2009-02-09 01:04:34 +01:00
if ( isset ( $_SERVER [ " WINDIR " ]))
{
@ ini_restore ( 'sendmail_from' );
}
// Forcage parametres
if ( ! empty ( $conf -> global -> MAIN_MAIL_SMTP_SERVER )) ini_restore ( 'SMTP' );
if ( ! empty ( $conf -> global -> MAIN_MAIL_SMTP_PORT )) ini_restore ( 'smtp_port' );
}
else if ( $conf -> global -> MAIN_MAIL_SENDMODE == 'smtps' )
{
// Use SMTPS library
// ------------------------------------------
$this -> smtps -> setTransportType ( 0 ); // Only this method is coded in SMTPs library
// Forcage parametres
if ( empty ( $conf -> global -> MAIN_MAIL_SMTP_SERVER )) $conf -> global -> MAIN_MAIL_SMTP_SERVER = ini_get ( 'SMTP' );
if ( empty ( $conf -> global -> MAIN_MAIL_SMTP_PORT )) $conf -> global -> MAIN_MAIL_SMTP_PORT = ini_get ( 'smtp_port' );
$this -> smtps -> setHost ( $conf -> global -> MAIN_MAIL_SMTP_SERVER );
2009-05-13 16:49:30 +02:00
$this -> smtps -> setPort ( $conf -> global -> MAIN_MAIL_SMTP_PORT ); //587 or 25;
2009-02-09 01:04:34 +01:00
2009-05-13 16:49:30 +02:00
if ( ! empty ( $conf -> global -> MAIN_MAIL_SMTPS_ID )) $this -> smtps -> setID ( $conf -> global -> MAIN_MAIL_SMTPS_ID );
if ( ! empty ( $conf -> global -> MAIN_MAIL_SMTPS_PW )) $this -> smtps -> setPW ( $conf -> global -> MAIN_MAIL_SMTPS_PW );
//$smtps->_msgReplyTo = 'reply@web.com';
2008-08-20 11:25:41 +02:00
2009-02-09 01:04:34 +01:00
$dest = $this -> smtps -> getFrom ( 'org' );
if ( ! $dest )
2007-06-07 23:24:01 +02:00
{
2009-02-09 01:04:34 +01:00
$this -> error = " Failed to send mail to SMTP= " . $conf -> global -> MAIN_MAIL_SMTP_SERVER . " , PORT= " . $conf -> global -> MAIN_MAIL_SMTP_PORT . " <br>Recipient address ' $dest ' invalid " ;
2009-05-24 23:30:37 +02:00
dol_syslog ( " CMailFile::sendfile: mail end error= " . $this -> error , LOG_ERR );
2007-06-07 23:24:01 +02:00
}
else
{
2009-05-13 16:49:30 +02:00
if ( ! empty ( $conf -> global -> MAIN_MAIL_DEBUG )) $this -> smtps -> setDebug ( true );
2009-02-09 01:04:34 +01:00
$result = $this -> smtps -> sendMsg ();
//print $resultvalue;
2007-06-07 23:24:01 +02:00
}
2009-02-09 01:04:34 +01:00
if ( ! empty ( $conf -> global -> MAIN_MAIL_DEBUG )) $this -> dump_mail ();
$result = $this -> smtps -> getErrors ();
if ( empty ( $this -> error ) && empty ( $result )) $res = true ;
else $res = false ;
2007-04-01 04:56:53 +02:00
}
2009-02-09 01:04:34 +01:00
else
2007-06-06 17:38:33 +02:00
{
2009-02-09 01:04:34 +01:00
// Send mail method not correctly defined
// --------------------------------------
return 'Bad value for MAIN_MAIL_SENDMODE constant' ;
2007-06-06 17:38:33 +02:00
}
2007-11-28 23:53:21 +01:00
2006-08-06 20:34:37 +02:00
}
else
{
2007-06-07 23:24:01 +02:00
$this -> error = 'No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS' ;
2009-03-12 23:49:05 +01:00
dol_syslog ( " CMailFile::sendfile: " . $this -> error , LOG_WARN );
2006-04-01 03:10:38 +02:00
}
2005-06-25 15:02:39 +02:00
2007-11-28 23:42:48 +01:00
error_reporting ( $errorlevel ); // Reactive niveau erreur origine
2005-06-25 15:02:39 +02:00
2007-06-07 23:24:01 +02:00
return $res ;
}
2005-06-25 15:02:39 +02:00
2006-08-06 20:34:37 +02:00
2009-05-13 16:49:30 +02:00
// 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 ) . '?=' ;
}
2009-02-09 01:04:34 +01:00
/**
* \brief Permet d ' encoder un fichier
* \param sourcefile
* \return < 0 si erreur , fichier encode si ok
*/
function _encode_file ( $sourcefile )
{
if ( is_readable ( $sourcefile ))
{
$fd = fopen ( $sourcefile , " r " );
$contents = fread ( $fd , filesize ( $sourcefile ));
$encoded = chunk_split ( base64_encode ( $contents ), 68 , $this -> eol );
fclose ( $fd );
return $encoded ;
}
else
{
$this -> error = " Error: Can't read file ' $sourcefile ' " ;
2009-02-20 23:53:15 +01:00
dol_syslog ( " CMailFile::encode_file: " . $this -> error , LOG_ERR );
2009-02-09 01:04:34 +01:00
return - 1 ;
}
}
2008-11-15 01:52:42 +01:00
/**
* \brief Ecrit le mail dans un fichier . Utilisation pour le debuggage .
*/
function dump_mail ()
{
2009-02-09 01:04:34 +01:00
global $conf , $dolibarr_main_data_root ;
2008-11-15 01:52:42 +01:00
if ( @ is_writeable ( $dolibarr_main_data_root )) // Avoid fatal error on fopen with open_basedir
{
2009-02-09 01:04:34 +01:00
$fp = fopen ( $dolibarr_main_data_root . " /dolibarr_mail.log " , " w " );
if ( $conf -> global -> MAIN_MAIL_SENDMODE == 'mail' )
{
fputs ( $fp , $this -> headers );
fputs ( $fp , $this -> eol ); // This eol is added by the mail function, so we add it in log
fputs ( $fp , $this -> message );
}
elseif ( $conf -> global -> MAIN_MAIL_SENDMODE == 'smtps' )
{
fputs ( $fp , $this -> smtps -> log );
}
2008-11-15 01:52:42 +01:00
fclose ( $fp );
if ( ! empty ( $conf -> global -> MAIN_UMASK ))
@ chmod ( $outputfile , octdec ( $conf -> global -> MAIN_UMASK ));
}
}
/**
2009-05-18 00:54:12 +02:00
* \brief Create SMTP headers
2008-11-15 01:52:42 +01:00
*/
function write_smtpheaders ()
{
2007-05-25 22:02:23 +02:00
global $conf ;
$out = " " ;
2006-08-06 20:34:37 +02:00
// Sender
2008-11-15 01:52:42 +01:00
//$out .= "X-Sender: ".getValidAddress($this->addr_from,2).$this->eol;
2009-05-18 00:54:12 +02:00
$out .= " From: " . $this -> getValidAddress ( $this -> addr_from , 0 , 1 ) . $this -> eol ;
$out .= " Return-Path: " . $this -> getValidAddress ( $this -> addr_from , 0 , 1 ) . $this -> eol ;
if ( isset ( $this -> reply_to ) && $this -> reply_to ) $out .= " Reply-To: " . $this -> getValidAddress ( $this -> reply_to , 2 ) . $this -> eol ;
if ( isset ( $this -> errors_to ) && $this -> errors_to ) $out .= " Errors-To: " . $this -> getValidAddress ( $this -> errors_to , 2 ) . $this -> eol ;
2006-08-06 20:34:37 +02:00
// Receiver
2009-05-18 00:54:12 +02:00
if ( isset ( $this -> addr_cc ) && $this -> addr_cc ) $out .= " Cc: " . $this -> getValidAddress ( $this -> addr_cc , 2 ) . $this -> eol ;
if ( isset ( $this -> addr_bcc ) && $this -> addr_bcc ) $out .= " Bcc: " . $this -> getValidAddress ( $this -> addr_bcc , 2 ) . $this -> eol ;
2008-11-15 01:52:42 +01:00
// Accuse reception
2009-05-18 00:54:12 +02:00
if ( isset ( $this -> deliveryreceipt ) && $this -> deliveryreceipt == 1 ) $out .= " Disposition-Notification-To: " . $this -> getValidAddress ( $this -> addr_from , 2 ) . $this -> eol ;
2008-11-15 01:52:42 +01:00
//$out .= "X-Priority: 3".$this->eol;
2009-05-11 19:13:56 +02:00
$out .= " X-Mailer: Dolibarr version " . DOL_VERSION . " (using php mail) " . $this -> eol ;
$out .= " MIME-Version: 1.0 " . $this -> eol ;
2006-08-06 20:34:37 +02:00
2009-05-13 16:49:30 +02:00
$out .= " Content-Type: multipart/related; boundary= \" " . $this -> mime_boundary . " \" " . $this -> eol ;
$out .= " Content-Transfer-Encoding: 8bit " . $this -> eol ;
2009-05-13 21:10:06 +02:00
$out .= $this -> eol ;
2009-05-18 00:54:12 +02:00
dol_syslog ( " CMailFile::write_smtpheaders smtp_header= \n " . $out );
2008-11-15 01:52:42 +01:00
return $out ;
}
2006-08-06 20:34:37 +02:00
2008-11-15 01:52:42 +01:00
/**
2009-05-18 00:54:12 +02:00
* \brief Creation header MIME
* \param filename_list
* \param mimefilename_list
2008-11-15 01:52:42 +01:00
*/
function write_mimeheaders ( $filename_list , $mimefilename_list )
{
2006-08-06 20:34:37 +02:00
$mimedone = 0 ;
2008-11-15 01:52:42 +01:00
$out = " " ;
2009-05-13 16:49:30 +02:00
2009-05-11 19:13:56 +02:00
if ( $filename_list )
2008-11-15 01:52:42 +01:00
{
2009-05-11 19:13:56 +02:00
for ( $i = 0 ; $i < count ( $filename_list ); $i ++ )
2008-11-15 01:52:42 +01:00
{
2009-05-11 19:13:56 +02:00
if ( $filename_list [ $i ])
2006-08-06 20:34:37 +02:00
{
2009-05-11 19:13:56 +02:00
if ( $mimefilename_list [ $i ]) $filename_list [ $i ] = $mimefilename_list [ $i ];
$out .= " X-attachments: $filename_list[$i] " . $this -> eol ;
2006-08-06 20:34:37 +02:00
}
2008-11-15 01:52:42 +01:00
}
}
2009-05-13 16:49:30 +02:00
2009-02-20 23:53:15 +01:00
dol_syslog ( " CMailFile::write_mimeheaders mime_header= \n " . $out , LOG_DEBUG );
2008-11-15 01:52:42 +01:00
return $out ;
}
/**
2009-05-13 21:10:06 +02:00
* \brief Permet d 'ecrire le corps du message (mode = ' mail ' )
2009-02-09 01:04:34 +01:00
* \param msgtext
2008-11-15 01:52:42 +01:00
*/
2009-05-13 21:10:06 +02:00
function write_body ( $msgtext )
2008-11-15 01:52:42 +01:00
{
global $conf ;
$out = '' ;
2009-05-13 16:49:30 +02:00
if ( $this -> msgishtml )
2008-11-15 01:52:42 +01:00
{
2009-05-13 16:49:30 +02:00
$out .= " -- " . $this -> mime_boundary . $this -> eol ;
2009-05-13 21:10:06 +02:00
$out .= " Content-Type: text/html; charset= \" " . $conf -> file -> character_set_client . " \" " . $this -> eol ;
2009-05-13 16:49:30 +02:00
}
else
{
$out .= " -- " . $this -> mime_boundary . $this -> eol ;
2009-05-13 21:10:06 +02:00
$out .= " Content-Type: text/plain; charset= \" " . $conf -> file -> character_set_client . " \" " . $this -> eol ;
2008-11-15 01:52:42 +01:00
}
2009-05-13 16:49:30 +02:00
$out .= $this -> eol ;
2009-05-19 17:40:25 +02:00
2008-11-15 01:52:42 +01:00
if ( $this -> msgishtml )
{
2006-08-06 20:34:37 +02:00
// Check if html header already in message
2009-05-13 21:10:06 +02:00
$strContent = $this -> checkIfHTML ( $msgtext );
2008-11-15 01:52:42 +01:00
}
else
{
2009-05-13 21:10:06 +02:00
$strContent .= $msgtext ;
2008-11-15 01:52:42 +01:00
}
2009-05-11 19:13:56 +02:00
2009-05-13 21:10:06 +02:00
// Make RFC821 Compliant, replace bare linefeeds
2009-05-19 17:40:25 +02:00
$strContent = preg_replace ( " /(?<! \r ) \n /si " , " \r \n " , $strContent );
2009-05-13 21:10:06 +02:00
2009-05-19 17:40:25 +02:00
$strContent = rtrim ( wordwrap ( $strContent ));
2009-05-13 21:10:06 +02:00
2009-05-19 17:40:25 +02:00
$out .= $strContent . $this -> eol ;
2009-05-13 21:10:06 +02:00
2008-11-15 01:52:42 +01:00
return $out ;
}
2009-05-13 16:49:30 +02:00
2009-05-13 21:10:06 +02:00
/**
* Correct an uncomplete html string
*
* @ param unknown_type $msg
* @ return unknown
*/
2009-05-19 17:40:25 +02:00
function checkIfHTML ( $msg )
2009-05-12 12:12:02 +02:00
{
if ( ! eregi ( '^[ \t]*<html' , $msg ))
{
2009-05-19 04:43:03 +02:00
$out = " <html><head><title></title> " ;
2009-05-19 17:40:25 +02:00
if ( ! empty ( $this -> styleCSS )) $out .= $this -> styleCSS ;
2009-05-19 04:43:03 +02:00
$out .= " </head><body> " ;
2009-05-12 12:12:02 +02:00
$out .= $msg ;
$out .= " </body></html> " ;
}
else
{
$out = $msg ;
}
return $out ;
}
2009-05-24 22:01:25 +02:00
2009-05-19 15:27:44 +02:00
/**
* Build a css style
*
* @ return css
*/
2009-05-19 17:40:25 +02:00
function buildCSS ()
2009-05-19 15:27:44 +02:00
{
2009-05-19 17:40:25 +02:00
$out = '' ;
2009-05-24 22:01:25 +02:00
2009-05-19 17:40:25 +02:00
if ( ! empty ( $this -> css ))
{
$out .= '<style type="text/css">' ;
$out .= 'body {' ;
2009-05-24 22:01:25 +02:00
2009-05-19 17:40:25 +02:00
if ( $this -> css [ 'bgcolor' ])
{
$out .= ' background-color: ' . $this -> css [ 'bgcolor' ] . ';' ;
}
if ( $this -> css [ 'bgimage' ])
{
// Todo: r<> cup<75> rer cid
$out .= ' background-image: url("cid:' . $this -> css [ 'bgimage_cid' ] . '");' ;
}
$out .= '}' ;
$out .= '</style>' ;
}
2009-05-24 22:01:25 +02:00
2009-05-19 15:27:44 +02:00
return $out ;
}
2008-11-15 01:52:42 +01:00
/**
\brief Permet d ' attacher un fichier
\param filename_list Tableau
\param mimetype_list Tableau
\param mimefilename_list Tableau
\return out Chaine fichiers encodes
*/
function write_files ( $filename_list , $mimetype_list , $mimefilename_list )
{
$out = '' ;
for ( $i = 0 ; $i < count ( $filename_list ); $i ++ )
{
if ( $filename_list [ $i ])
{
2009-02-20 23:53:15 +01:00
dol_syslog ( " CMailFile::write_files: i= $i " );
2008-11-15 01:52:42 +01:00
$encoded = $this -> _encode_file ( $filename_list [ $i ]);
if ( $encoded >= 0 )
{
if ( $mimefilename_list [ $i ]) $filename_list [ $i ] = $mimefilename_list [ $i ];
if ( ! $mimetype_list [ $i ]) { $mimetype_list [ $i ] = " application/octet-stream " ; }
2009-05-13 16:49:30 +02:00
$out .= " -- " . $this -> mime_boundary . $this -> eol ;
2008-11-15 01:52:42 +01:00
$out .= " Content-Type: " . $mimetype_list [ $i ] . " ; name= \" " . $filename_list [ $i ] . " \" " . $this -> eol ;
$out .= " Content-Transfer-Encoding: base64 " . $this -> eol ;
$out .= " Content-Disposition: attachment; filename= \" " . $filename_list [ $i ] . " \" " . $this -> eol ;
2009-05-13 21:10:06 +02:00
$out .= " Content-Description: \" " . " File Attachment " . " \" " . $this -> eol ;
2008-11-15 01:52:42 +01:00
$out .= $this -> eol ;
$out .= $encoded ;
$out .= $this -> eol ;
2009-05-11 19:13:56 +02:00
//$out.= $this->eol;
2008-11-15 01:52:42 +01:00
}
else
{
return $encoded ;
}
2006-08-06 20:34:37 +02:00
}
2008-11-15 01:52:42 +01:00
}
2005-06-25 15:02:39 +02:00
2009-05-13 21:10:06 +02:00
return $out ;
}
/**
\brief Permet d ' attacher une image
\param images_list Tableau
\return out Chaine images encodees
*/
function write_images ( $images_list )
{
$out = '' ;
if ( $images_list )
{
foreach ( $images_list as $img )
{
dol_syslog ( " CMailFile::write_images: i= $i " );
$out .= " -- " . $this -> mime_boundary . $this -> eol ;
$out .= " Content-Type: " . $img [ " content_type " ] . " ; name= \" " . $img [ " name " ] . " \" " . $this -> eol ;
$out .= " Content-Transfer-Encoding: base64 " . $this -> eol ;
$out .= " Content-Disposition: inline; filename= \" " . $img [ " name " ] . " \" " . $this -> eol ;
$out .= " Content-ID: < " . $img [ " cid " ] . " > " . $this -> eol ;
$out .= $this -> eol ;
$out .= $img [ " image_encoded " ];
$out .= $this -> eol ;
}
}
2009-05-11 19:13:56 +02:00
2008-11-15 01:52:42 +01:00
return $out ;
}
2008-04-17 01:11:45 +02:00
function check_server_port ( $host , $port )
{
$_retVal = 0 ;
if ( function_exists ( 'fsockopen' ))
{
//See if we can connect to the SMTP server
2008-11-15 01:52:42 +01:00
if ( $socket = @ fsockopen ( $host , // Host to 'hit', IP or domain
$port , // which Port number to use
$errno , // actual system level error
$errstr , // and any text that goes with the error
5 ) ) // timeout for reading/writing data over the socket
{
2008-04-17 01:11:45 +02:00
// Windows still does not have support for this timeout function
if ( function_exists ( 'socket_set_timeout' ))
2008-11-15 01:52:42 +01:00
socket_set_timeout ( $socket , 5 , 0 );
2008-04-17 01:11:45 +02:00
// Check response from Server
if ( $_retVal = $this -> server_parse ( $socket , " 220 " ) )
2008-11-15 01:52:42 +01:00
$_retVal = $socket ;
2008-04-17 01:11:45 +02:00
}
else
{
$this -> error = 'Error ' . $errno . ' - ' . $errstr ;
}
}
return $_retVal ;
}
// This function has been modified as provided
// by SirSir to allow multiline responses when
// using SMTP Extensions
//
function server_parse ( $socket , $response )
{
2008-11-15 01:52:42 +01:00
/**
* Default return value
*
* Returns constructed SELECT Object string or boolean upon failure
* Default value is set at TRUE
*
* @ var mixed $_retVal Indicates if Object was created or not
* @ access private
* @ static
*/
2008-04-17 01:11:45 +02:00
$_retVal = true ;
$server_response = '' ;
while ( substr ( $server_response , 3 , 1 ) != ' ' )
{
if ( ! ( $server_response = fgets ( $socket , 256 ) ) )
{
$this -> error = " Couldn't get mail server response codes " ;
$_retVal = false ;
}
}
if ( ! ( substr ( $server_response , 0 , 3 ) == $response ) )
{
$this -> error = " Ran into problems sending Mail. \r \n Response: $server_response " ;
$_retVal = false ;
}
return $_retVal ;
2008-11-15 01:52:42 +01:00
}
2009-05-13 16:49:30 +02:00
2009-05-11 19:13:56 +02:00
/**
\brief Recherche la presence d ' images dans le message html
\param images_dir Emplacement des images
\return int > 0 if OK , < 0 if KO
*/
function findHtmlImages ( $images_dir )
2009-05-13 16:49:30 +02:00
{
// Build the list of image extensions
$extensions = array_keys ( $this -> image_types );
preg_match_all ( '/(?:"|\')([^"\']+\.(' . implode ( '|' , $extensions ) . '))(?:"|\')/Ui' , $this -> html , $matches );
if ( $matches )
{
$i = 0 ;
foreach ( $matches [ 1 ] as $full )
{
eregi ( 'file=([A-Za-z0-9_\-\/]+[.]?[A-Za-z0-9]+)?$' , $full , $regs );
$img = $regs [ 1 ];
if ( file_exists ( $images_dir . '/' . $img ))
{
2009-05-17 12:19:55 +02:00
// Image path in src
2009-05-13 16:49:30 +02:00
$src = preg_quote ( $full );
2009-05-18 00:54:12 +02:00
2009-05-17 12:19:55 +02:00
// Image full path
$this -> html_images [ $i ][ " fullpath " ] = $images_dir . '/' . $img ;
2009-05-13 16:49:30 +02:00
// Image name
$this -> html_images [ $i ][ " name " ] = $img ;
// Content type
$ext = preg_replace ( '#^.*\.(\w{3,4})$#e' , 'strtolower("$1")' , $img );
$this -> html_images [ $i ][ " content_type " ] = $this -> image_types [ $ext ];
// cid
$this -> html_images [ $i ][ " cid " ] = md5 ( uniqid ( time ()));
$this -> html = preg_replace ( " #src= \" $src\ " | src = '$src' #", "src=\"cid:".$this->html_images[$i]["cid"]."\"", $this->html);
}
$i ++ ;
}
if ( ! empty ( $this -> html_images ))
{
2009-05-18 09:30:53 +02:00
$inline = array ();
2009-05-13 16:49:30 +02:00
$i = 0 ;
foreach ( $this -> html_images as $img )
{
2009-05-18 09:30:53 +02:00
$fullpath = $images_dir . '/' . $img [ " name " ];
2009-05-24 22:01:25 +02:00
2009-05-18 09:30:53 +02:00
// If duplicate images are embedded, they may show up as attachments, so remove them.
if ( ! in_array ( $fullpath , $inline ))
2009-05-13 16:49:30 +02:00
{
2009-05-18 09:30:53 +02:00
// Read image file
if ( $image = file_get_contents ( $fullpath ))
{
// On garde que le nom de l'image
eregi ( '([A-Za-z0-9_-]+[.]?[A-Za-z0-9]+)?$' , $img [ " name " ], $regs );
$imgName = $regs [ 1 ];
2009-05-24 22:01:25 +02:00
2009-05-18 09:30:53 +02:00
$this -> images_encoded [ $i ][ 'name' ] = $imgName ;
$this -> images_encoded [ $i ][ 'content_type' ] = $img [ " content_type " ];
$this -> images_encoded [ $i ][ 'cid' ] = $img [ " cid " ];
// Encodage de l'image
$this -> images_encoded [ $i ][ " image_encoded " ] = chunk_split ( base64_encode ( $image ), 68 , $this -> eol );
$inline [] = $fullpath ;
}
2009-05-13 16:49:30 +02:00
}
$i ++ ;
}
}
else
{
return - 1 ;
}
return 1 ;
}
else
{
return 0 ;
}
}
2009-05-18 00:54:12 +02:00
/**
2009-05-18 01:41:33 +02:00
* \brief Return an address for SMTP protocol
* \param adresses Example : 'John Doe <john@doe.com>' or 'john@doe.com'
* \param format 0 = Auto , 1 = emails with <> , 2 = emails without <>
* \param encode 1 = Encode name to RFC2822
* \return string If format 1 : '<john@doe.com>' or 'John Doe <john@doe.com>'
* If format 2 : 'john@doe.com'
2009-05-18 00:54:12 +02:00
*/
function getValidAddress ( $adresses , $format , $encode = '' )
{
global $conf ;
2008-11-15 01:52:42 +01:00
2009-05-18 00:54:12 +02:00
$ret = '' ;
2004-12-28 19:34:56 +01:00
2009-05-18 00:54:12 +02:00
$arrayaddress = split ( ',' , $adresses );
2008-11-15 01:52:42 +01:00
2009-05-18 00:54:12 +02:00
// Boucle sur chaque composant de l'adresse
foreach ( $arrayaddress as $val )
2006-08-06 20:34:37 +02:00
{
2009-05-18 00:54:12 +02:00
if ( eregi ( '^(.*)<(.*)>$' , trim ( $val ), $regs ))
2007-04-01 04:56:53 +02:00
{
2009-05-18 00:54:12 +02:00
$name = trim ( $regs [ 1 ]);
$email = trim ( $regs [ 2 ]);
2007-04-01 04:56:53 +02:00
}
2009-05-18 00:54:12 +02:00
else
2007-04-01 04:56:53 +02:00
{
2009-05-18 00:54:12 +02:00
$name = '' ;
$email = trim ( $val );
2007-04-01 04:56:53 +02:00
}
2009-05-18 00:54:12 +02:00
if ( $email )
2007-04-01 04:56:53 +02:00
{
2009-05-18 00:54:12 +02:00
$newemail = '' ;
if ( $format == 2 )
{
$newemail = $email ;
}
if ( $format == 1 )
{
$neweamil = '<' . $email . '>' ;
}
if ( $format == 0 )
{
if ( $conf -> global -> MAIN_MAIL_NO_FULL_EMAIL ) $newemail = '<' . $email . '>' ;
elseif ( ! $name ) $newemail = '<' . $email . '>' ;
else $newemail = ( $encode ? $this -> encodetorfc2822 ( $name ) : $name ) . ' <' . $email . '>' ;
}
2007-04-01 04:56:53 +02:00
2009-05-18 00:54:12 +02:00
$ret = ( $ret ? $ret . ',' : '' ) . $newemail ;
}
2006-08-06 20:34:37 +02:00
}
2008-11-15 01:52:42 +01:00
2009-05-18 00:54:12 +02:00
return $ret ;
}
2002-04-30 12:44:42 +02:00
}
2009-05-17 16:22:57 +02:00
2009-05-17 17:36:19 +02:00
?>