2013-09-23 13:17:31 +02:00
< ? php
/* Copyright ( C ) 2013 Laurent Destailleur < eldy @ users . sourceforge . net >
*
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
* along with this program . If not , see < http :// www . gnu . org / licenses />.
* or see http :// www . gnu . org /
*/
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)
2017-05-19 12:58:38 +02:00
// $trigger_name must be set (can be '')
// $actiontypecode can be set
// $object and $uobject may be defined
2013-09-23 13:17:31 +02:00
/*
* Add file in email form
2017-06-17 10:44:08 +02:00
*/
2017-06-27 16:42:11 +02:00
if ( GETPOST ( 'addfile' , 'alpha' ))
2013-09-23 13:17:31 +02:00
{
2016-06-24 16:10:52 +02: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
$vardir = $conf -> user -> dir_output . " / " . $user -> id ;
2016-06-24 16:10:52 +02:00
$upload_dir_tmp = $vardir . '/temp' ; // TODO Add $keytoavoidconflict in upload_dir path
2013-09-23 13:17:31 +02:00
2018-09-05 23:25:17 +02:00
dol_add_file_process ( $upload_dir_tmp , 0 , 0 , 'addedfile' , '' , null , $trackid , 0 );
2013-09-23 13:17:31 +02:00
$action = 'presend' ;
}
/*
* Remove file in email form
*/
2015-08-24 16:05:15 +02:00
if ( ! empty ( $_POST [ 'removedfile' ]) && empty ( $_POST [ 'removAll' ]))
2013-09-23 13:17:31 +02:00
{
2016-06-24 16:10:52 +02: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
$vardir = $conf -> user -> dir_output . " / " . $user -> id ;
2016-06-24 16:10:52 +02:00
$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.
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
2013-09-23 13:17:31 +02:00
$action = 'presend' ;
}
2015-08-24 16:05:15 +02:00
/*
* Remove all files in email form
*/
2017-06-27 16:42:11 +02:00
if ( GETPOST ( 'removAll' , 'alpha' ))
2015-08-24 16:05:15 +02:00
{
2016-06-24 16:10:52 +02:00
$trackid = GETPOST ( 'trackid' , 'aZ09' );
2017-01-23 08:33:05 +01:00
2017-03-02 14:29:22 +01:00
$listofpaths = array ();
2015-08-24 16:05:15 +02:00
$listofnames = array ();
$listofmimes = array ();
2017-03-02 14:29:22 +01:00
$keytoavoidconflict = empty ( $trackid ) ? '' : '-' . $trackid ;
2016-06-24 16:10:52 +02: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
2015-08-24 16:05:15 +02:00
foreach ( $listofpaths as $key => $value )
{
$pathtodelete = $value ;
$filetodelete = $listofnames [ $key ];
2016-09-20 12:19:17 +02:00
$result = dol_delete_file ( $pathtodelete , 1 ); // Delete uploded Files
2015-08-24 16:05:15 +02:00
$langs -> load ( " other " );
2015-11-19 13:59:58 +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
*/
2015-08-24 16:35:08 +02:00
if (( $action == 'send' || $action == 'relance' ) && ! $_POST [ 'addfile' ] && ! $_POST [ 'removAll' ] && ! $_POST [ 'removedfile' ] && ! $_POST [ 'cancel' ] && ! $_POST [ 'modelselected' ])
2013-09-23 13:17:31 +02:00
{
2018-01-22 17:15:44 +01:00
if ( empty ( $trackid )) $trackid = GETPOST ( 'trackid' , 'aZ09' );
2016-10-03 00:39:07 +02:00
$subject = '' ; $actionmsg = '' ; $actionmsg2 = '' ;
2017-01-23 08:33:05 +01:00
2013-09-23 13:17:31 +02:00
$langs -> load ( 'mails' );
2016-09-26 14:08:14 +02:00
if ( is_object ( $object ))
2013-09-23 13:17:31 +02:00
{
2017-03-02 14:29:22 +01:00
$result = $object -> fetch ( $id );
$sendtosocid = 0 ; // Thirdparty on object
2017-12-20 17:20:40 +01:00
if ( method_exists ( $object , " fetch_thirdparty " ) && ! in_array ( $object -> element , array ( 'societe' , 'member' , 'user' , 'expensereport' )))
2017-03-02 14:29:22 +01:00
{
$result = $object -> fetch_thirdparty ();
2017-05-15 00:15:56 +02:00
if ( $object -> element == 'user' && $result == 0 ) $result = 1 ; // Even if not found, we consider ok
2017-03-02 14:29:22 +01:00
$thirdparty = $object -> thirdparty ;
$sendtosocid = $thirdparty -> id ;
}
2017-10-08 16:34:42 +02:00
else if ( $object -> element == 'member' || $object -> element == 'user' )
2017-09-22 17:48:38 +02:00
{
$thirdparty = $object ;
if ( $thirdparty -> id > 0 ) $sendtosocid = $thirdparty -> id ;
}
2017-03-02 14:29:22 +01:00
else if ( $object -> element == 'societe' )
{
$thirdparty = $object ;
if ( $thirdparty -> id > 0 ) $sendtosocid = $thirdparty -> id ;
}
2017-05-19 12:58:38 +02:00
else dol_print_error ( '' , 'Use actions_sendmails.in.php for an element/object that is not supported' );
2018-04-11 18:31:46 +02:00
if ( is_object ( $hookmanager ))
{
$parameters = array ();
$reshook = $hookmanager -> executeHooks ( 'initSendToSocid' , $parameters , $object , $action ); // Note that $action and $object may have been modified by some hooks
}
2013-09-23 13:17:31 +02:00
}
2016-09-26 14:08:14 +02:00
else $thirdparty = $mysoc ;
2013-09-23 13:17:31 +02:00
if ( $result > 0 )
{
2017-02-22 17:46:54 +01:00
$sendto = '' ;
$sendtocc = '' ;
$sendtobcc = '' ;
$sendtoid = array ();
2018-05-22 20:17:31 +02:00
$sendtouserid = array ();
$sendtoccuserid = array ();
2017-02-22 17:46:54 +01:00
// Define $sendto
2017-02-15 11:41:09 +01:00
$receiver = $_POST [ 'receiver' ];
2017-02-22 17:46:54 +01:00
if ( ! is_array ( $receiver ))
{
2017-03-02 14:29:22 +01:00
if ( $receiver == '-1' ) $receiver = array ();
else $receiver = array ( $receiver );
2017-02-22 17:46:54 +01:00
}
$tmparray = array ();
2014-11-04 13:54:18 +01:00
if ( trim ( $_POST [ 'sendto' ]))
2013-09-23 13:17:31 +02:00
{
2017-02-22 17:46:54 +01:00
// Recipients are provided into free text
$tmparray [] = trim ( $_POST [ 'sendto' ]);
2013-09-23 13:17:31 +02:00
}
2017-02-22 17:46:54 +01:00
if ( count ( $receiver ) > 0 )
2013-09-23 13:17:31 +02:00
{
2017-03-02 14:29:22 +01:00
foreach ( $receiver as $key => $val )
2013-09-23 13:17:31 +02:00
{
2017-02-15 11:41:09 +01:00
// Recipient was provided from combo list
if ( $val == 'thirdparty' ) // Id of third party
{
2018-04-10 11:15:21 +02:00
$tmparray [] = dol_string_nospecial ( $thirdparty -> name , ' ' , array ( " , " )) . ' <' . $thirdparty -> email . '>' ;
2017-02-15 11:41:09 +01:00
}
2017-05-15 00:15:56 +02:00
elseif ( $val ) // Id du contact
2017-02-15 11:41:09 +01:00
{
2017-02-22 17:46:54 +01:00
$tmparray [] = $thirdparty -> contact_get_property (( int ) $val , 'email' );
2017-02-15 11:41:09 +01:00
$sendtoid [] = $val ;
}
2013-09-23 13:17:31 +02:00
}
}
2018-05-22 20:17:31 +02:00
if ( ! empty ( $conf -> global -> MAIN_MAIL_ENABLED_USER_DEST_SELECT ))
{
2018-03-21 11:48:30 +01:00
$receiveruser = $_POST [ 'receiveruser' ];
if ( is_array ( $receiveruser ) && count ( $receiveruser ) > 0 )
{
$fuserdest = new User ( $db );
foreach ( $receiveruser as $key => $val )
{
2018-05-22 20:17:31 +02:00
$tmparray [] = $fuserdest -> user_get_property ( $val , 'email' );
$sendtouserid [] = $val ;
2018-03-21 11:48:30 +01:00
}
}
}
2018-05-22 20:17:31 +02:00
2017-02-22 17:46:54 +01:00
$sendto = implode ( ',' , $tmparray );
2017-03-02 14:29:22 +01:00
2017-02-22 17:46:54 +01:00
// Define $sendtocc
$receivercc = $_POST [ 'receivercc' ];
if ( ! is_array ( $receivercc ))
{
2017-03-02 14:29:22 +01:00
if ( $receivercc == '-1' ) $receivercc = array ();
else $receivercc = array ( $receivercc );
2017-02-22 17:46:54 +01:00
}
$tmparray = array ();
2014-11-04 13:54:18 +01:00
if ( trim ( $_POST [ 'sendtocc' ]))
{
2017-02-22 17:46:54 +01:00
$tmparray [] = trim ( $_POST [ 'sendtocc' ]);
2014-11-04 13:54:18 +01:00
}
2017-02-22 17:46:54 +01:00
if ( count ( $receivercc ) > 0 )
2014-11-04 13:54:18 +01:00
{
2017-03-02 14:29:22 +01:00
foreach ( $receivercc as $key => $val )
2014-11-04 13:54:18 +01:00
{
2017-02-22 17:46:54 +01:00
// Recipient was provided from combo list
if ( $val == 'thirdparty' ) // Id of third party
{
2018-04-10 11:15:21 +02:00
$tmparray [] = dol_string_nospecial ( $thirdparty -> name , ' ' , array ( " , " )) . ' <' . $thirdparty -> email . '>' ;
2017-02-22 17:46:54 +01:00
}
2017-05-15 00:15:56 +02:00
elseif ( $val ) // Id du contact
2017-02-22 17:46:54 +01:00
{
$tmparray [] = $thirdparty -> contact_get_property (( int ) $val , 'email' );
//$sendtoid[] = $val; TODO Add also id of contact in CC ?
}
2014-11-04 13:54:18 +01:00
}
}
2018-03-21 11:48:30 +01:00
if ( ! empty ( $conf -> global -> MAIN_MAIL_ENABLED_USER_DEST_SELECT )) {
2018-05-22 20:17:31 +02:00
$receiverccuser = $_POST [ 'receiverccuser' ];
2018-03-21 11:48:30 +01:00
2018-05-22 20:17:31 +02:00
if ( is_array ( $receiverccuser ) && count ( $receiverccuser ) > 0 )
2018-03-21 11:48:30 +01:00
{
$fuserdest = new User ( $db );
2018-05-22 20:17:31 +02:00
foreach ( $receiverccuser as $key => $val )
2018-03-21 11:48:30 +01:00
{
2018-05-22 20:17:31 +02:00
$tmparray [] = $fuserdest -> user_get_property ( $val , 'email' );
$sendtoccuserid [] = $val ;
2018-03-21 11:48:30 +01:00
}
}
}
2017-02-22 17:46:54 +01:00
$sendtocc = implode ( ',' , $tmparray );
2013-09-23 13:17:31 +02:00
if ( dol_strlen ( $sendto ))
{
2017-06-17 10:44:08 +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
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 " );
2017-11-25 18:52:47 +01:00
$fromtype = GETPOST ( 'fromtype' , 'alpha' );
if ( $fromtype === 'robot' ) {
2018-04-10 11:15:21 +02:00
$from = dol_string_nospecial ( $conf -> global -> MAIN_MAIL_EMAIL_FROM , ' ' , array ( " , " )) . ' <' . $conf -> global -> MAIN_MAIL_EMAIL_FROM . '>' ;
2017-11-25 18:52:47 +01:00
}
elseif ( $fromtype === 'user' ) {
2018-04-10 11:15:21 +02:00
$from = dol_string_nospecial ( $user -> getFullName ( $langs ), ' ' , array ( " , " )) . ' <' . $user -> email . '>' ;
2017-03-02 14:29:22 +01:00
}
elseif ( $fromtype === 'company' ) {
2018-04-10 11:15:21 +02:00
$from = dol_string_nospecial ( $conf -> global -> MAIN_INFO_SOCIETE_NOM , ' ' , array ( " , " )) . ' <' . $conf -> global -> MAIN_INFO_SOCIETE_MAIL . '>' ;
2017-03-02 14:29:22 +01:00
}
elseif ( preg_match ( '/user_aliases_(\d+)/' , $fromtype , $reg )) {
$tmp = explode ( ',' , $user -> email_aliases );
$from = trim ( $tmp [( $reg [ 1 ] - 1 )]);
}
elseif ( preg_match ( '/global_aliases_(\d+)/' , $fromtype , $reg )) {
$tmp = explode ( ',' , $conf -> global -> MAIN_INFO_SOCIETE_MAIL_ALIASES );
$from = trim ( $tmp [( $reg [ 1 ] - 1 )]);
}
2017-10-11 19:41:19 +02:00
elseif ( preg_match ( '/senderprofile_(\d+)_(\d+)/' , $fromtype , $reg )) {
$sql = 'SELECT rowid, label, email FROM ' . MAIN_DB_PREFIX . 'c_email_senderprofile WHERE rowid = ' . ( int ) $reg [ 1 ];
$resql = $db -> query ( $sql );
$obj = $db -> fetch_object ( $resql );
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
}
}
2017-03-02 14:29:22 +01:00
else {
2018-04-10 11:15:21 +02:00
$from = dol_string_nospecial ( $_POST [ 'fromname' ], ' ' , array ( " , " )) . ' <' . $_POST [ 'frommail' ] . '>' ;
2017-03-02 14:29:22 +01:00
}
2018-04-10 11:15:21 +02:00
$replyto = dol_string_nospecial ( $_POST [ 'replytoname' ], ' ' , array ( " , " )) . ' <' . $_POST [ 'replytomail' ] . '>' ;
2017-09-16 09:15:10 +02:00
$message = GETPOST ( 'message' , 'none' );
$subject = GETPOST ( 'subject' , 'none' );
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" />
$message = preg_replace ( '/(<img.*src=")[^\"]*viewimage\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^\/]*\/>)/' , '\1' . $urlwithroot . '/viewimage.php\2modulepart=medias\3file=\4\5' , $message );
2014-11-04 13:54:18 +01:00
$sendtobcc = GETPOST ( 'sendtoccc' );
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...
if ( ! empty ( $autocopy ))
{
$sendtobcc .= ( empty ( $conf -> global -> $autocopy ) ? '' : (( $sendtobcc ? " , " : " " ) . $conf -> global -> $autocopy ));
}
2017-01-23 08:33:05 +01:00
2013-09-23 13:17:31 +02:00
$deliveryreceipt = $_POST [ 'deliveryreceipt' ];
2013-09-25 21:36:40 +02:00
if ( $action == 'send' || $action == 'relance' )
2013-09-23 13:17:31 +02:00
{
2016-10-24 02:02:30 +02:00
$actionmsg2 = $langs -> transnoentities ( 'MailSentBy' ) . ' ' . CMailFile :: getValidAddress ( $from , 4 , 0 , 1 ) . ' ' . $langs -> transnoentities ( 'To' ) . ' ' . CMailFile :: getValidAddress ( $sendto , 4 , 0 , 1 );
2013-09-23 13:17:31 +02:00
if ( $message )
{
2016-10-24 02:02:30 +02:00
$actionmsg = $langs -> transnoentities ( 'MailFrom' ) . ': ' . dol_escape_htmltag ( $from );
$actionmsg = dol_concatdesc ( $actionmsg , $langs -> transnoentities ( 'MailTo' ) . ': ' . dol_escape_htmltag ( $sendto ));
if ( $sendtocc ) $actionmsg = dol_concatdesc ( $actionmsg , $langs -> transnoentities ( 'Bcc' ) . " : " . dol_escape_htmltag ( $sendtocc ));
2014-11-18 19:03:02 +01:00
$actionmsg = dol_concatdesc ( $actionmsg , $langs -> transnoentities ( 'MailTopic' ) . " : " . $subject );
$actionmsg = dol_concatdesc ( $actionmsg , $langs -> transnoentities ( 'TextUsedInTheMessageBody' ) . " : " );
$actionmsg = dol_concatdesc ( $actionmsg , $message );
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 );
2016-06-24 16:10:52 +02:00
$formmail -> trackid = $trackid ; // $trackid must be defined
2017-01-23 08:33:05 +01:00
2013-09-23 13:17:31 +02:00
$attachedfiles = $formmail -> get_attached_files ();
$filepath = $attachedfiles [ 'paths' ];
$filename = $attachedfiles [ 'names' ];
$mimetype = $attachedfiles [ 'mimes' ];
2015-08-24 16:22:21 +02:00
2015-11-13 00:19:11 +01:00
// Feature to push mail sent into Sent folder
2018-02-03 17:32:38 +01:00
/* This code must be now included into the hook mail , method sendMailAfter
2015-11-13 00:19:11 +01:00
if ( ! empty ( $conf -> dolimail -> enabled ))
2015-08-24 16:05:15 +02:00
{
2015-11-13 00:19:11 +01:00
$mailfromid = explode ( " # " , $_POST [ 'frommail' ], 3 ); // $_POST['frommail'] = 'aaa#Sent# <aaa@aaa.com>' // TODO Use a better way to define Sent dir.
2015-08-26 15:04:50 +02:00
if ( count ( $mailfromid ) == 0 ) $from = $_POST [ 'fromname' ] . ' <' . $_POST [ 'frommail' ] . '>' ;
2015-08-24 16:05:15 +02:00
else
{
$mbid = $mailfromid [ 1 ];
2013-09-23 13:17:31 +02:00
2018-02-03 17:32:38 +01:00
// IMAP Postbox
2015-08-24 16:05:15 +02:00
$mailboxconfig = new IMAP ( $db );
$mailboxconfig -> fetch ( $mbid );
if ( $mailboxconfig -> mailbox_imap_host ) $ref = $mailboxconfig -> get_ref ();
2016-09-20 12:19:17 +02:00
2015-08-24 16:05:15 +02:00
$mailboxconfig -> folder_id = $mailboxconfig -> mailbox_imap_outbox ;
$mailboxconfig -> userfolder_fetch ();
2016-09-20 12:19:17 +02:00
2015-08-24 16:05:15 +02:00
if ( $mailboxconfig -> mailbox_save_sent_mails == 1 )
{
2016-09-20 12:19:17 +02:00
2015-08-24 16:05:15 +02:00
$folder = str_replace ( $ref , '' , $mailboxconfig -> folder_cache_key );
2015-11-13 00:19:11 +01:00
if ( ! $folder ) $folder = " Sent " ; // Default Sent folder
2016-09-20 12:19:17 +02:00
2015-08-24 16:05:15 +02:00
$mailboxconfig -> mbox = imap_open ( $mailboxconfig -> get_connector_url () . $folder , $mailboxconfig -> mailbox_imap_login , $mailboxconfig -> mailbox_imap_password );
2018-04-16 13:51:16 +02:00
if ( false === $mailboxconfig -> mbox )
2015-08-24 16:05:15 +02:00
{
2018-04-16 13:51:16 +02:00
$info = false ;
2015-08-24 16:05:15 +02:00
$err = $langs -> trans ( 'Error3_Imap_Connection_Error' );
2015-11-19 13:59:58 +01:00
setEventMessages ( $err , $mailboxconfig -> element , null , 'errors' );
2016-09-20 12:19:17 +02:00
}
else
2015-08-24 16:05:15 +02:00
{
$mailboxconfig -> mailboxid = $_POST [ 'frommail' ];
$mailboxconfig -> foldername = $folder ;
$from = $mailfromid [ 0 ] . $mailfromid [ 2 ];
$imap = 1 ;
}
2016-09-20 12:19:17 +02:00
}
2015-08-24 16:05:15 +02:00
}
}
2018-02-03 17:32:38 +01:00
*/
2016-09-20 12:19:17 +02:00
2017-09-16 03:13:44 +02:00
// Make substitution in email content
$substitutionarray = getCommonSubstitutionArray ( $langs , 0 , null , $object );
$substitutionarray [ '__EMAIL__' ] = $sendto ;
$substitutionarray [ '__CHECK_READ__' ] = ( is_object ( $object ) && is_object ( $object -> thirdparty )) ? '<img src="' . DOL_MAIN_URL_ROOT . '/public/emailing/mailing-read.php?tag=' . $object -> thirdparty -> tag . '&securitykey=' . urlencode ( $conf -> global -> MAILING_EMAIL_UNSUBSCRIBE_KEY ) . '" width="1" height="1" style="width:1px;height:1px" border="0"/>' : '' ;
$parameters = array ( 'mode' => 'formemail' );
complete_substitutions_array ( $substitutionarray , $langs , $object , $parameters );
2017-08-28 12:18:18 +02:00
$subject = make_substitutions ( $subject , $substitutionarray );
$message = make_substitutions ( $message , $substitutionarray );
2017-09-22 20:18:11 +02:00
if ( method_exists ( $object , 'makeSubstitution' ))
{
$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)
2017-08-24 02:02:00 +02:00
if ( empty ( $sendcontext )) $sendcontext = 'standard' ;
2017-08-28 12:53:02 +02:00
$mailfile = new CMailFile ( $subject , $sendto , $from , $message , $filepath , $mimetype , $filename , $sendtocc , $sendtobcc , $deliveryreceipt , - 1 , '' , '' , $trackid , '' , $sendcontext );
2013-09-23 13:17:31 +02:00
if ( $mailfile -> error )
{
2018-03-27 11:17:40 +02:00
setEventMessages ( $mailfile -> error , $mailfile -> errors , 'errors' );
2016-06-24 16:10:52 +02:00
$action = 'presend' ;
2013-09-23 13:17:31 +02:00
}
else
{
$result = $mailfile -> sendfile ();
if ( $result )
{
2018-02-03 17:32:38 +01:00
// Two hooks are available into method $mailfile->sendfile, so dedicated code is no more required
/*
2015-11-13 00:19:11 +01:00
if ( ! empty ( $conf -> dolimail -> enabled ))
2015-08-24 16:05:15 +02:00
{
2015-11-13 00:19:11 +01:00
$mid = ( GETPOST ( 'mid' , 'int' ) ? GETPOST ( 'mid' , 'int' ) : 0 ); // Original mail id is set ?
2015-08-24 16:05:15 +02:00
if ( $mid )
{
2015-11-13 00:19:11 +01:00
// set imap flag answered if it is an answered mail
2015-08-24 16:05:15 +02:00
$dolimail = new DoliMail ( $db );
$dolimail -> id = $mid ;
$res = $dolimail -> set_prop ( $user , 'answered' , 1 );
2017-03-02 14:29:22 +01:00
}
2015-08-24 16:05:15 +02:00
if ( $imap == 1 )
{
// write mail to IMAP Server
2016-09-20 12:19:17 +02:00
$movemail = $mailboxconfig -> putMail ( $subject , $sendto , $from , $message , $filepath , $mimetype , $filename , $sendtocc , $folder , $deliveryreceipt , $mailfile );
2015-11-21 05:20:08 +01:00
if ( $movemail ) setEventMessages ( $langs -> trans ( " MailMovedToImapFolder " , $folder ), null , 'mesgs' );
2016-09-20 12:19:17 +02:00
else setEventMessages ( $langs -> trans ( " MailMovedToImapFolder_Warning " , $folder ), null , 'warnings' );
2017-03-02 14:29:22 +01:00
}
2018-02-03 17:32:38 +01:00
} */
2015-12-18 12:45:04 +01:00
2018-05-17 16:50:40 +02:00
// Initialisation of datas of object to call trigger
2017-01-16 13:06:26 +01:00
if ( is_object ( $object ))
{
2017-05-19 12:58:38 +02:00
if ( empty ( $actiontypecode )) $actiontypecode = 'AC_OTH_AUTO' ; // Event insert into agenda automatically
2017-06-17 10:44:08 +02:00
2017-05-19 12:58:38 +02:00
$object -> socid = $sendtosocid ; // To link to a company
$object -> sendtoid = $sendtoid ; // To link to contacts/addresses. This is an array.
$object -> actiontypecode = $actiontypecode ; // Type of event ('AC_OTH', 'AC_OTH_AUTO', 'AC_XXX'...)
$object -> actionmsg = $actionmsg ; // Long text
$object -> actionmsg2 = $actionmsg2 ; // Short text
2017-01-23 08:33:05 +01:00
$object -> trackid = $trackid ;
$object -> fk_element = $object -> id ;
$object -> elementtype = $object -> element ;
2017-07-18 19:33:25 +02:00
if ( is_array ( $attachedfiles ) && count ( $attachedfiles ) > 0 ) {
$object -> attachedfiles = $attachedfiles ;
}
2018-03-21 22:58:17 +01:00
if ( is_array ( $sendtouserid ) && count ( $sendtouserid ) > 0 && ! empty ( $conf -> global -> MAIN_MAIL_ENABLED_USER_DEST_SELECT )) {
$object -> sendtouserid = $sendtouserid ;
}
2017-01-23 08:33:05 +01:00
// Call of triggers
2017-05-19 12:58:38 +02:00
if ( ! empty ( $trigger_name ))
{
include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php' ;
$interface = new Interfaces ( $db );
$result = $interface -> run_triggers ( $trigger_name , $object , $user , $langs , $conf );
2017-09-16 01:34:15 +02:00
if ( $result < 0 ) {
2017-12-30 12:12:13 +01:00
setEventMessages ( $interface -> error , $interface -> errors , 'errors' );
2017-05-19 12:58:38 +02:00
}
2017-01-23 08:33:05 +01:00
}
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
$mesg = $langs -> trans ( 'MailSuccessfulySent' , $mailfile -> getValidAddress ( $from , 2 ), $mailfile -> getValidAddress ( $sendto , 2 ));
setEventMessages ( $mesg , null , 'mesgs' );
2018-02-03 17:32:38 +01:00
2018-02-03 17:42:17 +01:00
$moreparam = '' ;
if ( isset ( $paramname2 ) || isset ( $paramval2 )) $moreparam .= '&' . ( $paramname2 ? $paramname2 : 'mid' ) . '=' . $paramval2 ;
header ( 'Location: ' . $_SERVER [ " PHP_SELF " ] . '?' . ( $paramname ? $paramname : 'id' ) . '=' . ( is_object ( $object ) ? $object -> id : '' ) . $moreparam );
exit ;
2013-09-23 13:17:31 +02:00
}
else
{
$langs -> load ( " other " );
2015-08-24 16:05:15 +02:00
$mesg = '<div class="error">' ;
2013-09-23 13:17:31 +02:00
if ( $mailfile -> error )
{
$mesg .= $langs -> trans ( 'ErrorFailedToSendMail' , $from , $sendto );
$mesg .= '<br>' . $mailfile -> error ;
}
else
{
2015-08-24 16:05:15 +02:00
$mesg .= 'No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS' ;
2013-09-23 13:17:31 +02:00
}
2015-08-24 16:05:15 +02:00
$mesg .= '</div>' ;
2015-11-19 13:59:58 +01:00
setEventMessages ( $mesg , null , 'warnings' );
2013-09-23 13:17:31 +02:00
$action = 'presend' ;
}
}
}
else
{
$langs -> load ( " errors " );
2015-11-19 13:59:58 +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' ;
}
}
else
{
$langs -> load ( " other " );
2017-09-22 17:48:38 +02: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' ;
}
}