2013-09-23 13:17:31 +02:00
< ? php
/* Copyright ( C ) 2013 Laurent Destailleur < eldy @ users . sourceforge . net >
2024-03-13 19:43:34 +01:00
* Copyright ( C ) 2024 MDW < mdeweerd @ users . noreply . github . com >
2024-11-04 23:53:20 +01:00
* Copyright ( C ) 2024 Frédéric France < frederic . france @ free . fr >
2018-12-01 16:55:45 +01:00
* Copyright ( C ) 2013 Juanjo Menent < jmenent @ 2 byte . es >
*
2017-03-02 14:29:22 +01: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 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
* or see https :// www . gnu . org /
2017-03-02 14:29:22 +01:00
*/
2013-09-23 13:17:31 +02:00
/**
* \file htdocs / core / actions_sendmails . inc . php
2017-09-22 17:48:38 +02:00
* \brief Code for actions on sending mails from object page
*/
2013-09-23 13:17:31 +02:00
2016-09-26 14:08:14 +02:00
// $mysoc must be defined
2013-09-23 13:17:31 +02:00
// $id must be defined
2018-04-20 01:39:26 +02:00
// $paramname may be defined
// $autocopy may be defined (used to know the automatic BCC to add)
2019-12-02 09:38:16 +01:00
// $triggersendname must be set (can be '')
2017-05-19 12:58:38 +02:00
// $actiontypecode can be set
2024-11-04 23:53:20 +01:00
// $object and $subject may be defined
/**
* @ var CommonObject $object
* @ var Conf $conf
* @ var DoliDB $db
2024-11-10 11:16:23 +01:00
* @ var HookManager $hookmanager
2024-11-04 23:53:20 +01:00
* @ var Societe $mysoc
* @ var Translate $langs
*
2024-11-06 19:39:46 +01:00
* @ var string $dolibarr_main_url_root
2024-11-04 23:53:20 +01:00
* @ var string $action
* @ var ? string $subject
*/
2024-03-13 19:43:34 +01:00
'
@ phan - var - force Societe $mysoc
@ phan - var - force CommonObject $object
' ;
2013-09-23 13:17:31 +02:00
2024-11-10 11:16:23 +01:00
$error = 0 ;
2013-09-23 13:17:31 +02:00
/*
* Add file in email form
2017-06-17 10:44:08 +02:00
*/
2021-01-02 09:35:57 +01:00
if ( GETPOST ( 'addfile' , 'alpha' )) {
2019-01-27 11:55:16 +01:00
$trackid = GETPOST ( 'trackid' , 'aZ09' );
2017-01-23 08:33:05 +01:00
2017-03-02 14:29:22 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2013-09-23 13:17:31 +02:00
// Set tmp user directory
2019-12-18 15:20:36 +01:00
$vardir = $conf -> user -> dir_output . " / " . $user -> id ;
$upload_dir_tmp = $vardir . '/temp' ; // TODO Add $keytoavoidconflict in upload_dir path
2013-09-23 13:17:31 +02:00
2019-05-19 02:03:16 +02:00
dol_add_file_process ( $upload_dir_tmp , 1 , 0 , 'addedfile' , '' , null , $trackid , 0 );
2019-12-18 15:20:36 +01:00
$action = 'presend' ;
2013-09-23 13:17:31 +02:00
}
/*
* Remove file in email form
*/
2022-02-22 23:44:56 +01:00
if ( GETPOST ( 'removedfile' ) && ! GETPOST ( 'removAll' )) {
2019-01-27 11:55:16 +01:00
$trackid = GETPOST ( 'trackid' , 'aZ09' );
2017-01-23 08:33:05 +01:00
2013-09-23 13:17:31 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
// Set tmp user directory
2019-12-18 15:20:36 +01:00
$vardir = $conf -> user -> dir_output . " / " . $user -> id ;
$upload_dir_tmp = $vardir . '/temp' ; // TODO Add $keytoavoidconflict in upload_dir path
2013-09-23 13:17:31 +02:00
2016-06-24 16:10:52 +02: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.
2019-12-18 15:20:36 +01:00
dol_remove_file_process ( GETPOST ( 'removedfile' , 'alpha' ), 0 , 1 , $trackid ); // We do not delete because if file is the official PDF of doc, we don't want to remove it physically
$action = 'presend' ;
2013-09-23 13:17:31 +02:00
}
2015-08-24 16:05:15 +02:00
/*
* Remove all files in email form
*/
2021-01-02 09:35:57 +01:00
if ( GETPOST ( 'removAll' , 'alpha' )) {
2019-01-27 11:55:16 +01:00
$trackid = GETPOST ( 'trackid' , 'aZ09' );
2017-01-23 08:33:05 +01:00
2019-12-18 15:20:36 +01:00
$listofpaths = array ();
$listofnames = array ();
$listofmimes = array ();
$keytoavoidconflict = empty ( $trackid ) ? '' : '-' . $trackid ;
2021-01-02 09:35:57 +01: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 16:05:15 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php' ;
$formmail = new FormMail ( $db );
2016-06-24 16:10:52 +02:00
$formmail -> trackid = $trackid ;
2017-01-23 08:33:05 +01:00
2021-01-02 09:35:57 +01:00
foreach ( $listofpaths as $key => $value ) {
2015-08-24 16:05:15 +02:00
$pathtodelete = $value ;
$filetodelete = $listofnames [ $key ];
2024-01-13 19:48:20 +01:00
$result = dol_delete_file ( $pathtodelete , 1 ); // Delete uploaded Files
2016-09-20 12:19:17 +02:00
2015-08-24 16:05:15 +02:00
$langs -> load ( " other " );
2019-01-27 11:55:16 +01:00
setEventMessages ( $langs -> trans ( " FileWasRemoved " , $filetodelete ), null , 'mesgs' );
2016-09-20 12:19:17 +02:00
2015-08-24 16:05:15 +02:00
$formmail -> remove_attached_files ( $key ); // Update Session
}
}
2013-09-23 13:17:31 +02:00
/*
* Send mail
*/
2022-02-22 23:44:56 +01:00
if (( $action == 'send' || $action == 'relance' ) && ! GETPOST ( 'addfile' ) && ! GETPOST ( 'removAll' ) && ! GETPOST ( 'removedfile' ) && ! GETPOST ( 'cancel' ) && ! GETPOST ( 'modelselected' )) {
2021-01-02 09:35:57 +01:00
if ( empty ( $trackid )) {
$trackid = GETPOST ( 'trackid' , 'aZ09' );
}
2018-01-22 17:15:44 +01:00
2022-10-23 17:14:23 +02: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 20:37:16 +01:00
$subject = '' ;
2022-04-07 13:28:24 +02:00
//$actionmsg = '';
2021-03-01 20:37:16 +01:00
$actionmsg2 = '' ;
2017-01-23 08:33:05 +01:00
2013-09-23 13:17:31 +02:00
$langs -> load ( 'mails' );
2024-11-19 03:07:18 +01:00
$sendtosocid = 0 ; // Id of related thirdparty
2021-01-02 09:35:57 +01:00
if ( is_object ( $object )) {
2019-12-18 15:20:36 +01:00
$result = $object -> fetch ( $id );
2017-03-02 14:29:22 +01:00
2021-01-02 09:35:57 +01:00
if ( method_exists ( $object , " fetch_thirdparty " ) && ! in_array ( $object -> element , array ( 'member' , 'user' , 'expensereport' , 'societe' , 'contact' ))) {
2020-08-23 02:38:56 +02:00
$resultthirdparty = $object -> fetch_thirdparty ();
2019-12-18 15:20:36 +01:00
$thirdparty = $object -> thirdparty ;
2021-01-02 09:35:57 +01:00
if ( is_object ( $thirdparty )) {
$sendtosocid = $thirdparty -> id ;
}
} elseif ( $object -> element == 'member' || $object -> element == 'user' ) {
2019-12-18 15:20:36 +01:00
$thirdparty = $object ;
2021-01-02 09:35:57 +01:00
if ( $object -> socid > 0 ) {
$sendtosocid = $object -> socid ;
}
} elseif ( $object -> element == 'expensereport' ) {
2019-12-18 15:20:36 +01:00
$tmpuser = new User ( $db );
2019-09-23 12:36:12 +02:00
$tmpuser -> fetch ( $object -> fk_user_author );
2019-12-18 15:20:36 +01:00
$thirdparty = $tmpuser ;
2021-01-02 09:35:57 +01:00
if ( $object -> socid > 0 ) {
$sendtosocid = $object -> socid ;
}
} elseif ( $object -> element == 'societe' ) {
2019-12-18 15:20:36 +01:00
$thirdparty = $object ;
2021-01-02 09:35:57 +01:00
if ( is_object ( $thirdparty ) && $thirdparty -> id > 0 ) {
$sendtosocid = $thirdparty -> id ;
}
} elseif ( $object -> element == 'contact' ) {
2019-12-18 15:20:36 +01:00
$contact = $object ;
2020-08-23 02:38:56 +02:00
if ( $contact -> id > 0 ) {
$contact -> fetch_thirdparty ();
$thirdparty = $contact -> thirdparty ;
2021-01-02 09:35:57 +01:00
if ( is_object ( $thirdparty ) && $thirdparty -> id > 0 ) {
$sendtosocid = $thirdparty -> id ;
}
2020-08-23 02:38:56 +02:00
}
2021-01-02 09:35:57 +01:00
} else {
2024-01-20 09:22:38 +01:00
dol_print_error ( null , " Use actions_sendmails.in.php for an element/object ' " . $object -> element . " ' that is not supported " );
2021-01-02 09:35:57 +01:00
}
2018-04-11 18:31:46 +02:00
2021-01-02 09:35:57 +01:00
if ( is_object ( $hookmanager )) {
2019-12-18 15:20:36 +01: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 18:31:46 +02:00
}
2020-08-23 02:38:56 +02:00
} else {
$thirdparty = $mysoc ;
}
2013-09-23 13:17:31 +02:00
2021-01-02 09:35:57 +01:00
if ( $result > 0 ) {
2019-12-18 15:20:36 +01:00
$sendto = '' ;
$sendtocc = '' ;
$sendtobcc = '' ;
2017-02-22 17:46:54 +01:00
$sendtoid = array ();
2019-12-18 15:20:36 +01:00
$sendtouserid = array ();
$sendtoccuserid = array ();
2017-02-22 17:46:54 +01:00
// Define $sendto
2022-02-22 23:44:56 +01:00
$receiver = GETPOST ( 'receiver' , 'alphawithlgt' );
2021-01-02 09:35:57 +01:00
if ( ! is_array ( $receiver )) {
if ( $receiver == '-1' ) {
$receiver = array ();
} else {
$receiver = array ( $receiver );
}
2017-02-22 17:46:54 +01:00
}
2019-09-23 12:36:12 +02:00
2019-12-18 15:20:36 +01:00
$tmparray = array ();
2024-03-28 21:29:02 +01:00
if ( trim ( GETPOST ( 'sendto' , 'alphawithlgt' ))) {
2021-07-31 16:54:18 +02:00
// Recipients are provided into free text field
2022-02-22 23:44:56 +01:00
$tmparray [] = trim ( GETPOST ( 'sendto' , 'alphawithlgt' ));
2013-09-23 13:17:31 +02:00
}
2020-08-23 02:38:56 +02:00
2024-03-28 21:29:02 +01:00
if ( trim ( GETPOST ( 'tomail' , 'alphawithlgt' ))) {
2021-07-31 16:54:18 +02:00
// Recipients are provided into free hidden text field
2022-02-22 23:44:56 +01:00
$tmparray [] = trim ( GETPOST ( 'tomail' , 'alphawithlgt' ));
2021-07-29 17:32:06 +02:00
}
2021-01-02 09:35:57 +01:00
if ( count ( $receiver ) > 0 ) {
2020-08-23 02:38:56 +02:00
// Recipient was provided from combo list
2021-01-02 09:35:57 +01: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)
2019-09-23 12:36:12 +02:00
$tmparray [] = dol_string_nospecial ( $thirdparty -> getFullName ( $langs ), ' ' , array ( " , " )) . ' <' . $thirdparty -> email . '>' ;
2021-01-02 09:35:57 +01:00
} elseif ( $val == 'contact' ) { // Key selected means current contact
2019-09-23 12:36:12 +02:00
$tmparray [] = dol_string_nospecial ( $contact -> getFullName ( $langs ), ' ' , array ( " , " )) . ' <' . $contact -> email . '>' ;
2020-08-23 02:38:56 +02:00
$sendtoid [] = $contact -> id ;
2024-12-26 11:15:22 +01: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 09:35:57 +01:00
} elseif ( $val ) { // $val is the Id of a contact
2019-01-27 11:55:16 +01:00
$tmparray [] = $thirdparty -> contact_get_property (( int ) $val , 'email' );
2020-08-23 02:38:56 +02:00
$sendtoid [] = (( int ) $val );
2017-02-15 11:41:09 +01:00
}
2013-09-23 13:17:31 +02:00
}
}
2019-09-23 12:36:12 +02:00
2023-11-27 11:39:32 +01:00
if ( getDolGlobalString ( 'MAIN_MAIL_ENABLED_USER_DEST_SELECT' )) {
2022-02-22 23:44:56 +01:00
$receiveruser = GETPOST ( 'receiveruser' , 'alphawithlgt' );
2021-01-02 09:35:57 +01:00
if ( is_array ( $receiveruser ) && count ( $receiveruser ) > 0 ) {
2018-03-21 11:48:30 +01:00
$fuserdest = new User ( $db );
2021-01-02 09:35:57 +01:00
foreach ( $receiveruser as $key => $val ) {
2019-01-27 11:55:16 +01:00
$tmparray [] = $fuserdest -> user_get_property ( $val , 'email' );
2018-05-22 20:17:31 +02:00
$sendtouserid [] = $val ;
2018-03-21 11:48:30 +01:00
}
}
}
2018-05-22 20:17:31 +02:00
2019-12-18 15:20:36 +01:00
$sendto = implode ( ',' , $tmparray );
2017-03-02 14:29:22 +01:00
2017-02-22 17:46:54 +01:00
// Define $sendtocc
2022-02-22 23:44:56 +01:00
$receivercc = GETPOST ( 'receivercc' , 'alphawithlgt' );
2021-01-02 09:35:57 +01:00
if ( ! is_array ( $receivercc )) {
if ( $receivercc == '-1' ) {
$receivercc = array ();
} else {
$receivercc = array ( $receivercc );
}
2017-02-22 17:46:54 +01:00
}
2019-12-18 15:20:36 +01:00
$tmparray = array ();
2024-03-28 21:29:02 +01:00
if ( trim ( GETPOST ( 'sendtocc' , 'alphawithlgt' ))) {
2022-02-22 23:44:56 +01:00
$tmparray [] = trim ( GETPOST ( 'sendtocc' , 'alphawithlgt' ));
2014-11-04 13:54:18 +01:00
}
2021-01-02 09:35:57 +01:00
if ( count ( $receivercc ) > 0 ) {
foreach ( $receivercc as $key => $val ) {
2021-01-12 23:30:59 +01:00
if ( $val == 'thirdparty' ) { // Key selected means current thirdparty (may be usd for current member or current user too)
2021-01-02 09:35:57 +01:00
// Recipient was provided from combo list
2018-04-10 11:15:21 +02:00
$tmparray [] = dol_string_nospecial ( $thirdparty -> name , ' ' , array ( " , " )) . ' <' . $thirdparty -> email . '>' ;
2021-01-02 09:35:57 +01:00
} elseif ( $val == 'contact' ) { // Key selected means current contact
// Recipient was provided from combo list
2018-11-06 16:40:39 +01:00
$tmparray [] = dol_string_nospecial ( $contact -> name , ' ' , array ( " , " )) . ' <' . $contact -> email . '>' ;
2020-08-23 02:38:56 +02:00
//$sendtoid[] = $contact->id; TODO Add also id of contact in CC ?
2021-01-02 09:35:57 +01:00
} elseif ( $val ) { // $val is the Id of a contact
2019-01-27 11:55:16 +01:00
$tmparray [] = $thirdparty -> contact_get_property (( int ) $val , 'email' );
2020-08-23 02:38:56 +02:00
//$sendtoid[] = ((int) $val); TODO Add also id of contact in CC ?
2017-02-22 17:46:54 +01:00
}
2014-11-04 13:54:18 +01:00
}
}
2023-11-27 11:39:32 +01:00
if ( getDolGlobalString ( 'MAIN_MAIL_ENABLED_USER_DEST_SELECT' )) {
2022-02-22 23:44:56 +01:00
$receiverccuser = GETPOST ( 'receiverccuser' , 'alphawithlgt' );
2018-03-21 11:48:30 +01:00
2021-01-02 09:35:57 +01:00
if ( is_array ( $receiverccuser ) && count ( $receiverccuser ) > 0 ) {
2018-03-21 11:48:30 +01:00
$fuserdest = new User ( $db );
2021-01-02 09:35:57 +01:00
foreach ( $receiverccuser as $key => $val ) {
2019-01-27 11:55:16 +01:00
$tmparray [] = $fuserdest -> user_get_property ( $val , 'email' );
2018-05-22 20:17:31 +02:00
$sendtoccuserid [] = $val ;
2018-03-21 11:48:30 +01:00
}
}
}
2019-12-18 15:20:36 +01:00
$sendtocc = implode ( ',' , $tmparray );
2013-09-23 13:17:31 +02:00
2021-01-02 09:35:57 +01:00
if ( dol_strlen ( $sendto )) {
2020-09-07 10:18:17 +02: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 10:44:08 +02:00
2020-09-07 10:18:17 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/CMailFile.class.php' ;
2017-01-23 08:33:05 +01:00
2013-09-23 13:17:31 +02:00
$langs -> load ( " commercial " );
2020-01-20 18:38:53 +01:00
$reg = array ();
2019-01-27 11:55:16 +01:00
$fromtype = GETPOST ( 'fromtype' , 'alpha' );
2017-11-25 18:52:47 +01:00
if ( $fromtype === 'robot' ) {
2023-10-15 15:32:35 +02:00
$from = dol_string_nospecial ( $conf -> global -> MAIN_MAIL_EMAIL_FROM , ' ' , array ( " , " )) . ' <' . getDolGlobalString ( 'MAIN_MAIL_EMAIL_FROM' ) . '>' ;
2020-05-21 15:05:19 +02:00
} elseif ( $fromtype === 'user' ) {
2019-12-18 15:20:36 +01:00
$from = dol_string_nospecial ( $user -> getFullName ( $langs ), ' ' , array ( " , " )) . ' <' . $user -> email . '>' ;
2020-05-21 15:05:19 +02:00
} elseif ( $fromtype === 'company' ) {
2023-10-15 15:32:35 +02:00
$from = dol_string_nospecial ( $conf -> global -> MAIN_INFO_SOCIETE_NOM , ' ' , array ( " , " )) . ' <' . getDolGlobalString ( 'MAIN_INFO_SOCIETE_MAIL' ) . '>' ;
2020-05-21 15:05:19 +02:00
} elseif ( preg_match ( '/user_aliases_(\d+)/' , $fromtype , $reg )) {
2019-12-18 15:20:36 +01:00
$tmp = explode ( ',' , $user -> email_aliases );
2024-08-15 16:57:02 +02:00
$from = trim ( $tmp [(( int ) $reg [ 1 ] - 1 )]);
2020-05-21 15:05:19 +02:00
} elseif ( preg_match ( '/global_aliases_(\d+)/' , $fromtype , $reg )) {
2023-12-13 15:20:53 +01:00
$tmp = explode ( ',' , getDolGlobalString ( 'MAIN_INFO_SOCIETE_MAIL_ALIASES' ));
2024-08-15 16:57:02 +02:00
$from = trim ( $tmp [(( int ) $reg [ 1 ] - 1 )]);
2020-05-21 15:05:19 +02:00
} elseif ( preg_match ( '/senderprofile_(\d+)_(\d+)/' , $fromtype , $reg )) {
2020-01-20 18:38:53 +01:00
$sql = 'SELECT rowid, label, email FROM ' . MAIN_DB_PREFIX . 'c_email_senderprofile' ;
$sql .= ' WHERE rowid = ' . ( int ) $reg [ 1 ];
2017-10-11 19:41:19 +02:00
$resql = $db -> query ( $sql );
$obj = $db -> fetch_object ( $resql );
2021-01-02 09:35:57 +01:00
if ( $obj ) {
2018-04-10 11:15:21 +02:00
$from = dol_string_nospecial ( $obj -> label , ' ' , array ( " , " )) . ' <' . $obj -> email . '>' ;
2017-10-11 19:41:19 +02:00
}
2023-08-02 12:05:44 +02:00
} elseif ( preg_match ( '/from_template_(\d+)/' , $fromtype , $reg )) {
$sql = 'SELECT rowid, email_from FROM ' . MAIN_DB_PREFIX . 'c_email_templates' ;
$sql .= ' WHERE rowid = ' . ( int ) $reg [ 1 ];
$resql = $db -> query ( $sql );
$obj = $db -> fetch_object ( $resql );
if ( $obj ) {
$from = $obj -> email_from ;
}
2020-05-21 15:05:19 +02:00
} else {
2022-02-22 23:44:56 +01:00
$from = dol_string_nospecial ( GETPOST ( 'fromname' ), ' ' , array ( " , " )) . ' <' . GETPOST ( 'frommail' ) . '>' ;
2017-03-02 14:29:22 +01:00
}
2022-02-22 23:44:56 +01:00
$replyto = dol_string_nospecial ( GETPOST ( 'replytoname' ), ' ' , array ( " , " )) . ' <' . GETPOST ( 'replytomail' ) . '>' ;
2024-02-18 02:12:34 +01:00
2020-09-18 01:29:17 +02:00
$message = GETPOST ( 'message' , 'restricthtml' );
$subject = GETPOST ( 'subject' , 'restricthtml' );
2017-06-17 10:44:08 +02: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 15:20:36 +01:00
$message = preg_replace ( '/(<img.*src=")[^\"]*viewimage\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^\/]*\/>)/' , '\1' . $urlwithroot . '/viewimage.php\2modulepart=medias\3file=\4\5' , $message );
2017-06-17 10:44:08 +02:00
2024-11-18 14:56:09 +01:00
$sendtobcc = GETPOST ( 'sendtoccc' , 'alphawithlgt' );
2017-09-17 15:38:50 +02: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 09:35:57 +01:00
if ( ! empty ( $autocopy )) {
2024-04-08 11:31:28 +02:00
$sendtobcc .= ( getDolGlobalString ( $autocopy ) ? (( $sendtobcc ? " , " : " " ) . getDolGlobalString ( $autocopy )) : '' );
2017-09-17 15:38:50 +02:00
}
2017-01-23 08:33:05 +01:00
2024-09-13 03:09:23 +02:00
$deliveryreceipt = GETPOSTINT ( 'deliveryreceipt' ) ? 1 : 0 ;
2013-09-23 13:17:31 +02:00
2021-01-02 09:35:57 +01:00
if ( $action == 'send' || $action == 'relance' ) {
2023-12-12 09:45:26 +01:00
$actionmsg2 = $langs -> transnoentities ( 'MailSentByTo' , CMailFile :: getValidAddress ( $from , 4 , 0 , 1 ), CMailFile :: getValidAddress ( $sendto , 4 , 0 , 1 ));
2022-04-07 13:28:24 +02:00
/* if ( $message ) {
2019-12-18 15:20:36 +01:00
$actionmsg = $langs -> transnoentities ( 'MailFrom' ) . ': ' . dol_escape_htmltag ( $from );
$actionmsg = dol_concatdesc ( $actionmsg , $langs -> transnoentities ( 'MailTo' ) . ': ' . dol_escape_htmltag ( $sendto ));
2021-01-02 09:35:57 +01:00
if ( $sendtocc ) {
$actionmsg = dol_concatdesc ( $actionmsg , $langs -> transnoentities ( 'Bcc' ) . " : " . dol_escape_htmltag ( $sendtocc ));
}
2019-12-18 15:20:36 +01:00
$actionmsg = dol_concatdesc ( $actionmsg , $langs -> transnoentities ( 'MailTopic' ) . " : " . $subject );
$actionmsg = dol_concatdesc ( $actionmsg , $langs -> transnoentities ( 'TextUsedInTheMessageBody' ) . " : " );
2014-11-18 19:03:02 +01:00
$actionmsg = dol_concatdesc ( $actionmsg , $message );
2022-04-07 13:28:24 +02:00
} */
2013-09-23 13:17:31 +02:00
}
// Create form object
include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php' ;
$formmail = new FormMail ( $db );
2019-12-18 15:20:36 +01:00
$formmail -> trackid = $trackid ; // $trackid must be defined
2017-01-23 08:33:05 +01:00
2019-12-18 15:20:36 +01:00
$attachedfiles = $formmail -> get_attached_files ();
2013-09-23 13:17:31 +02:00
$filepath = $attachedfiles [ 'paths' ];
$filename = $attachedfiles [ 'names' ];
$mimetype = $attachedfiles [ 'mimes' ];
2015-08-24 16:22:21 +02:00
2017-09-16 03:13:44 +02:00
// Make substitution in email content
2019-12-18 15:20:36 +01:00
$substitutionarray = getCommonSubstitutionArray ( $langs , 0 , null , $object );
2017-09-16 03:13:44 +02:00
$substitutionarray [ '__EMAIL__' ] = $sendto ;
2023-06-09 11:53:39 +02: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 03:13:44 +02:00
2024-03-13 19:43:34 +01:00
$parameters = array ( 'mode' => 'formemail' );
2017-09-16 03:13:44 +02:00
complete_substitutions_array ( $substitutionarray , $langs , $object , $parameters );
2017-08-28 12:18:18 +02:00
2019-12-18 15:20:36 +01:00
$subject = make_substitutions ( $subject , $substitutionarray );
$message = make_substitutions ( $message , $substitutionarray );
2017-08-28 12:18:18 +02:00
2021-06-09 23:09:31 +02:00
if ( is_object ( $object ) && method_exists ( $object , 'makeSubstitution' )) {
2017-09-22 20:18:11 +02:00
$subject = $object -> makeSubstitution ( $subject );
$message = $object -> makeSubstitution ( $message );
}
2017-08-28 12:18:18 +02:00
// Send mail (substitutionarray must be done just before this)
2021-01-02 09:35:57 +01:00
if ( empty ( $sendcontext )) {
$sendcontext = 'standard' ;
}
2022-10-23 17:14:23 +02:00
$mailfile = new CMailFile ( $subject , $sendto , $from , $message , $filepath , $mimetype , $filename , $sendtocc , $sendtobcc , $deliveryreceipt , - 1 , '' , '' , $trackid , '' , $sendcontext , '' , $upload_dir_tmp );
2017-08-28 12:53:02 +02:00
2022-08-04 09:14:34 +02:00
if ( ! empty ( $mailfile -> error ) || ! empty ( $mailfile -> errors )) {
2018-03-27 11:17:40 +02:00
setEventMessages ( $mailfile -> error , $mailfile -> errors , 'errors' );
2019-12-18 15:20:36 +01:00
$action = 'presend' ;
2020-05-21 15:05:19 +02:00
} else {
2019-12-18 15:20:36 +01:00
$result = $mailfile -> sendfile ();
2023-11-17 12:01:03 +01:00
if ( $result ) {
2018-05-17 16:50:40 +02:00
// Initialisation of datas of object to call trigger
2021-01-02 09:35:57 +01:00
if ( is_object ( $object )) {
if ( empty ( $actiontypecode )) {
2024-04-29 17:50:58 +02:00
$actiontypecode = 'AC_OTH_AUTO' ; // Event inserted into agenda automatically
2021-01-02 09:35:57 +01:00
}
2017-06-17 10:44:08 +02:00
2019-12-18 15:20:36 +01:00
$object -> socid = $sendtosocid ; // To link to a company
2020-08-23 02:38:56 +02:00
$object -> sendtoid = $sendtoid ; // To link to contact-addresses. This is an array.
2019-12-18 15:20:36 +01:00
$object -> actiontypecode = $actiontypecode ; // Type of event ('AC_OTH', 'AC_OTH_AUTO', 'AC_XXX'...)
2022-04-07 13:28:24 +02:00
$object -> actionmsg = $message ; // Long text
2024-03-28 13:31:09 +01:00
$object -> actionmsg2 = $actionmsg2 ; // Short text ($langs->transnoentities('MailSentByTo')...);
2023-11-27 11:39:32 +01:00
if ( getDolGlobalString ( 'MAIN_MAIL_REPLACE_EVENT_TITLE_BY_EMAIL_SUBJECT' )) {
2022-12-05 11:11:18 +01:00
$object -> actionmsg2 = $subject ; // Short text
}
2018-11-12 15:27:59 +01:00
2019-12-18 15:20:36 +01:00
$object -> trackid = $trackid ;
2021-01-02 09:35:57 +01:00
$object -> fk_element = $object -> id ;
$object -> elementtype = $object -> element ;
2019-12-18 15:20:36 +01:00
if ( is_array ( $attachedfiles ) && count ( $attachedfiles ) > 0 ) {
$object -> attachedfiles = $attachedfiles ;
2017-07-18 19:33:25 +02:00
}
2023-11-27 11:39:32 +01:00
if ( is_array ( $sendtouserid ) && count ( $sendtouserid ) > 0 && getDolGlobalString ( 'MAIN_MAIL_ENABLED_USER_DEST_SELECT' )) {
2019-12-18 15:20:36 +01:00
$object -> sendtouserid = $sendtouserid ;
2018-03-21 22:58:17 +01:00
}
2017-01-23 08:33:05 +01:00
2019-12-18 15:20:36 +01:00
$object -> email_msgid = $mailfile -> msgid ; // @todo Set msgid into $mailfile after sending
2018-11-12 15:27:59 +01:00
$object -> email_from = $from ;
$object -> email_subject = $subject ;
$object -> email_to = $sendto ;
$object -> email_tocc = $sendtocc ;
$object -> email_tobcc = $sendtobcc ;
$object -> email_subject = $subject ;
2018-11-03 22:53:55 +01:00
2020-04-02 12:03:29 +02:00
// Call of triggers (you should have set $triggersendname to execute trigger. $trigger_name is deprecated)
2021-01-02 09:35:57 +01:00
if ( ! empty ( $triggersendname ) || ! empty ( $trigger_name )) {
2020-04-02 12:03:29 +02:00
// Call trigger
2024-11-19 03:07:18 +01:00
$result = $object -> call_trigger ( empty ( $triggersendname ) ? $trigger_name : $triggersendname , $user ); // @phan-suppress-current-line PhanPossiblyUndeclaredGlobalVariable
2021-01-02 09:35:57 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2020-04-02 12:03:29 +02:00
// End call triggers
if ( $error ) {
setEventMessages ( $object -> error , $object -> errors , 'errors' );
2020-09-07 10:18:17 +02:00
}
2017-01-23 08:33:05 +01:00
}
2020-04-02 12:03:29 +02:00
// End call of triggers
2013-09-23 13:17:31 +02:00
}
2017-06-17 10:44:08 +02:00
2017-12-30 12:12:13 +01:00
// Redirect here
// This avoid sending mail twice if going out and then back to page
2019-12-18 15:20:36 +01:00
$mesg = $langs -> trans ( 'MailSuccessfulySent' , $mailfile -> getValidAddress ( $from , 2 ), $mailfile -> getValidAddress ( $sendto , 2 ));
2017-12-30 12:12:13 +01:00
setEventMessages ( $mesg , null , 'mesgs' );
2018-02-03 17:32:38 +01:00
2021-01-02 09:35:57 +01:00
$moreparam = '' ;
2024-11-19 03:07:18 +01:00
if ( isset ( $paramval2 )) { // @phan-var-suppress-current-line PhanPluginUndeclaredVariableIsset
// @phan-var-suppress-next-line PhanUndeclaredGlobalVariable
2021-01-02 09:35:57 +01:00
$moreparam .= '&' . ( $paramname2 ? $paramname2 : 'mid' ) . '=' . $paramval2 ;
}
2024-03-08 22:50:15 +01:00
header ( 'Location: ' . $_SERVER [ " PHP_SELF " ] . '?' . ( $paramname ? ? 'id' ) . '=' . ( is_object ( $object ) ? $object -> id : '' ) . $moreparam );
2021-01-02 09:35:57 +01:00
exit ;
2020-05-21 15:05:19 +02:00
} else {
2013-09-23 13:17:31 +02:00
$langs -> load ( " other " );
2019-12-18 15:20:36 +01:00
$mesg = '<div class="error">' ;
2022-08-10 10:09:52 +02:00
if ( ! empty ( $mailfile -> error ) || ! empty ( $mailfile -> errors )) {
2019-12-18 15:20:36 +01:00
$mesg .= $langs -> transnoentities ( 'ErrorFailedToSendMail' , dol_escape_htmltag ( $from ), dol_escape_htmltag ( $sendto ));
2022-08-10 10:09:52 +02: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 15:05:19 +02:00
} else {
2020-10-28 17:48:20 +01:00
$mesg .= $langs -> transnoentities ( 'ErrorFailedToSendMail' , dol_escape_htmltag ( $from ), dol_escape_htmltag ( $sendto ));
2023-11-27 11:39:32 +01:00
if ( getDolGlobalString ( 'MAIN_DISABLE_ALL_MAILS' )) {
2020-10-23 16:39:45 +02:00
$mesg .= '<br>Feature is disabled by option MAIN_DISABLE_ALL_MAILS' ;
} else {
2024-01-13 19:48:20 +01:00
$mesg .= '<br>Unknown Error, please refer to your administrator' ;
2020-10-23 16:39:45 +02:00
}
2013-09-23 13:17:31 +02:00
}
2019-12-18 15:20:36 +01:00
$mesg .= '</div>' ;
2015-08-24 16:05:15 +02:00
2015-11-19 13:59:58 +01:00
setEventMessages ( $mesg , null , 'warnings' );
2013-09-23 13:17:31 +02:00
$action = 'presend' ;
}
}
2020-05-21 15:05:19 +02:00
} else {
2013-09-23 13:17:31 +02:00
$langs -> load ( " errors " );
2019-01-27 11:55:16 +01:00
setEventMessages ( $langs -> trans ( 'ErrorFieldRequired' , $langs -> transnoentitiesnoconv ( " MailTo " )), null , 'warnings' );
2017-06-22 11:04:29 +02:00
dol_syslog ( 'Try to send email with no recipient defined' , LOG_WARNING );
2013-09-23 13:17:31 +02:00
$action = 'presend' ;
}
2020-05-21 15:05:19 +02:00
} else {
2020-08-23 02:38:56 +02:00
$langs -> load ( " errors " );
2019-01-27 11:55:16 +01:00
setEventMessages ( $langs -> trans ( 'ErrorFailedToReadObject' , $object -> element ), null , 'errors' );
2013-09-23 13:17:31 +02:00
dol_syslog ( 'Failed to read data of object id=' . $object -> id . ' element=' . $object -> element );
$action = 'presend' ;
}
}