2002-04-30 12:44:42 +02:00
< ? php
2011-12-28 12:48:18 +01:00
/**
* Copyright ( C ) Dan Potter
* Copyright ( C ) Eric Seigne
* Copyright ( C ) 2000 - 2005 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
2005-06-25 15:02:39 +02:00
* Copyright ( C ) 2003 Jean - Louis Bergamo < jlb @ j1b . org >
2012-02-05 01:11:20 +01:00
* Copyright ( C ) 2004 - 2012 Laurent Destailleur < eldy @ users . sourceforge . net >
2012-12-30 15:13:49 +01:00
* Copyright ( C ) 2005 - 2012 Regis Houssin < regis . houssin @ capnetworks . com >
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
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2005-06-25 15:02:39 +02:00
* ( 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
2011-08-01 01:24:38 +02:00
* along with this program . If not , see < http :// www . gnu . org / licenses />.
2005-06-25 15:02:39 +02:00
* or see http :// www . gnu . org /
*
* Lots of code inspired from Dan Potter ' s CMailFile class
*/
/**
2011-10-24 09:29:17 +02:00
* \file htdocs / core / class / CMailFile . class . php
2010-08-28 12:55:11 +02:00
* \brief File of class to send emails ( with attachments or not )
2008-11-15 01:52:42 +01:00
*/
2004-07-16 00:17:39 +02:00
2005-06-25 15:02:39 +02:00
/**
2012-02-01 16:00:22 +01:00
* Class to send emails ( with attachments or not )
* Usage : $mailfile = new CMailFile ( $subject , $sendto , $replyto , $message , $filepath , $mimetype , $filename , $cc , $ccc , $deliveryreceipt , $msgishtml , $errors_to );
* $mailfile -> sendfile ();
2008-11-15 01:52:42 +01:00
*/
2004-07-16 00:17:39 +02:00
class CMailFile
2003-07-07 12:04:20 +02:00
{
2011-12-28 12:48:18 +01:00
var $subject ; // Topic: Subject of email
var $addr_from ; // From: Label of sender (name but can contains an email inside <>)
// Sender: Who send the email ("Sender" has sent emails on behalf of "From").
// Use it with an email from a sending host from is a SPF protected domain and sending host is not this domain.
// Return-Path: Email where to send bounds.
var $errors_to ; // Errors-To: Email where to send errors.
2008-11-15 01:52:42 +01:00
var $addr_to ;
var $addr_cc ;
var $addr_bcc ;
2011-03-25 19:12:58 +01:00
var $mixed_boundary ;
var $related_boundary ;
var $alternative_boundary ;
2008-11-15 01:52:42 +01:00
var $deliveryreceipt ;
var $eol ;
var $atleastonefile = 0 ;
var $error = '' ;
2010-01-13 14:45:29 +01:00
var $smtps ; // Contains SMTPs object (if this method is used)
2012-07-28 19:11:05 +02:00
var $phpmailer ; // Contains PHPMailer object (if this method is used)
2009-05-18 00:54:12 +02:00
2009-05-19 17:40:25 +02:00
//CSS
var $css ;
2009-11-18 16:40:17 +01:00
//! Defined css style for body background
2009-05-19 17:40:25 +02:00
var $styleCSS ;
2013-03-15 18:27:48 +01:00
//! Defined background directly in body tag
2009-11-18 16:40:17 +01:00
var $bodyCSS ;
2009-05-24 22:01:25 +02:00
2009-05-17 16:22:57 +02:00
// Image
2011-11-16 23:58:25 +01:00
var $html ;
2009-05-11 19:13:56 +02:00
var $image_boundary ;
var $atleastoneimage = 0 ;
2011-11-16 23:58:25 +01:00
var $html_images = array ();
2009-05-11 19:13:56 +02:00
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
/**
2011-07-04 11:36:29 +02:00
* CMailFile
2011-07-20 13:07:12 +02:00
*
2011-12-07 17:28:11 +01:00
* @ param string $subject Topic / Subject of mail
2013-02-23 16:21:49 +01:00
* @ param string $to Recipients emails ( RFC 2822 : " Nom firstname <email>[, ...] " ou " email[, ...] " ou " <email>[, ...] " )
* @ param string $from Sender email ( RFC 2822 : " Nom firstname <email>[, ...] " ou " email[, ...] " ou " <email>[, ...] " )
2011-12-07 17:28:11 +01:00
* @ param string $msg Message
* @ param array $filename_list List of files to attach ( full path of filename on file system )
* @ param array $mimetype_list List of MIME type of attached files
* @ param array $mimefilename_list List of attached file name in message
* @ param string $addr_cc Email cc
2014-04-25 16:45:14 +02:00
* @ param string $addr_bcc Email bcc ( Note : This is autocompleted with MAIN_MAIL_AUTOCOPY_TO if defined )
* @ param int $deliveryreceipt Ask a delivery receipt
* @ param int $msgishtml 1 = String IS already html , 0 = String IS NOT html , - 1 = Unknown make autodetection ( with fast mode , not reliable )
* @ param string $errors_to Email errors
* @ param string $css Css option
2008-11-15 01:52:42 +01:00
*/
2012-07-30 17:17:33 +02:00
function __construct ( $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
2012-11-13 17:25:47 +01:00
// We define end of line (RFC 821).
2011-07-20 13:07:12 +02:00
$this -> eol = " \r \n " ;
2013-03-13 19:14:49 +01:00
// We define end of line for header fields (RFC 822bis section 2.3 says header must contains \r\n).
2012-11-13 17:25:47 +01:00
$this -> eol2 = " \r \n " ;
2013-03-13 19:14:49 +01:00
if ( ! empty ( $conf -> global -> MAIN_FIX_FOR_BUGGED_MTA ))
2012-11-13 17:25:47 +01:00
{
$this -> eol = " \n " ;
$this -> eol2 = " \n " ;
}
2009-05-18 00:54:12 +02:00
2011-03-25 19:12:58 +01:00
// On defini mixed_boundary
2012-02-03 23:40:59 +01:00
$this -> mixed_boundary = " multipart_x. " . time () . " .x_boundary " ;
2009-05-17 12:27:13 +02:00
// On defini related_boundary
2012-02-02 09:48:35 +01:00
$this -> related_boundary = 'mul_' . dol_hash ( uniqid ( " dolibarr2 " ));
2009-05-17 12:27:13 +02:00
// On defini alternative_boundary
2012-02-02 09:48:35 +01:00
$this -> alternative_boundary = 'mul_' . dol_hash ( uniqid ( " dolibarr3 " ));
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 );
2011-11-16 23:58:25 +01:00
2009-05-11 19:13:56 +02:00
// 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
2014-08-05 09:27:40 +02:00
// Add autocopy to (Note: Adding bcc for specific modules are also done from pages)
2010-08-28 12:55:11 +02:00
if ( ! empty ( $conf -> global -> MAIN_MAIL_AUTOCOPY_TO )) $addr_bcc .= ( $addr_bcc ? ', ' : '' ) . $conf -> global -> MAIN_MAIL_AUTOCOPY_TO ;
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 = " " ;
2011-03-25 19:12:58 +01:00
$files_encoded = " " ;
2009-02-09 01:04:34 +01:00
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
2012-02-10 13:28:53 +01:00
$mime_headers = $this -> write_mimeheaders ( $filename_list , $mimefilename_list );
2009-05-13 16:49:30 +02:00
2009-11-18 13:31:55 +01:00
if ( ! empty ( $this -> html ))
{
if ( ! empty ( $css ))
{
$this -> css = $css ;
2012-02-01 17:59:38 +01:00
$this -> buildCSS (); // Build a css style (mode = all) into this->styleCSS and this->bodyCSS
2009-11-18 13:31:55 +01:00
}
2010-01-13 00:03:34 +01:00
2009-11-18 13:31:55 +01:00
$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
2014-05-01 17:06:14 +02:00
$images_encoded = '' ;
2009-05-13 21:10:06 +02:00
if ( $this -> atleastoneimage )
{
2014-05-01 17:06:14 +02:00
$images_encoded .= $this -> write_images ( $this -> images_encoded );
2011-06-20 23:18:06 +02:00
// always end related and end alternative after inline images
$images_encoded .= " -- " . $this -> related_boundary . " -- " . $this -> eol ;
$images_encoded .= $this -> eol . " -- " . $this -> alternative_boundary . " -- " . $this -> eol ;
$images_encoded .= $this -> eol ;
2009-05-13 21:10:06 +02:00
}
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 )
{
2011-03-25 19:12:58 +01:00
$files_encoded = $this -> write_files ( $filename_list , $mimetype_list , $mimefilename_list );
2009-02-09 01:04:34 +01:00
}
2008-11-15 01:52:42 +01:00
2012-02-01 20:44:06 +01:00
// We now define $this->headers and $this->message
2009-02-09 01:04:34 +01:00
$this -> headers = $smtp_headers . $mime_headers ;
// 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.
2012-02-01 20:44:06 +01:00
$this -> headers = preg_replace ( " /([ \r \n ]+) $ /i " , " " , $this -> headers );
2012-02-02 23:05:26 +01:00
$this -> message = 'This is a message with multiple parts in MIME format.' . $this -> eol ;
$this -> message .= $text_body . $images_encoded . $files_encoded ;
2012-02-01 20:44:06 +01:00
$this -> message .= " -- " . $this -> mixed_boundary . " -- " . $this -> eol ;
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
// ------------------------------------------
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/smtps.class.php' ;
2009-02-09 01:04:34 +01:00
$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-08-04 12:21:48 +02:00
$smtps -> setTO ( $this -> getValidAddress ( $to , 0 , 1 ));
$smtps -> setFrom ( $this -> getValidAddress ( $from , 0 , 1 ));
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 ;
2013-03-15 18:27:48 +01:00
$this -> buildCSS ();
2009-05-19 17:40:25 +02:00
}
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
}
2012-07-28 19:11:05 +02:00
// TODO not stable, in progress
2012-07-28 22:07:27 +02:00
else if ( $conf -> global -> MAIN_MAIL_SENDMODE == 'phpmailer' )
{
// Use PHPMailer library
// ------------------------------------------
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/includes/phpmailer/class.phpmailer.php' ;
2012-07-28 22:07:27 +02:00
$this -> phpmailer = new PHPMailer ();
$this -> phpmailer -> CharSet = $conf -> file -> character_set_client ;
$this -> phpmailer -> Subject ( $this -> encodetorfc2822 ( $subject ));
$this -> phpmailer -> setTO ( $this -> getValidAddress ( $to , 0 , 1 ));
$this -> phpmailer -> SetFrom ( $this -> getValidAddress ( $from , 0 , 1 ));
if ( ! empty ( $this -> html ))
{
if ( ! empty ( $css ))
{
$this -> css = $css ;
2013-03-15 18:27:48 +01:00
$this -> buildCSS ();
2012-07-28 22:07:27 +02:00
}
$msg = $this -> html ;
$msg = $this -> checkIfHTML ( $msg );
}
if ( $this -> msgishtml ) $smtps -> setBodyContent ( $msg , 'html' );
else $smtps -> setBodyContent ( $msg , 'plain' );
if ( $this -> atleastoneimage )
{
foreach ( $this -> images_encoded as $img )
{
$smtps -> setImageInline ( $img [ 'image_encoded' ], $img [ 'name' ], $img [ 'content_type' ], $img [ 'cid' ]);
}
}
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 ]);
}
}
$smtps -> setCC ( $addr_cc );
$smtps -> setBCC ( $addr_bcc );
$smtps -> setErrorsTo ( $errors_to );
$smtps -> setDeliveryReceipt ( $deliveryreceipt );
$this -> smtps = $smtps ;
2012-07-28 19:11:05 +02:00
}
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-11-17 23:00:30 +01:00
* Send mail that was prepared by constructor
2010-01-13 00:03:34 +01:00
*
2009-11-17 23:00:30 +01:00
* @ 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
{
2011-01-14 02:11:02 +01:00
$this -> error = " Failed to send mail with php mail to HOST= " . ini_get ( 'SMTP' ) . " , PORT= " . ini_get ( 'smtp_port' ) . " <br>Recipient address ' $dest ' invalid " ;
2011-11-01 13:22:39 +01:00
dol_syslog ( " CMailFile::sendfile: mail end error= " . $this -> error , LOG_ERR );
2009-02-09 01:04:34 +01:00
}
else
2008-08-20 21:21:16 +02:00
{
2009-11-01 15:16:30 +01:00
dol_syslog ( " CMailFile::sendfile: mail start HOST= " . ini_get ( 'SMTP' ) . " , PORT= " . ini_get ( 'smtp_port' ), LOG_DEBUG );
2009-02-09 01:04:34 +01:00
2010-07-18 13:47:55 +02:00
$bounce = '' ; // By default
2011-07-20 15:01:03 +02:00
if ( ! empty ( $conf -> global -> MAIN_MAIL_ALLOW_SENDMAIL_F ))
2009-02-09 01:04:34 +01:00
{
// 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)
2014-06-28 14:48:00 +02:00
$bounce .= ( $bounce ? ' ' : '' ) . ( ! empty ( $conf -> global -> MAIN_MAIL_ERRORS_TO ) ? '-f' . $this -> getValidAddress ( $conf -> global -> MAIN_MAIL_ERRORS_TO , 2 ) : ( $this -> addr_from != '' ? '-f' . $this -> getValidAddress ( $this -> addr_from , 2 ) : '' ) );
2009-02-09 01:04:34 +01:00
}
2011-07-20 15:01:03 +02:00
if ( ! empty ( $conf -> global -> MAIN_MAIL_SENDMAIL_FORCE_BA )) // To force usage of -ba option. This option tells sendmail to read From: or Sender: to setup sender
{
$bounce .= ( $bounce ? ' ' : '' ) . '-ba' ;
}
2009-02-09 01:04:34 +01:00
2009-05-13 21:10:06 +02:00
$this -> message = stripslashes ( $this -> message );
if ( ! empty ( $conf -> global -> MAIN_MAIL_DEBUG )) $this -> dump_mail ();
2010-07-18 13:47:55 +02:00
if ( ! empty ( $bounce )) $res = mail ( $dest , $this -> encodetorfc2822 ( $this -> subject ), $this -> message , $this -> headers , $bounce );
else $res = mail ( $dest , $this -> encodetorfc2822 ( $this -> subject ), $this -> message , $this -> headers );
2009-05-24 22:01:25 +02:00
if ( ! $res )
2009-05-17 17:36:19 +02:00
{
2011-01-14 02:11:02 +01:00
$this -> error = " Failed to send mail with php mail to HOST= " . ini_get ( 'SMTP' ) . " , PORT= " . ini_get ( 'smtp_port' ) . " <br>Check your server logs and your firewalls setup " ;
2011-11-01 13:22:39 +01:00
dol_syslog ( " CMailFile::sendfile: mail end error= " . $this -> error , LOG_ERR );
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' );
}
2009-02-09 01:04:34 +01:00
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' );
2009-11-01 15:16:30 +01:00
// If we use SSL/TLS
$server = $conf -> global -> MAIN_MAIL_SMTP_SERVER ;
2009-11-01 15:45:52 +01:00
if ( ! empty ( $conf -> global -> MAIN_MAIL_EMAIL_TLS ) && function_exists ( 'openssl_open' )) $server = 'ssl://' . $server ;
2009-11-01 15:16:30 +01:00
$this -> smtps -> setHost ( $server );
$this -> smtps -> setPort ( $conf -> global -> MAIN_MAIL_SMTP_PORT ); // 25, 465...;
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
2011-01-14 02:11:02 +01:00
$res = true ;
$from = $this -> smtps -> getFrom ( 'org' );
if ( ! $from )
{
$this -> error = " Failed to send mail with smtps lib to HOST= " . $server . " , PORT= " . $conf -> global -> MAIN_MAIL_SMTP_PORT . " <br>Sender address ' $from ' invalid " ;
dol_syslog ( " CMailFile::sendfile: mail end error= " . $this -> error , LOG_ERR );
$res = false ;
}
$dest = $this -> smtps -> getTo ();
2009-02-09 01:04:34 +01:00
if ( ! $dest )
2007-06-07 23:24:01 +02:00
{
2011-01-14 02:11:02 +01:00
$this -> error = " Failed to send mail with smtps lib to HOST= " . $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 );
2010-05-12 15:14:26 +02:00
$res = false ;
2007-06-07 23:24:01 +02:00
}
2011-07-02 18:48:31 +02:00
2011-01-14 02:11:02 +01:00
if ( $res )
2007-06-07 23:24:01 +02:00
{
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 ();
2010-04-13 22:58:36 +02:00
//print $result;
2009-02-09 01:04:34 +01:00
2010-05-12 15:14:26 +02:00
if ( ! empty ( $conf -> global -> MAIN_MAIL_DEBUG )) $this -> dump_mail ();
2009-02-09 01:04:34 +01:00
2010-05-12 15:14:26 +02:00
$result = $this -> smtps -> getErrors ();
if ( empty ( $this -> error ) && empty ( $result )) $res = true ;
else
{
if ( empty ( $this -> error )) $this -> error = $result ;
dol_syslog ( " CMailFile::sendfile: mail end error= " . $this -> error , LOG_ERR );
$res = false ;
}
2010-04-13 22:58:36 +02:00
}
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' ;
2010-07-15 01:12:49 +02:00
dol_syslog ( " CMailFile::sendfile: " . $this -> error , LOG_WARNING );
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
/**
2011-07-20 13:07:12 +02:00
* Read a file on disk and return encoded content for emails ( mode = 'mail' )
2010-01-13 00:03:34 +01:00
*
2011-12-01 23:36:00 +01:00
* @ param string $sourcefile Path to file to encode
* @ return int < 0 if KO , encoded string if OK
2009-02-09 01:04:34 +01:00
*/
function _encode_file ( $sourcefile )
{
2011-12-01 23:36:00 +01:00
$newsourcefile = dol_osencode ( $sourcefile );
2009-10-14 18:13:20 +02:00
if ( is_readable ( $newsourcefile ))
2009-02-09 01:04:34 +01:00
{
2009-10-14 18:13:20 +02:00
$contents = file_get_contents ( $newsourcefile ); // Need PHP 4.3
2012-02-01 16:00:22 +01:00
$encoded = chunk_split ( base64_encode ( $contents ), 76 , $this -> eol ); // 76 max is defined into http://tools.ietf.org/html/rfc2047
2009-02-09 01:04:34 +01:00
return $encoded ;
}
else
{
2011-12-01 23:36:00 +01:00
$this -> error = " Error: Can't read file ' " . $sourcefile . " ' into _encode_file " ;
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
/**
2010-10-10 01:17:41 +02:00
* Write content of a SMTP request into a dump file ( mode = all )
* Used for debugging .
2013-03-13 19:14:49 +01:00
* Note that to see full SMTP protocol , you can use tcpdump - w / tmp / smtp - s 2000 port 25 "
2011-12-01 23:36:00 +01:00
*
* @ return void
2008-11-15 01:52:42 +01:00
*/
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
{
2011-12-05 17:13:48 +01:00
$outputfile = $dolibarr_main_data_root . " /dolibarr_mail.log " ;
$fp = fopen ( $outputfile , " w " );
2009-02-09 01:04:34 +01:00
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' )
{
2013-03-13 19:14:49 +01:00
fputs ( $fp , $this -> smtps -> log ); // this->smtps->log is filled only if MAIN_MAIL_DEBUG was set to on
2009-02-09 01:04:34 +01:00
}
2008-11-15 01:52:42 +01:00
fclose ( $fp );
if ( ! empty ( $conf -> global -> MAIN_UMASK ))
@ chmod ( $outputfile , octdec ( $conf -> global -> MAIN_UMASK ));
}
}
2010-10-10 01:17:41 +02:00
/**
* Correct an uncomplete html string
*
2011-12-07 17:28:11 +01:00
* @ param string $msg String
* @ return string Completed string
2010-10-10 01:17:41 +02:00
*/
function checkIfHTML ( $msg )
{
if ( ! preg_match ( '/^[\s\t]*<html/i' , $msg ))
{
$out = " <html><head><title></title> " ;
if ( ! empty ( $this -> styleCSS )) $out .= $this -> styleCSS ;
$out .= " </head><body " ;
if ( ! empty ( $this -> bodyCSS )) $out .= $this -> bodyCSS ;
$out .= " > " ;
$out .= $msg ;
$out .= " </body></html> " ;
}
else
{
$out = $msg ;
}
return $out ;
}
/**
2012-02-01 17:59:38 +01:00
* Build a css style ( mode = all ) into this -> styleCSS and this -> bodyCSS
2010-10-10 01:17:41 +02:00
*
* @ return css
*/
function buildCSS ()
{
if ( ! empty ( $this -> css ))
{
// Style CSS
$this -> styleCSS = '<style type="text/css">' ;
$this -> styleCSS .= 'body {' ;
if ( $this -> css [ 'bgcolor' ])
{
$this -> styleCSS .= ' background-color: ' . $this -> css [ 'bgcolor' ] . ';' ;
2013-03-15 18:27:48 +01:00
$this -> bodyCSS .= ' bgcolor="' . $this -> css [ 'bgcolor' ] . '"' ;
2010-10-10 01:17:41 +02:00
}
if ( $this -> css [ 'bgimage' ])
{
// TODO recuperer cid
$this -> styleCSS .= ' background-image: url("cid:' . $this -> css [ 'bgimage_cid' ] . '");' ;
}
$this -> styleCSS .= '}' ;
$this -> styleCSS .= '</style>' ;
}
}
2008-11-15 01:52:42 +01:00
/**
2010-10-10 01:17:41 +02:00
* Create SMTP headers ( mode = 'mail' )
2010-01-13 00:03:34 +01:00
*
2009-11-17 23:00:30 +01:00
* @ return 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
2012-02-01 22:39:17 +01:00
$host = dol_getprefix ();
2006-08-06 20:34:37 +02:00
// Sender
2012-02-10 13:28:53 +01:00
//$out.= "Sender: ".getValidAddress($this->addr_from,2)).$this->eol2;
2012-11-14 11:31:08 +01:00
$out .= " From: " . $this -> getValidAddress ( $this -> addr_from , 3 , 1 ) . $this -> eol2 ;
2012-11-13 17:25:47 +01:00
if ( ! empty ( $conf -> global -> MAIN_MAIL_SENDMAIL_FORCE_BA ))
{
2012-11-14 11:31:08 +01:00
$out .= " To: " . $this -> getValidAddress ( $this -> addr_to , 0 , 1 ) . $this -> eol2 ;
2012-11-13 17:25:47 +01:00
}
2014-06-28 14:06:29 +02:00
// Return-Path is important because it is used by SPF. Some MTA does not read Return-Path from header but from command line. See option MAIN_MAIL_ALLOW_SENDMAIL_F for that.
2012-02-10 13:28:53 +01:00
$out .= " Return-Path: " . $this -> getValidAddress ( $this -> addr_from , 0 , 1 ) . $this -> eol2 ;
2012-02-08 12:55:25 +01:00
if ( isset ( $this -> reply_to ) && $this -> reply_to ) $out .= " Reply-To: " . $this -> getValidAddress ( $this -> reply_to , 2 ) . $this -> eol2 ;
if ( isset ( $this -> errors_to ) && $this -> errors_to ) $out .= " Errors-To: " . $this -> getValidAddress ( $this -> errors_to , 2 ) . $this -> eol2 ;
2006-08-06 20:34:37 +02:00
// Receiver
2012-02-08 12:55:25 +01:00
if ( isset ( $this -> addr_cc ) && $this -> addr_cc ) $out .= " Cc: " . $this -> getValidAddress ( $this -> addr_cc , 2 ) . $this -> eol2 ;
if ( isset ( $this -> addr_bcc ) && $this -> addr_bcc ) $out .= " Bcc: " . $this -> getValidAddress ( $this -> addr_bcc , 2 ) . $this -> eol2 ;
2008-11-15 01:52:42 +01:00
2012-02-04 13:39:21 +01:00
// Delivery receipt
2012-02-08 12:55:25 +01:00
if ( isset ( $this -> deliveryreceipt ) && $this -> deliveryreceipt == 1 ) $out .= " Disposition-Notification-To: " . $this -> getValidAddress ( $this -> addr_from , 2 ) . $this -> eol2 ;
2008-11-15 01:52:42 +01:00
2012-02-08 12:55:25 +01:00
//$out.= "X-Priority: 3".$this->eol2;
2012-02-01 22:39:17 +01:00
2012-02-08 12:55:25 +01:00
$out .= 'Date: ' . date ( " r " ) . $this -> eol2 ;
$out .= 'Message-ID: <' . time () . '.phpmail@' . $host . " > " . $this -> eol2 ;
2012-02-01 22:39:17 +01:00
2012-02-08 12:55:25 +01:00
$out .= " X-Mailer: Dolibarr version " . DOL_VERSION . " (using php mail) " . $this -> eol2 ;
$out .= " Mime-Version: 1.0 " . $this -> eol2 ;
2006-08-06 20:34:37 +02:00
2012-02-05 01:11:20 +01:00
//$out.= "From: ".$this->getValidAddress($this->addr_from,3,1).$this->eol;
2012-02-04 13:39:21 +01:00
2012-02-08 12:55:25 +01:00
$out .= " Content-Type: multipart/mixed; boundary= \" " . $this -> mixed_boundary . " \" " . $this -> eol2 ;
$out .= " Content-Transfer-Encoding: 8bit " . $this -> eol2 ;
2011-07-02 18:48:31 +02:00
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
/**
2011-07-20 13:07:12 +02:00
* Create header MIME ( mode = 'mail' )
2010-01-13 00:03:34 +01:00
*
2011-12-07 17:28:11 +01:00
* @ param array $filename_list Array of filenames
* @ param array $mimefilename_list Array of mime types
* @ return array mime headers
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
{
2010-09-01 09:50:28 +02:00
$filename_list_size = count ( $filename_list );
for ( $i = 0 ; $i < $filename_list_size ; $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 ];
2012-02-08 12:55:25 +01:00
$out .= " X-attachments: $filename_list[$i] " . $this -> eol2 ;
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 ;
}
/**
2011-07-20 13:07:12 +02:00
* Return email content ( mode = 'mail' )
2010-01-13 00:03:34 +01:00
*
2011-12-07 17:28:11 +01:00
* @ param string $msgtext Message string
* @ return string String content
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 = '' ;
2011-07-02 18:48:31 +02:00
2012-06-17 18:40:45 +02:00
$out .= " -- " . $this -> mixed_boundary . $this -> eol ;
2011-06-20 23:18:06 +02:00
if ( $this -> atleastoneimage )
2008-11-15 01:52:42 +01:00
{
2011-06-20 23:18:06 +02:00
$out .= " Content-Type: multipart/alternative; boundary= \" " . $this -> alternative_boundary . " \" " . $this -> eol ;
$out .= $this -> eol ;
$out .= " -- " . $this -> alternative_boundary . $this -> eol ;
2009-05-13 16:49:30 +02:00
}
2011-07-02 18:48:31 +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
{
2014-05-01 17:06:14 +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
2011-12-07 17:28:11 +01:00
$strContent = preg_replace ( " /(?<! \r ) \n /si " , " \r \n " , $strContent );
2012-11-14 11:31:08 +01:00
if ( ! empty ( $conf -> global -> MAIN_FIX_FOR_BUGGED_MTA ))
{
2012-11-13 17:25:47 +01:00
$strContent = preg_replace ( " / \r \n /si " , " \n " , $strContent );
}
2013-03-13 19:14:49 +01:00
2010-10-18 21:52:57 +02:00
//$strContent = rtrim(chunk_split($strContent)); // Function chunck_split seems bugged
$strContent = rtrim ( wordwrap ( $strContent ));
2009-05-13 21:10:06 +02:00
2011-06-20 23:18:06 +02:00
if ( $this -> msgishtml )
{
2011-07-02 18:48:31 +02:00
if ( $this -> atleastoneimage )
2011-06-20 23:18:06 +02:00
{
$out .= " Content-Type: text/plain; charset= " . $conf -> file -> character_set_client . $this -> eol ;
$out .= $this -> eol . strip_tags ( $strContent ) . $this -> eol ; // Add plain text message
$out .= " -- " . $this -> alternative_boundary . $this -> eol ;
$out .= " Content-Type: multipart/related; boundary= \" " . $this -> related_boundary . " \" " . $this -> eol ;
$out .= $this -> eol ;
$out .= " -- " . $this -> related_boundary . $this -> eol ;
}
$out .= " Content-Type: text/html; charset= " . $conf -> file -> character_set_client . $this -> eol ;
$out .= $this -> eol . $strContent . $this -> eol ;
}
else
{
$out .= " Content-Type: text/plain; charset= " . $conf -> file -> character_set_client . $this -> eol ;
2011-06-24 13:13:29 +02:00
$out .= $this -> eol . $strContent . $this -> eol ;
2011-06-20 23:18:06 +02:00
}
2011-07-02 18:48:31 +02:00
2011-06-20 23:18:06 +02:00
$out .= $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
2008-11-15 01:52:42 +01:00
/**
2011-07-20 13:07:12 +02:00
* Attach file to email ( mode = 'mail' )
2010-01-13 00:03:34 +01:00
*
2011-12-07 17:28:11 +01:00
* @ param array $filename_list Tableau
* @ param array $mimetype_list Tableau
* @ param array $mimefilename_list Tableau
* @ return string Chaine fichiers encodes
2008-11-15 01:52:42 +01:00
*/
function write_files ( $filename_list , $mimetype_list , $mimefilename_list )
{
$out = '' ;
2010-09-01 09:50:28 +02:00
$filename_list_size = count ( $filename_list );
for ( $i = 0 ; $i < $filename_list_size ; $i ++ )
2008-11-15 01:52:42 +01:00
{
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 " ; }
2011-03-25 19:12:58 +01:00
$out .= " -- " . $this -> mixed_boundary . $this -> eol ;
2010-10-10 01:17:41 +02:00
$out .= " Content-Disposition: attachment; filename= \" " . $filename_list [ $i ] . " \" " . $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 ;
2010-10-10 01:17:41 +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 ;
}
/**
2011-07-20 13:07:12 +02:00
* Attach an image to email ( mode = 'mail' )
2010-01-13 00:03:34 +01:00
*
2011-12-07 17:28:11 +01:00
* @ param array $images_list Tableau
* @ return string Chaine images encodees
2009-05-13 21:10:06 +02:00
*/
function write_images ( $images_list )
{
$out = '' ;
if ( $images_list )
{
foreach ( $images_list as $img )
{
dol_syslog ( " CMailFile::write_images: i= $i " );
2011-06-20 23:18:06 +02:00
$out .= " -- " . $this -> related_boundary . $this -> eol ; // always related for an inline image
2009-05-13 21:10:06 +02:00
$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 ;
}
2009-11-01 15:16:30 +01:00
/**
* Try to create a socket connection
2011-07-20 13:07:12 +02:00
*
2011-12-07 17:28:11 +01:00
* @ param string $host Add ssl :// for SSL / TLS .
* @ param int $port Example : 25 , 465
* @ return int Socket id if ok , 0 if KO
2009-11-01 15:16:30 +01:00
*/
2008-04-17 01:11:45 +02:00
function check_server_port ( $host , $port )
{
$_retVal = 0 ;
2009-11-01 15:16:30 +01:00
$timeout = 5 ; // Timeout in seconds
2008-04-17 01:11:45 +02:00
if ( function_exists ( 'fsockopen' ))
{
2009-10-28 21:37:06 +01:00
dol_syslog ( " Try socket connection to host= " . $host . " port= " . $port );
2008-04-17 01:11:45 +02:00
//See if we can connect to the SMTP server
2011-12-07 17:28:11 +01:00
if ( $socket = @ fsockopen (
$host , // Host to test, IP or domain. Add ssl:// for SSL/TLS.
$port , // which Port number to use
$errno , // actual system level error
$errstr , // and any text that goes with the error
$timeout
)) // timeout for reading/writing data over the socket
2008-11-15 01:52:42 +01:00
{
2008-04-17 01:11:45 +02:00
// Windows still does not have support for this timeout function
2009-11-01 15:16:30 +01:00
if ( function_exists ( 'stream_set_timeout' )) stream_set_timeout ( $socket , $timeout , 0 );
2009-10-28 21:37:06 +01:00
dol_syslog ( " Now we wait for answer 220 " );
2008-04-17 01:11:45 +02:00
// Check response from Server
2009-11-01 15:16:30 +01:00
if ( $_retVal = $this -> server_parse ( $socket , " 220 " ) ) $_retVal = $socket ;
2008-04-17 01:11:45 +02:00
}
else
{
2011-12-07 17:28:11 +01:00
$this -> error = utf8_check ( 'Error ' . $errno . ' - ' . $errstr ) ? 'Error ' . $errno . ' - ' . $errstr : utf8_encode ( 'Error ' . $errno . ' - ' . $errstr );
2008-04-17 01:11:45 +02:00
}
}
return $_retVal ;
}
2010-07-21 19:39:17 +02:00
/**
2011-07-20 13:07:12 +02:00
* This function has been modified as provided by SirSir to allow multiline responses when
2010-07-21 19:39:17 +02:00
* using SMTP Extensions .
2011-07-20 13:07:12 +02:00
*
2011-12-07 17:28:11 +01:00
* @ param Socket $socket Socket
* @ param string $response Response string
* @ return boolean true if success
2010-07-21 19:39:17 +02:00
*/
2008-04-17 01:11:45 +02:00
function server_parse ( $socket , $response )
{
2010-10-28 00:41:40 +02:00
$_retVal = true ; // Indicates if Object was created or not
2008-04-17 01:11:45 +02:00
$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
/**
2011-07-20 13:07:12 +02:00
* Seearch images into html message and init array this -> images_encoded if found
2010-01-13 00:03:34 +01:00
*
2011-12-07 17:28:11 +01:00
* @ param string $images_dir Location of physical images files
* @ return int > 0 if OK , < 0 if KO
2009-05-11 19:13:56 +02:00
*/
function findHtmlImages ( $images_dir )
2009-05-13 16:49:30 +02:00
{
// Build the list of image extensions
$extensions = array_keys ( $this -> image_types );
2011-11-16 23:58:25 +01:00
2009-05-13 16:49:30 +02:00
preg_match_all ( '/(?:"|\')([^"\']+\.(' . implode ( '|' , $extensions ) . '))(?:"|\')/Ui' , $this -> html , $matches );
if ( $matches )
{
$i = 0 ;
foreach ( $matches [ 1 ] as $full )
{
2009-11-18 15:19:03 +01:00
if ( preg_match ( '/file=([A-Za-z0-9_\-\/]+[\.]?[A-Za-z0-9]+)?$/i' , $full , $regs ))
2009-05-13 16:49:30 +02:00
{
2009-11-18 15:19:03 +01:00
$img = $regs [ 1 ];
2010-01-13 00:03:34 +01:00
2009-11-18 15:19:03 +01:00
if ( file_exists ( $images_dir . '/' . $img ))
{
// Image path in src
$src = preg_quote ( $full , '/' );
2010-01-13 00:03:34 +01:00
2009-11-18 15:19:03 +01:00
// Image full path
$this -> html_images [ $i ][ " fullpath " ] = $images_dir . '/' . $img ;
2010-01-13 00:03:34 +01:00
2009-11-18 15:19:03 +01:00
// Image name
$this -> html_images [ $i ][ " name " ] = $img ;
2010-01-13 00:03:34 +01:00
2009-11-18 15:19:03 +01:00
// Content type
2013-09-11 15:17:31 +02:00
if ( preg_match ( '/^.+\.(\w{3,4})$/' , $img , $reg ))
{
$ext = strtolower ( $reg [ 1 ]);
$this -> html_images [ $i ][ " content_type " ] = $this -> image_types [ $ext ];
2013-09-11 14:48:02 +02:00
}
2010-01-13 00:03:34 +01:00
2009-11-18 15:19:03 +01:00
// cid
2011-09-06 08:53:52 +02:00
$this -> html_images [ $i ][ " cid " ] = dol_hash ( uniqid ( time ()));
2009-11-18 15:19:03 +01:00
$this -> html = preg_replace ( " /src= \" $src\ " | src = '$src' / i " , " src = \ " cid: " . $this -> html_images [ $i ][ " cid " ] . " \" " , $this -> html );
}
$i ++ ;
2009-05-13 16:49:30 +02:00
}
}
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
2009-10-23 12:44:19 +02:00
preg_match ( '/([A-Za-z0-9_-]+[\.]?[A-Za-z0-9]+)?$/i' , $img [ " name " ], $regs );
2009-05-18 09:30:53 +02:00
$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-11-17 23:00:30 +01:00
* Return an address for SMTP protocol
2011-07-20 13:07:12 +02:00
*
2013-02-15 19:56:51 +01:00
* @ param string $address Example : 'John Doe <john@doe.com>' or 'john@doe.com'
2012-02-02 10:22:04 +01:00
* @ param int $format 0 = auto , 1 = emails with <> , 2 = emails without <> , 3 = auto + label between "
2011-12-07 17:28:11 +01:00
* @ param int $encode 1 = Encode name to RFC2822
2012-02-02 10:22:04 +01:00
* @ return string If format 0 : '<john@doe.com>' or 'John Doe <john@doe.com>' or '=?UTF-8?B?Sm9obiBEb2U=?= <john@doe.com>'
* If format 1 : '<john@doe.com>'
2011-12-07 17:28:11 +01:00
* If format 2 : 'john@doe.com'
2012-02-02 10:22:04 +01:00
* If format 3 : '<john@doe.com>' or '"John Doe" <john@doe.com>' or '"=?UTF-8?B?Sm9obiBEb2U=?=" <john@doe.com>'
2009-05-18 00:54:12 +02:00
*/
2013-02-15 19:56:51 +01:00
function getValidAddress ( $address , $format , $encode = '' )
2009-05-18 00:54:12 +02:00
{
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
2013-02-15 19:56:51 +01:00
$arrayaddress = explode ( ',' , $address );
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-10-23 12:44:19 +02:00
if ( preg_match ( '/^(.*)<(.*)>$/i' , 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 ;
}
2012-02-02 10:22:04 +01:00
if ( $format == 1 || $format == 3 )
2009-05-18 00:54:12 +02:00
{
2009-09-23 20:17:34 +02:00
$newemail = '<' . $email . '>' ;
2009-05-18 00:54:12 +02:00
}
2012-02-02 10:22:04 +01:00
if ( $format == 0 || $format == 3 )
2009-05-18 00:54:12 +02:00
{
2012-09-15 10:01:35 +02:00
if ( ! empty ( $conf -> global -> MAIN_MAIL_NO_FULL_EMAIL )) $newemail = '<' . $email . '>' ;
2009-05-18 00:54:12 +02:00
elseif ( ! $name ) $newemail = '<' . $email . '>' ;
2012-02-02 10:22:04 +01:00
else $newemail = ( $format == 3 ? '"' : '' ) . ( $encode ? $this -> encodetorfc2822 ( $name ) : $name ) . ( $format == 3 ? '"' : '' ) . ' <' . $email . '>' ;
2009-05-18 00:54:12 +02:00
}
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