 SET NAMES utf8;

DELIMITER $$

DROP PROCEDURE IF EXISTS web_mailer$$

CREATE DEFINER=`root`@`localhost` PROCEDURE web_mailer(
IN _MODE VARCHAR(200),
IN _IDN_ VARCHAR(38), -- z web_b2b_notify
IN _GUID VARCHAR(38), -- obiekt
IN _ParamA VARCHAR(255),
IN _ParamB VARCHAR(255),
IN _ParamC MEDIUMTEXT
)

/*
SET @sess='testsess';
CALL web_mailer("SEND_DEBUG","",@sess,"apro68@gmail.com","temat","tresc");
CALL web_vars('SET_GLOBAL',@sess,'','','','','','','','debugmail','<AP> apro68@gmail.com','');
CALL web_vars('SET_VAR',@app, @sess,'debugmail','<AP> apro68@gmail.com','','','','');

*/

 
BEGIN

DECLARE _UGUID VARCHAR(38);
DECLARE _CC VARCHAR(100);
DECLARE _DEBUG BOOLEAN;
DECLARE _IDN INT;
DECLARE _INTERVAL INT;
SET _IDN=_VarInt(_IDN_),_UGUID=_ParamA,_CC=_ParamB,_DEBUG=_ParamC LIKE "%DEBUG%",@web_mailer.mode=NULL,_ParamC=REPLACE(_ParamC,'DEBUG','');
SET _INTERVAL=_VarINT(_ParamC);SET _INTERVAL=IF(_INTERVAL>0,_INTERVAL,24);
SELECT value INTO @session.globals.Owner FROM _db_globals g WHERE g.key='Owner';

IF _MODE="TEST" THEN
    SET @idn=_VarInt(_IDN);
END IF;

-- SET @sess="SERVER";
-- CALL web_mailer("SEND_DEBUG","",@sess,"apro68@gmail.com","temat","tresc");


IF _MODE="SEND_DEBUG" THEN
    SET @web_mailer.mode=_MODE,@comguid=CONCAT('DEBUG',uuid_short());
    SET @debugmail=NULLIF(web_vars_get('*','*','','debugmail'),'');
    SET @debugmail=IFNULL(@debugmail,'apro68@gmail.com');
--    SET @sender='motlawa@pliszka.pl';
    SET @sender=NULLIF(web_vars_get('*','*','','debugsendermail'),'');
    SET @debugmail=IF(_ParamA<>'',_ParamA,@debugmail);
    IF _GUID='SERVER' THEN
        SET @userlogin='root',@userguid='root',@session.status=1;
    ELSE
        SELECT log_user,log_USER_GUID,IFNULL(status,"")="ACTIVE" INTO @userlogin,@userguid,@session.status FROM _sys_db_logons WHERE guid=_GUID;
    END IF;
    IF @session.status=1 THEN
        INSERT INTO com_msg_ng (guid,type,owner,Sender,Msg_Title,srcguid,srctable,Msg_Body) 
            VALUES (@comguid,'OUT',@userguid,@sender,_ParamB,@userguid,'osoby',
        CONCAT(_ParamC,'<BR><BR>sesja: ',_GUID,'<BR><BR>user:',@userlogin));
        INSERT INTO com_msg_po (GUIDNg,Type,Medium_Type,Recipient,RecipientRole,Dispatch_ID) 
            VALUES (@comguid,'OUT','mail',@debugmail,'To',@comguid);
        IF _DEBUG THEN
            SELECT * FROM com_msg_ng WHERE guid=@comguid;SELECT * FROM com_msg_po WHERE guidng=@comguid;
        END IF;
    END IF;
END IF;


IF @web_mailer.mode IS NULL THEN
  SELECT CONCAT(_MODE,' nie zdefiniowany');
END IF;


END $$

DELIMITER ;

/*
$Log: web_mailer.sql,v $
Revision 1.5  2026/03/30 11:20:11  apro
.

Revision 1.4  2025/10/09 12:00:43  apro
koment

Revision 1.3  2025/09/05 12:58:45  apro
debugmail

Revision 1.2  2025/03/18 09:15:46  waliant
$SEND$ Warningi

Revision 1.1  2025/03/17 12:32:44  apro
$SEND$

*/
