2013-09-23 11:17:31 +00:00
< ? php
/* Copyright ( C ) 2013 Laurent Destailleur < eldy @ users . sourceforge . net >
2026-05-09 22:26:46 +00:00
* Copyright ( C ) 2024 - 2026 MDW < mdeweerd @ users . noreply . github . com >
2025-11-09 00:34:33 +00:00
* Copyright ( C ) 2024 - 2025 Frédéric France < frederic . france @ free . fr >
2018-12-01 15:55:45 +00:00
* Copyright ( C ) 2013 Juanjo Menent < jmenent @ 2 byte . es >
*
2017-03-02 13:29:22 +00: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 3 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
2019-09-23 19:55:30 +00:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
* or see https :// www . gnu . org /
2017-03-02 13:29:22 +00:00
*/
2013-09-23 11:17:31 +00:00
/**
* \file htdocs / core / actions_sendmails . inc . php
2017-09-22 15:48:38 +00:00
* \brief Code for actions on sending mails from object page
*/
2013-09-23 11:17:31 +00:00
2024-11-04 22:53:20 +00:00
/**
* @ var Conf $conf
* @ var DoliDB $db
2024-11-10 10:16:23 +00:00
* @ var HookManager $hookmanager
2024-11-04 22:53:20 +00:00
* @ var Societe $mysoc
* @ var Translate $langs
2025-03-05 19:25:12 +00:00
* @ var User $user
2024-11-04 22:53:20 +00:00
*
2026-04-27 22:43:57 +00:00
* @ var CommonObject $object
*
2025-03-05 19:25:12 +00:00
* @ var int $id
2026-04-27 22:42:43 +00:00
* @ var int $error
2025-03-05 19:25:12 +00:00
* @ var string $dolibarr_main_url_root
* @ var string $action
2024-11-04 22:53:20 +00:00
* @ var ? string $subject
2025-03-05 19:25:12 +00:00
* @ var ? string $triggersendname ( can be '' )
* @ var ? string $sendcontext
* @ var ? string $autocopy ( used to know the automatic BCC to add )
* @ var ? string $actiontypecode
* @ var ? string $paramname
2024-11-04 22:53:20 +00:00
*/
2024-03-13 18:43:34 +00:00
'
@ phan - var - force Societe $mysoc
@ phan - var - force CommonObject $object
2026-04-27 22:43:57 +00:00
@ phan - var - force int $error
2024-03-13 18:43:34 +00:00
' ;
2013-09-23 11:17:31 +00:00
2024-11-10 10:16:23 +00:00
2013-09-23 11:17:31 +00:00
/*
* Add file in email form
2017-06-17 08:44:08 +00:00
*/
2021-01-02 08:35:57 +00:00
if ( GETPOST ( 'addfile' , 'alpha' )) {
2019-01-27 10:55:16 +00:00
$trackid = GETPOST ( 'trackid' , 'aZ09' );
2017-01-23 07:33:05 +00:00
2017-03-02 13:29:22 +00:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2013-09-23 11:17:31 +00:00
// Set tmp user directory
2019-12-18 14:20:36 +00:00
$vardir = $conf -> user -> dir_output . " / " . $user -> id ;
$upload_dir_tmp = $vardir . '/temp' ; // TODO Add $keytoavoidconflict in upload_dir path
2013-09-23 11:17:31 +00:00
2019-05-19 00:03:16 +00:00
dol_add_file_process ( $upload_dir_tmp , 1 , 0 , 'addedfile' , '' , null , $trackid , 0 );
2019-12-18 14:20:36 +00:00
$action = 'presend' ;
2013-09-23 11:17:31 +00:00
}
/*
* Remove file in email form
*/
2025-02-25 14:15:49 +00:00
if ( GETPOST ( 'removedfile' ) && ! GETPOST ( 'removeAll' )) {
2019-01-27 10:55:16 +00:00
$trackid = GETPOST ( 'trackid' , 'aZ09' );
2017-01-23 07:33:05 +00:00
2013-09-23 11:17:31 +00:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
// Set tmp user directory
2019-12-18 14:20:36 +00:00
$vardir = $conf -> user -> dir_output . " / " . $user -> id ;
$upload_dir_tmp = $vardir . '/temp' ; // TODO Add $keytoavoidconflict in upload_dir path
2013-09-23 11:17:31 +00:00
2016-06-24 14:10:52 +00:00
// TODO Delete only files that was uploaded from email form. This can be addressed by adding the trackid into the temp path then changing donotdeletefile to 2 instead of 1 to say "delete only if into temp dir"
// GETPOST('removedfile','alpha') is position of file into $_SESSION["listofpaths"...] array.
2025-03-15 15:34:35 +00:00
dol_remove_file_process ( GETPOSTINT ( 'removedfile' ), 0 , 1 , $trackid ); // We do not delete because if file is the official PDF of doc, we don't want to remove it physically
2019-12-18 14:20:36 +00:00
$action = 'presend' ;
2013-09-23 11:17:31 +00:00
}
2015-08-24 14:05:15 +00:00
/*
* Remove all files in email form
*/
2025-02-25 14:15:49 +00:00
if ( GETPOST ( 'removeAll' , 'alpha' )) {
2019-01-27 10:55:16 +00:00
$trackid = GETPOST ( 'trackid' , 'aZ09' );
2017-01-23 07:33:05 +00:00
2019-12-18 14:20:36 +00:00
$listofpaths = array ();
$listofnames = array ();
$listofmimes = array ();
$keytoavoidconflict = empty ( $trackid ) ? '' : '-' . $trackid ;
2021-01-02 08:35:57 +00:00
if ( ! empty ( $_SESSION [ " listofpaths " . $keytoavoidconflict ])) {
$listofpaths = explode ( ';' , $_SESSION [ " listofpaths " . $keytoavoidconflict ]);
}
if ( ! empty ( $_SESSION [ " listofnames " . $keytoavoidconflict ])) {
$listofnames = explode ( ';' , $_SESSION [ " listofnames " . $keytoavoidconflict ]);
}
if ( ! empty ( $_SESSION [ " listofmimes " . $keytoavoidconflict ])) {
$listofmimes = explode ( ';' , $_SESSION [ " listofmimes " . $keytoavoidconflict ]);
}
2015-08-24 14:05:15 +00:00
include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php' ;
$formmail = new FormMail ( $db );
2016-06-24 14:10:52 +00:00
$formmail -> trackid = $trackid ;
2017-01-23 07:33:05 +00:00
2021-01-02 08:35:57 +00:00
foreach ( $listofpaths as $key => $value ) {
2015-08-24 14:05:15 +00:00
$pathtodelete = $value ;
$filetodelete = $listofnames [ $key ];
2024-01-13 18:48:20 +00:00
$result = dol_delete_file ( $pathtodelete , 1 ); // Delete uploaded Files
2016-09-20 10:19:17 +00:00
2015-08-24 14:05:15 +00:00
$langs -> load ( " other " );
2019-01-27 10:55:16 +00:00
setEventMessages ( $langs -> trans ( " FileWasRemoved " , $filetodelete ), null , 'mesgs' );
2016-09-20 10:19:17 +00:00
2015-08-24 14:05:15 +00:00
$formmail -> remove_attached_files ( $key ); // Update Session
}
}
2013-09-23 11:17:31 +00:00
/*
* Send mail
*/
2025-02-26 16:32:15 +00:00
if (( $action == 'send' || $action == 'relance' ) && ! GETPOST ( 'addfile' ) && ! GETPOST ( 'removeAll' ) && ! GETPOST ( 'removedfile' ) && ! GETPOST ( 'cancel' ) && ! GETPOST ( 'modelselected' )) {
2021-01-02 08:35:57 +00:00
if ( empty ( $trackid )) {
$trackid = GETPOST ( 'trackid' , 'aZ09' );
}
2018-01-22 16:15:44 +00:00
2022-10-23 15:14:23 +00:00
// Set tmp user directory (used to convert images embedded as img src=data:image)
$vardir = $conf -> user -> dir_output . " / " . $user -> id ;
$upload_dir_tmp = $vardir . '/temp' ; // TODO Add $keytoavoidconflict in upload_dir path
2021-03-01 19:37:16 +00:00
$subject = '' ;
2022-04-07 11:28:24 +00:00
//$actionmsg = '';
2021-03-01 19:37:16 +00:00
$actionmsg2 = '' ;
2025-11-09 00:34:33 +00:00
$thirdparty = null ;
$contact = null ;
$result = 0 ;
2017-01-23 07:33:05 +00:00
2013-09-23 11:17:31 +00:00
$langs -> load ( 'mails' );
2024-11-19 02:07:18 +00:00
$sendtosocid = 0 ; // Id of related thirdparty
2021-01-02 08:35:57 +00:00
if ( is_object ( $object )) {
2019-12-18 14:20:36 +00:00
$result = $object -> fetch ( $id );
2017-03-02 13:29:22 +00:00
2021-01-02 08:35:57 +00:00
if ( method_exists ( $object , " fetch_thirdparty " ) && ! in_array ( $object -> element , array ( 'member' , 'user' , 'expensereport' , 'societe' , 'contact' ))) {
2025-07-21 21:20:08 +00:00
$object -> fetch_thirdparty ();
2019-12-18 14:20:36 +00:00
$thirdparty = $object -> thirdparty ;
2021-01-02 08:35:57 +00:00
if ( is_object ( $thirdparty )) {
$sendtosocid = $thirdparty -> id ;
}
} elseif ( $object -> element == 'member' || $object -> element == 'user' ) {
2026-05-09 22:26:46 +00:00
'@phan-var-force Adherent|User $object' ;
2019-12-18 14:20:36 +00:00
$thirdparty = $object ;
2021-01-02 08:35:57 +00:00
if ( $object -> socid > 0 ) {
$sendtosocid = $object -> socid ;
}
} elseif ( $object -> element == 'expensereport' ) {
2026-05-09 22:26:46 +00:00
'@phan-var-force ExpenseReport $object' ;
2019-12-18 14:20:36 +00:00
$tmpuser = new User ( $db );
2019-09-23 10:36:12 +00:00
$tmpuser -> fetch ( $object -> fk_user_author );
2019-12-18 14:20:36 +00:00
$thirdparty = $tmpuser ;
2021-01-02 08:35:57 +00:00
if ( $object -> socid > 0 ) {
$sendtosocid = $object -> socid ;
}
} elseif ( $object -> element == 'societe' ) {
2026-05-09 22:26:46 +00:00
'@phan-var-force Societe $object' ;
2019-12-18 14:20:36 +00:00
$thirdparty = $object ;
2021-01-02 08:35:57 +00:00
if ( is_object ( $thirdparty ) && $thirdparty -> id > 0 ) {
$sendtosocid = $thirdparty -> id ;
}
} elseif ( $object -> element == 'contact' ) {
2026-05-09 22:26:46 +00:00
'@phan-var-force Contact $object' ;
2019-12-18 14:20:36 +00:00
$contact = $object ;
2020-08-23 00:38:56 +00:00
if ( $contact -> id > 0 ) {
$contact -> fetch_thirdparty ();
$thirdparty = $contact -> thirdparty ;
2021-01-02 08:35:57 +00:00
if ( is_object ( $thirdparty ) && $thirdparty -> id > 0 ) {
$sendtosocid = $thirdparty -> id ;
}
2020-08-23 00:38:56 +00:00
}
2021-01-02 08:35:57 +00:00
} else {
2024-01-20 08:22:38 +00:00
dol_print_error ( null , " Use actions_sendmails.in.php for an element/object ' " . $object -> element . " ' that is not supported " );
2021-01-02 08:35:57 +00:00
}
2018-04-11 16:31:46 +00:00
2021-01-02 08:35:57 +00:00
if ( is_object ( $hookmanager )) {
2019-12-18 14:20:36 +00:00
$parameters = array ();
$reshook = $hookmanager -> executeHooks ( 'initSendToSocid' , $parameters , $object , $action ); // Note that $action and $object may have been modified by some hooks
2018-04-11 16:31:46 +00:00
}
2020-08-23 00:38:56 +00:00
} else {
$thirdparty = $mysoc ;
2026-02-07 23:00:12 +00:00
$result = 1 ; // No object to fetch (e.g. Setup -> Emails -> send test email): consider OK
2020-08-23 00:38:56 +00:00
}
2013-09-23 11:17:31 +00:00
2021-01-02 08:35:57 +00:00
if ( $result > 0 ) {
2026-07-19 15:32:14 +00:00
$email_from = '' ;
2019-12-18 14:20:36 +00:00
$sendto = '' ;
$sendtocc = '' ;
$sendtobcc = '' ;
2017-02-22 16:46:54 +00:00
$sendtoid = array ();
2019-12-18 14:20:36 +00:00
$sendtouserid = array ();
$sendtoccuserid = array ();
2017-02-22 16:46:54 +00:00
// Define $sendto
2022-02-22 22:44:56 +00:00
$receiver = GETPOST ( 'receiver' , 'alphawithlgt' );
2021-01-02 08:35:57 +00:00
if ( ! is_array ( $receiver )) {
if ( $receiver == '-1' ) {
$receiver = array ();
} else {
$receiver = array ( $receiver );
}
2017-02-22 16:46:54 +00:00
}
2019-09-23 10:36:12 +00:00
2019-12-18 14:20:36 +00:00
$tmparray = array ();
2024-03-28 20:29:02 +00:00
if ( trim ( GETPOST ( 'sendto' , 'alphawithlgt' ))) {
2021-07-31 14:54:18 +00:00
// Recipients are provided into free text field
2022-02-22 22:44:56 +00:00
$tmparray [] = trim ( GETPOST ( 'sendto' , 'alphawithlgt' ));
2013-09-23 11:17:31 +00:00
}
2020-08-23 00:38:56 +00:00
2024-03-28 20:29:02 +00:00
if ( trim ( GETPOST ( 'tomail' , 'alphawithlgt' ))) {
2021-07-31 14:54:18 +00:00
// Recipients are provided into free hidden text field
2022-02-22 22:44:56 +00:00
$tmparray [] = trim ( GETPOST ( 'tomail' , 'alphawithlgt' ));
2021-07-29 15:32:06 +00:00
}
2021-01-02 08:35:57 +00:00
if ( count ( $receiver ) > 0 ) {
2020-08-23 00:38:56 +00:00
// Recipient was provided from combo list
2021-01-02 08:35:57 +00:00
foreach ( $receiver as $key => $val ) {
if ( $val == 'thirdparty' ) { // Key selected means current third party ('thirdparty' may be used for current member or current user too)
2026-04-03 01:15:36 +00:00
$thirdpartyEmail = ( is_object ( $thirdparty ) && ! empty ( $thirdparty -> email )) ? ( string ) $thirdparty -> email : '' ;
if ( $thirdpartyEmail !== '' ) {
$thirdpartyLabel = '' ;
if ( is_object ( $thirdparty )) {
if ( method_exists ( $thirdparty , 'getFullName' )) {
$thirdpartyLabel = ( string ) $thirdparty -> getFullName ( $langs );
} elseif ( ! empty ( $thirdparty -> name )) {
$thirdpartyLabel = ( string ) $thirdparty -> name ;
} elseif ( ! empty ( $thirdparty -> nom )) {
$thirdpartyLabel = ( string ) $thirdparty -> nom ;
}
}
if ( $thirdpartyLabel !== '' ) {
$tmparray [] = dol_string_nospecial ( $thirdpartyLabel , ' ' , array ( " , " )) . ' <' . $thirdpartyEmail . '>' ;
} else {
$tmparray [] = $thirdpartyEmail ;
}
}
2021-01-02 08:35:57 +00:00
} elseif ( $val == 'contact' ) { // Key selected means current contact
2019-09-23 10:36:12 +00:00
$tmparray [] = dol_string_nospecial ( $contact -> getFullName ( $langs ), ' ' , array ( " , " )) . ' <' . $contact -> email . '>' ;
2020-08-23 00:38:56 +00:00
$sendtoid [] = $contact -> id ;
2024-12-26 10:15:22 +00:00
} elseif ( $val && $object -> element == 'project' && empty ( $object -> socid )) { // $val is the Id of a contact
$contact = new Contact ( $db );
$ret = $contact -> fetch (( int ) $val );
if ( $ret > 0 && ! empty ( $contact -> socid )) {
$thirdparty = new Societe ( $db );
$thirdparty -> fetch ( $contact -> socid );
$tmparray [] = $thirdparty -> contact_get_property (( int ) $val , 'email' );
$sendtoid [] = (( int ) $val );
}
2021-01-02 08:35:57 +00:00
} elseif ( $val ) { // $val is the Id of a contact
2019-01-27 10:55:16 +00:00
$tmparray [] = $thirdparty -> contact_get_property (( int ) $val , 'email' );
2020-08-23 00:38:56 +00:00
$sendtoid [] = (( int ) $val );
2017-02-15 10:41:09 +00:00
}
2013-09-23 11:17:31 +00:00
}
}
2019-09-23 10:36:12 +00:00
2023-11-27 10:39:32 +00:00
if ( getDolGlobalString ( 'MAIN_MAIL_ENABLED_USER_DEST_SELECT' )) {
2022-02-22 22:44:56 +00:00
$receiveruser = GETPOST ( 'receiveruser' , 'alphawithlgt' );
2021-01-02 08:35:57 +00:00
if ( is_array ( $receiveruser ) && count ( $receiveruser ) > 0 ) {
2018-03-21 10:48:30 +00:00
$fuserdest = new User ( $db );
2021-01-02 08:35:57 +00:00
foreach ( $receiveruser as $key => $val ) {
2019-01-27 10:55:16 +00:00
$tmparray [] = $fuserdest -> user_get_property ( $val , 'email' );
2018-05-22 18:17:31 +00:00
$sendtouserid [] = $val ;
2018-03-21 10:48:30 +00:00
}
}
}
2018-05-22 18:17:31 +00:00
2019-12-18 14:20:36 +00:00
$sendto = implode ( ',' , $tmparray );
2017-03-02 13:29:22 +00:00
2017-02-22 16:46:54 +00:00
// Define $sendtocc
2022-02-22 22:44:56 +00:00
$receivercc = GETPOST ( 'receivercc' , 'alphawithlgt' );
2021-01-02 08:35:57 +00:00
if ( ! is_array ( $receivercc )) {
if ( $receivercc == '-1' ) {
$receivercc = array ();
} else {
$receivercc = array ( $receivercc );
}
2017-02-22 16:46:54 +00:00
}
2019-12-18 14:20:36 +00:00
$tmparray = array ();
2024-03-28 20:29:02 +00:00
if ( trim ( GETPOST ( 'sendtocc' , 'alphawithlgt' ))) {
2022-02-22 22:44:56 +00:00
$tmparray [] = trim ( GETPOST ( 'sendtocc' , 'alphawithlgt' ));
2014-11-04 12:54:18 +00:00
}
2021-01-02 08:35:57 +00:00
if ( count ( $receivercc ) > 0 ) {
foreach ( $receivercc as $key => $val ) {
2021-01-12 22:30:59 +00:00
if ( $val == 'thirdparty' ) { // Key selected means current thirdparty (may be usd for current member or current user too)
2021-01-02 08:35:57 +00:00
// Recipient was provided from combo list
2025-11-09 00:34:33 +00:00
$tmparray [] = dol_string_nospecial (( string ) $thirdparty -> name , ' ' , array ( " , " )) . ' <' . $thirdparty -> email . '>' ;
2021-01-02 08:35:57 +00:00
} elseif ( $val == 'contact' ) { // Key selected means current contact
// Recipient was provided from combo list
2025-11-09 00:34:33 +00:00
$tmparray [] = dol_string_nospecial (( string ) $contact -> name , ' ' , array ( " , " )) . ' <' . $contact -> email . '>' ;
2020-08-23 00:38:56 +00:00
//$sendtoid[] = $contact->id; TODO Add also id of contact in CC ?
2021-01-02 08:35:57 +00:00
} elseif ( $val ) { // $val is the Id of a contact
2019-01-27 10:55:16 +00:00
$tmparray [] = $thirdparty -> contact_get_property (( int ) $val , 'email' );
2020-08-23 00:38:56 +00:00
//$sendtoid[] = ((int) $val); TODO Add also id of contact in CC ?
2017-02-22 16:46:54 +00:00
}
2014-11-04 12:54:18 +00:00
}
}
2023-11-27 10:39:32 +00:00
if ( getDolGlobalString ( 'MAIN_MAIL_ENABLED_USER_DEST_SELECT' )) {
2022-02-22 22:44:56 +00:00
$receiverccuser = GETPOST ( 'receiverccuser' , 'alphawithlgt' );
2018-03-21 10:48:30 +00:00
2021-01-02 08:35:57 +00:00
if ( is_array ( $receiverccuser ) && count ( $receiverccuser ) > 0 ) {
2018-03-21 10:48:30 +00:00
$fuserdest = new User ( $db );
2021-01-02 08:35:57 +00:00
foreach ( $receiverccuser as $key => $val ) {
2019-01-27 10:55:16 +00:00
$tmparray [] = $fuserdest -> user_get_property ( $val , 'email' );
2018-05-22 18:17:31 +00:00
$sendtoccuserid [] = $val ;
2018-03-21 10:48:30 +00:00
}
}
}
2019-12-18 14:20:36 +00:00
$sendtocc = implode ( ',' , $tmparray );
2013-09-23 11:17:31 +00:00
2021-01-02 08:35:57 +00:00
if ( dol_strlen ( $sendto )) {
2020-09-07 08:18:17 +00:00
// Define $urlwithroot
$urlwithouturlroot = preg_replace ( '/' . preg_quote ( DOL_URL_ROOT , '/' ) . '$/i' , '' , trim ( $dolibarr_main_url_root ));
$urlwithroot = $urlwithouturlroot . DOL_URL_ROOT ; // This is to use external domain name found into config file
//$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
2017-06-17 08:44:08 +00:00
2020-09-07 08:18:17 +00:00
require_once DOL_DOCUMENT_ROOT . '/core/class/CMailFile.class.php' ;
2017-01-23 07:33:05 +00:00
2013-09-23 11:17:31 +00:00
$langs -> load ( " commercial " );
2020-01-20 17:38:53 +00:00
$reg = array ();
2019-01-27 10:55:16 +00:00
$fromtype = GETPOST ( 'fromtype' , 'alpha' );
2025-03-05 18:34:24 +00:00
$emailsendersignature = '' ;
2017-11-25 17:52:47 +00:00
if ( $fromtype === 'robot' ) {
2026-07-19 15:32:14 +00:00
$email_from = dol_string_nospecial ( getDolGlobalString ( 'MAIN_MAIL_EMAIL_FROM' ), ' ' , array ( " , " )) . ' <' . getDolGlobalString ( 'MAIN_MAIL_EMAIL_FROM' ) . '>' ;
2020-05-21 13:05:19 +00:00
} elseif ( $fromtype === 'user' ) {
2026-07-19 15:32:14 +00:00
$email_from = dol_string_nospecial ( $user -> getFullName ( $langs ), ' ' , array ( " , " )) . ' <' . $user -> email . '>' ;
2020-05-21 13:05:19 +00:00
} elseif ( $fromtype === 'company' ) {
2026-07-19 15:32:14 +00:00
$email_from = dol_string_nospecial ( getDolGlobalString ( 'MAIN_INFO_SOCIETE_NOM' ), ' ' , array ( " , " )) . ' <' . getDolGlobalString ( 'MAIN_INFO_SOCIETE_MAIL' ) . '>' ;
2020-05-21 13:05:19 +00:00
} elseif ( preg_match ( '/global_aliases_(\d+)/' , $fromtype , $reg )) {
2023-12-13 14:20:53 +00:00
$tmp = explode ( ',' , getDolGlobalString ( 'MAIN_INFO_SOCIETE_MAIL_ALIASES' ));
2026-07-19 15:32:14 +00:00
$email_from = trim ( $tmp [(( int ) $reg [ 1 ] - 1 )]);
2020-05-21 13:05:19 +00:00
} elseif ( preg_match ( '/senderprofile_(\d+)_(\d+)/' , $fromtype , $reg )) {
2026-07-19 15:32:14 +00:00
$sql = " SELECT rowid, label, email, signature FROM " . MAIN_DB_PREFIX . " c_email_senderprofile " ;
$sql .= " WHERE rowid = " . ( int ) $reg [ 1 ];
2017-10-11 17:41:19 +00:00
$resql = $db -> query ( $sql );
$obj = $db -> fetch_object ( $resql );
2021-01-02 08:35:57 +00:00
if ( $obj ) {
2026-07-19 15:32:14 +00:00
$email_from = dol_string_nospecial ( $obj -> label , ' ' , array ( " , " )) . ' <' . $obj -> email . '>' ;
2025-03-05 18:34:53 +00:00
$emailsendersignature = $obj -> signature ;
2017-10-11 17:41:19 +00:00
}
2023-08-02 10:05:44 +00:00
} elseif ( preg_match ( '/from_template_(\d+)/' , $fromtype , $reg )) {
2026-07-19 15:32:14 +00:00
$sql = " SELECT rowid, " . $db -> sanitize ( $email_from ) . " FROM " . MAIN_DB_PREFIX . " c_email_templates " ;
$sql .= " WHERE rowid = " . ( int ) $reg [ 1 ];
2023-08-02 10:05:44 +00:00
$resql = $db -> query ( $sql );
$obj = $db -> fetch_object ( $resql );
if ( $obj ) {
2026-07-19 15:32:14 +00:00
$email_from = $obj -> $email_from ;
2023-08-02 10:05:44 +00:00
}
2020-05-21 13:05:19 +00:00
} else {
2026-07-19 15:32:14 +00:00
$email_from = dol_string_nospecial ( GETPOST ( 'fromname' ), ' ' , array ( " , " )) . ' <' . GETPOST ( 'frommail' ) . '>' ;
2017-03-02 13:29:22 +00:00
}
2025-01-16 11:21:06 +00:00
$replyto = '' ;
2025-01-16 11:29:00 +00:00
if ( GETPOST ( 'replytomail' )) {
$replyto = dol_string_nospecial ( GETPOST ( 'replytoname' ), ' ' , array ( " , " ));
$replyto .= ( $replyto ? ' ' : '' ) . '<' . GETPOST ( 'replytomail' ) . '>' ;
2025-01-16 11:21:06 +00:00
}
2024-02-18 01:12:34 +00:00
2020-09-17 23:29:17 +00:00
$message = GETPOST ( 'message' , 'restricthtml' );
$subject = GETPOST ( 'subject' , 'restricthtml' );
2017-06-17 08:44:08 +00:00
// Make a change into HTML code to allow to include images from medias directory with an external reabable URL.
// <img alt="" src="/dolibarr_dev/htdocs/viewimage.php?modulepart=medias&entity=1&file=image/ldestailleur_166x166.jpg" style="height:166px; width:166px" />
// become
// <img alt="" src="'.$urlwithroot.'viewimage.php?modulepart=medias&entity=1&file=image/ldestailleur_166x166.jpg" style="height:166px; width:166px" />
2019-12-18 14:20:36 +00:00
$message = preg_replace ( '/(<img.*src=")[^\"]*viewimage\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^\/]*\/>)/' , '\1' . $urlwithroot . '/viewimage.php\2modulepart=medias\3file=\4\5' , $message );
2017-06-17 08:44:08 +00:00
2024-11-18 13:56:09 +00:00
$sendtobcc = GETPOST ( 'sendtoccc' , 'alphawithlgt' );
2017-09-17 13:38:50 +00:00
// Autocomplete the $sendtobcc
// $autocopy can be MAIN_MAIL_AUTOCOPY_PROPOSAL_TO, MAIN_MAIL_AUTOCOPY_ORDER_TO, MAIN_MAIL_AUTOCOPY_INVOICE_TO, MAIN_MAIL_AUTOCOPY_SUPPLIER_PROPOSAL_TO...
2021-01-02 08:35:57 +00:00
if ( ! empty ( $autocopy )) {
2024-04-08 09:31:28 +00:00
$sendtobcc .= ( getDolGlobalString ( $autocopy ) ? (( $sendtobcc ? " , " : " " ) . getDolGlobalString ( $autocopy )) : '' );
2017-09-17 13:38:50 +00:00
}
2017-01-23 07:33:05 +00:00
2024-09-13 01:09:23 +00:00
$deliveryreceipt = GETPOSTINT ( 'deliveryreceipt' ) ? 1 : 0 ;
2013-09-23 11:17:31 +00:00
2021-01-02 08:35:57 +00:00
if ( $action == 'send' || $action == 'relance' ) {
2026-07-19 15:32:14 +00:00
$actionmsg2 = $langs -> transnoentities ( 'MailSentByTo' , CMailFile :: getValidAddress ( $email_from , 4 , 0 , 1 ), CMailFile :: getValidAddress ( $sendto , 4 , 0 , 1 ));
2022-04-07 11:28:24 +00:00
/* if ( $message ) {
2026-07-19 15:32:14 +00:00
$actionmsg = $langs -> transnoentities ( 'MailFrom' ) . ': ' . dol_escape_htmltag ( $email_from );
2019-12-18 14:20:36 +00:00
$actionmsg = dol_concatdesc ( $actionmsg , $langs -> transnoentities ( 'MailTo' ) . ': ' . dol_escape_htmltag ( $sendto ));
2021-01-02 08:35:57 +00:00
if ( $sendtocc ) {
$actionmsg = dol_concatdesc ( $actionmsg , $langs -> transnoentities ( 'Bcc' ) . " : " . dol_escape_htmltag ( $sendtocc ));
}
2019-12-18 14:20:36 +00:00
$actionmsg = dol_concatdesc ( $actionmsg , $langs -> transnoentities ( 'MailTopic' ) . " : " . $subject );
$actionmsg = dol_concatdesc ( $actionmsg , $langs -> transnoentities ( 'TextUsedInTheMessageBody' ) . " : " );
2014-11-18 18:03:02 +00:00
$actionmsg = dol_concatdesc ( $actionmsg , $message );
2022-04-07 11:28:24 +00:00
} */
2013-09-23 11:17:31 +00:00
}
// Create form object
include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php' ;
$formmail = new FormMail ( $db );
2019-12-18 14:20:36 +00:00
$formmail -> trackid = $trackid ; // $trackid must be defined
2017-01-23 07:33:05 +00:00
2019-12-18 14:20:36 +00:00
$attachedfiles = $formmail -> get_attached_files ();
2013-09-23 11:17:31 +00:00
$filepath = $attachedfiles [ 'paths' ];
$filename = $attachedfiles [ 'names' ];
$mimetype = $attachedfiles [ 'mimes' ];
2015-08-24 14:22:21 +00:00
2017-09-16 01:13:44 +00:00
// Make substitution in email content
2019-12-18 14:20:36 +00:00
$substitutionarray = getCommonSubstitutionArray ( $langs , 0 , null , $object );
2025-03-05 18:34:24 +00:00
2025-03-05 18:37:47 +00:00
$substitutionarray [ '__SENDEREMAIL_SIGNATURE__' ] = ( empty ( $emailsendersignature ) ? $user -> signature : $emailsendersignature );
2017-09-16 01:13:44 +00:00
$substitutionarray [ '__EMAIL__' ] = $sendto ;
2023-06-09 09:53:39 +00:00
$substitutionarray [ '__CHECK_READ__' ] = ( is_object ( $object ) && is_object ( $object -> thirdparty )) ? '<img src="' . DOL_MAIN_URL_ROOT . '/public/emailing/mailing-read.php?tag=undefined&securitykey=' . dol_hash ( getDolGlobalString ( 'MAILING_EMAIL_UNSUBSCRIBE_KEY' ) . " -undefined " , 'md5' ) . '" width="1" height="1" style="width:1px;height:1px" border="0"/>' : '' ;
2017-09-16 01:13:44 +00:00
2024-03-13 18:43:34 +00:00
$parameters = array ( 'mode' => 'formemail' );
2017-09-16 01:13:44 +00:00
complete_substitutions_array ( $substitutionarray , $langs , $object , $parameters );
2017-08-28 10:18:18 +00:00
2019-12-18 14:20:36 +00:00
$subject = make_substitutions ( $subject , $substitutionarray );
$message = make_substitutions ( $message , $substitutionarray );
2017-08-28 10:18:18 +00:00
2021-06-09 21:09:31 +00:00
if ( is_object ( $object ) && method_exists ( $object , 'makeSubstitution' )) {
2017-09-22 18:18:11 +00:00
$subject = $object -> makeSubstitution ( $subject );
$message = $object -> makeSubstitution ( $message );
}
2017-08-28 10:18:18 +00:00
// Send mail (substitutionarray must be done just before this)
2021-01-02 08:35:57 +00:00
if ( empty ( $sendcontext )) {
$sendcontext = 'standard' ;
}
2026-07-19 15:32:14 +00:00
$mailfile = new CMailFile ( $subject , $sendto , $email_from , $message , $filepath , $mimetype , $filename , $sendtocc , $sendtobcc , $deliveryreceipt , - 1 , '' , '' , $trackid , '' , $sendcontext , $replyto , $upload_dir_tmp );
2017-08-28 10:53:02 +00:00
2022-08-04 07:14:34 +00:00
if ( ! empty ( $mailfile -> error ) || ! empty ( $mailfile -> errors )) {
2018-03-27 09:17:40 +00:00
setEventMessages ( $mailfile -> error , $mailfile -> errors , 'errors' );
2019-12-18 14:20:36 +00:00
$action = 'presend' ;
2020-05-21 13:05:19 +00:00
} else {
2019-12-18 14:20:36 +00:00
$result = $mailfile -> sendfile ();
2023-11-17 11:01:03 +00:00
if ( $result ) {
2018-05-17 14:50:40 +00:00
// Initialisation of datas of object to call trigger
2021-01-02 08:35:57 +00:00
if ( is_object ( $object )) {
2026-01-05 15:17:40 +00:00
$db -> begin (); // Transaction for post action must start after sending email to avoid lock when sending email that may be long
2021-01-02 08:35:57 +00:00
if ( empty ( $actiontypecode )) {
2026-06-08 23:37:34 +00:00
$actiontypecode = 'AC_EMAIL' ; // Event inserted into agenda automatically
2021-01-02 08:35:57 +00:00
}
2017-06-17 08:44:08 +00:00
2019-12-18 14:20:36 +00:00
$object -> socid = $sendtosocid ; // To link to a company
2020-08-23 00:38:56 +00:00
$object -> sendtoid = $sendtoid ; // To link to contact-addresses. This is an array.
2019-12-18 14:20:36 +00:00
$object -> actiontypecode = $actiontypecode ; // Type of event ('AC_OTH', 'AC_OTH_AUTO', 'AC_XXX'...)
2022-04-07 11:28:24 +00:00
$object -> actionmsg = $message ; // Long text
2024-03-28 12:31:09 +00:00
$object -> actionmsg2 = $actionmsg2 ; // Short text ($langs->transnoentities('MailSentByTo')...);
2023-11-27 10:39:32 +00:00
if ( getDolGlobalString ( 'MAIN_MAIL_REPLACE_EVENT_TITLE_BY_EMAIL_SUBJECT' )) {
2022-12-05 10:11:18 +00:00
$object -> actionmsg2 = $subject ; // Short text
}
2018-11-12 14:27:59 +00:00
2019-12-18 14:20:36 +00:00
$object -> trackid = $trackid ;
2021-01-02 08:35:57 +00:00
$object -> fk_element = $object -> id ;
$object -> elementtype = $object -> element ;
2019-12-18 14:20:36 +00:00
if ( is_array ( $attachedfiles ) && count ( $attachedfiles ) > 0 ) {
$object -> attachedfiles = $attachedfiles ;
2017-07-18 17:33:25 +00:00
}
2023-11-27 10:39:32 +00:00
if ( is_array ( $sendtouserid ) && count ( $sendtouserid ) > 0 && getDolGlobalString ( 'MAIN_MAIL_ENABLED_USER_DEST_SELECT' )) {
2019-12-18 14:20:36 +00:00
$object -> sendtouserid = $sendtouserid ;
2018-03-21 21:58:17 +00:00
}
2017-01-23 07:33:05 +00:00
2026-01-05 15:34:39 +00:00
// TODO Fix this: Such properties does not exists on all objects
$object -> context [ 'email_msgid' ] = $mailfile -> msgid ;
2026-07-19 15:32:14 +00:00
$object -> context [ '$email_from' ] = $email_from ;
2026-01-05 15:34:39 +00:00
$object -> context [ 'email_subject' ] = $subject ;
$object -> context [ 'email_to' ] = $sendto ;
$object -> context [ 'email_tocc' ] = $sendtocc ;
$object -> context [ 'email_tobcc' ] = $sendtobcc ;
2019-12-18 14:20:36 +00:00
$object -> email_msgid = $mailfile -> msgid ; // @todo Set msgid into $mailfile after sending
2026-07-19 15:32:14 +00:00
$object -> $email_from = $email_from ;
2018-11-12 14:27:59 +00:00
$object -> email_subject = $subject ;
$object -> email_to = $sendto ;
$object -> email_tocc = $sendtocc ;
$object -> email_tobcc = $sendtobcc ;
2018-11-03 21:53:55 +00:00
2025-03-05 19:25:12 +00:00
// Call of triggers (you should have set $triggersendname to execute trigger.
if ( ! empty ( $triggersendname )) {
2025-11-26 12:41:16 +00:00
if ( $triggersendname == 'BILL_SENTBYMAIL' && $object instanceof Facture ) {
2026-01-05 15:17:40 +00:00
/* @var Facture $object */
2025-11-26 12:41:16 +00:00
// If sending email for invoice, we increase the counter of invoices sent by email
$sql = " UPDATE " . MAIN_DB_PREFIX . " facture SET email_sent_counter = email_sent_counter + 1 " ;
$sql .= " WHERE rowid = " . (( int ) $object -> id );
2025-12-29 21:58:32 +00:00
$resql = $db -> query ( $sql );
if ( $resql ) {
$object -> email_sent_counter += 1 ;
}
2025-11-26 12:41:16 +00:00
}
2025-03-05 19:25:12 +00:00
$result = $object -> call_trigger ( $triggersendname , $user ); // @phan-suppress-current-line PhanPossiblyUndeclaredGlobalVariable
2021-01-02 08:35:57 +00:00
if ( $result < 0 ) {
$error ++ ;
}
2020-04-02 10:03:29 +00:00
if ( $error ) {
setEventMessages ( $object -> error , $object -> errors , 'errors' );
2020-09-07 08:18:17 +00:00
}
2017-01-23 07:33:05 +00:00
}
2020-04-02 10:03:29 +00:00
// End call of triggers
2026-01-05 15:17:40 +00:00
if ( ! $error ) {
$db -> commit ();
} else {
$db -> rollback ();
}
2013-09-23 11:17:31 +00:00
}
2017-06-17 08:44:08 +00:00
2017-12-30 11:12:13 +00:00
// Redirect here
// This avoid sending mail twice if going out and then back to page
2026-07-19 15:32:14 +00:00
$mesg = $langs -> trans ( 'MailSuccessfulySent' , $mailfile -> getValidAddress ( $email_from , 2 ), $mailfile -> getValidAddress ( $sendto , 2 ));
2017-12-30 11:12:13 +00:00
setEventMessages ( $mesg , null , 'mesgs' );
2018-02-03 16:32:38 +00:00
2025-03-05 19:25:12 +00:00
header ( 'Location: ' . $_SERVER [ " PHP_SELF " ] . '?' . ( $paramname ? ? 'id' ) . '=' . ( is_object ( $object ) ? $object -> id : '' ));
2021-01-02 08:35:57 +00:00
exit ;
2020-05-21 13:05:19 +00:00
} else {
2013-09-23 11:17:31 +00:00
$langs -> load ( " other " );
2019-12-18 14:20:36 +00:00
$mesg = '<div class="error">' ;
2022-08-10 08:09:52 +00:00
if ( ! empty ( $mailfile -> error ) || ! empty ( $mailfile -> errors )) {
2026-07-19 15:32:14 +00:00
$mesg .= $langs -> transnoentities ( 'ErrorFailedToSendMail' , dol_escape_htmltag ( $email_from ), dol_escape_htmltag ( $sendto ));
2022-08-10 08:09:52 +00:00
if ( ! empty ( $mailfile -> error )) {
$mesg .= '<br>' . $mailfile -> error ;
}
if ( ! empty ( $mailfile -> errors ) && is_array ( $mailfile -> errors )) {
$mesg .= '<br>' . implode ( '<br>' , $mailfile -> errors );
}
2020-05-21 13:05:19 +00:00
} else {
2026-07-19 15:32:14 +00:00
$mesg .= $langs -> transnoentities ( 'ErrorFailedToSendMail' , dol_escape_htmltag ( $email_from ), dol_escape_htmltag ( $sendto ));
2023-11-27 10:39:32 +00:00
if ( getDolGlobalString ( 'MAIN_DISABLE_ALL_MAILS' )) {
2020-10-23 14:39:45 +00:00
$mesg .= '<br>Feature is disabled by option MAIN_DISABLE_ALL_MAILS' ;
} else {
2024-01-13 18:48:20 +00:00
$mesg .= '<br>Unknown Error, please refer to your administrator' ;
2020-10-23 14:39:45 +00:00
}
2013-09-23 11:17:31 +00:00
}
2019-12-18 14:20:36 +00:00
$mesg .= '</div>' ;
2015-08-24 14:05:15 +00:00
2015-11-19 12:59:58 +00:00
setEventMessages ( $mesg , null , 'warnings' );
2013-09-23 11:17:31 +00:00
$action = 'presend' ;
}
}
2020-05-21 13:05:19 +00:00
} else {
2013-09-23 11:17:31 +00:00
$langs -> load ( " errors " );
2019-01-27 10:55:16 +00:00
setEventMessages ( $langs -> trans ( 'ErrorFieldRequired' , $langs -> transnoentitiesnoconv ( " MailTo " )), null , 'warnings' );
2017-06-22 09:04:29 +00:00
dol_syslog ( 'Try to send email with no recipient defined' , LOG_WARNING );
2013-09-23 11:17:31 +00:00
$action = 'presend' ;
}
2020-05-21 13:05:19 +00:00
} else {
2020-08-23 00:38:56 +00:00
$langs -> load ( " errors " );
2019-01-27 10:55:16 +00:00
setEventMessages ( $langs -> trans ( 'ErrorFailedToReadObject' , $object -> element ), null , 'errors' );
2013-09-23 11:17:31 +00:00
dol_syslog ( 'Failed to read data of object id=' . $object -> id . ' element=' . $object -> element );
$action = 'presend' ;
}
}