Răsfoiți Sursa

#53 - alert before send mail and relevant changes in SMTPMailer module

Alex Trofimov 12 ani în urmă
părinte
comite
8e1587df1f

+ 21 - 13
inc/utils.inc.php

@@ -331,9 +331,25 @@ function sendMail($sRecipientEmail, $sMailSubject, $sMailBody, $iRecipientID = 0
     $sMailSubject = '=?UTF-8?B?' . base64_encode( $sMailSubject ) . '?=';
     $sMailHeader = "MIME-Version: 1.0\r\n" . $sMailHeader;
 
-    // if SMPT mailer is installed and enabled - send mail throught it
-    if (!$isDisableAlert && 'on' == getParam('bx_smtp_on')) { // TODO: remake to use alert: before_send_mail
-        return BxDolService::call('bx_smtp', 'send', array($sRecipientEmail, $sMailSubject, $sMailBody, $sMailHeader, $sMailParameters, 'html' == $sEmailFlag, $aRecipientInfo));
+    // build data for alert handler
+    $bResult = null;
+    $aAlert = array(
+        'email' => $sRecipientEmail,
+        'subject' => $sMailSubject,
+        'body' => $sMailBody,
+        'header' => $sMailHeader,
+        'params' => $sMailParameters,
+        'recipient' => $aRecipientInfo,
+        'html' => 'html' == $sEmailFlag ? true : false,
+        'override_result' => &$bResult,
+    );
+
+    // system alert
+    if (!$isDisableAlert) {
+        bx_alert('system', 'before_send_mail', (isset($aRecipientInfo['ID']) ? $aRecipientInfo['ID'] : 0), '', $aAlert);
+        if ($bResult !== null)
+            return $bResult;
+        unset($aAlert['override_result']);
     }
 
     // send mail
@@ -347,16 +363,8 @@ function sendMail($sRecipientEmail, $sMailSubject, $sMailBody, $iRecipientID = 0
     }
 
     // system alert
-    if (!$isDisableAlert) {
-        bx_alert('profile', 'send_mail', (isset($aRecipientInfo['ID']) ? $aRecipientInfo['ID'] : 0), '', array(
-            'email'     => $sRecipientEmail,
-            'subject'   => $sMailSubject,
-            'body'      => $sMailBody,
-            'header'    => $sMailHeader,
-            'params'    => $sMailParameters,
-            'html'      => 'html' == $sEmailFlag ? true : false,
-        ));
-    }
+    if (!$isDisableAlert)
+        bx_alert('system', 'send_mail', (isset($aRecipientInfo['ID']) ? $aRecipientInfo['ID'] : 0), '', $aAlert);
 
     return $iSendingResult;
 }

+ 27 - 0
modules/boonex/smtpmailer/classes/BxSMTPAlertsResponse.php

@@ -0,0 +1,27 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
+ * CC-BY License - http://creativecommons.org/licenses/by/3.0/
+ *
+ * @defgroup    SMTPMailer SMTP Mailer
+ * @ingroup     DolphinModules
+ *
+ * @{
+ */
+
+/**
+ * alerts handler
+ */
+class BxSMTPAlertsResponse extends BxDolAlertsResponse 
+{   
+    public function response($oAlert) 
+    {
+        if ('system' != $oAlert->sUnit || 'before_send_mail' != $oAlert->sAction || 'on' != getParam('bx_smtp_on'))
+            return;
+
+        $a = $oAlert->aExtras;
+        $a['override_result'] = BxDolService::call('bx_smtp', 'send', array($a['email'], $a['subject'], $a['body'], $a['header'], $a['params'], $a['html'], $a['recipient']));
+    }
+}
+
+/** @} */

+ 4 - 3
modules/boonex/smtpmailer/classes/BxSMTPModule.php

@@ -111,6 +111,7 @@ class BxSMTPModule extends BxDolModule
             'body'      => $sMailBody,
             'header'    => $sMailHeader,
             'params'    => $sMailParameters,
+            'recipient' => $aRecipientInfo,
             'html'      => $isHtml,
         );
         bx_alert('profile', 'send_mail', $aRecipientInfo ? $aRecipientInfo['ID'] : 0, '', $aAlertData);
@@ -124,10 +125,10 @@ class BxSMTPModule extends BxDolModule
         $sMsg  = '';
         if (isset($_POST['tester_submit']) && $_POST['tester_submit']) {
             
+            $isHTML = bx_get('html') == 'on' ? true : false;
             $sRecipient = bx_process_pass(bx_get('recipient'));
             $sSubj = bx_process_pass(bx_get('subject'));
-            $sBody = bx_process_pass(bx_get('body'));
-            $isHTML = bx_get('html') == 'on' ? true : false;
+            $sBody = bx_process_pass(bx_get('body'), $isHTML ? BX_DATA_HTML : BX_DATA_TEXT);
     
             if (sendMail($sRecipient, $sSubj, $sBody, 0, array(), BX_EMAIL_SYSTEM, $isHTML ? 'html' : ''))
                 $sMsg = MsgBox(_t('_bx_smtp_send_ok'));
@@ -179,7 +180,7 @@ class BxSMTPModule extends BxDolModule
 
     function log ($s)
     {
-        $fn = BX_DIRECTORY_PATH_TMP . "smtp_mailer.log";
+        $fn = BX_DIRECTORY_PATH_ROOT . "logs/smtp_mailer.log";
         $f = @fopen ($fn, 'a');
         if (!$f) 
             return;

+ 10 - 1
modules/boonex/smtpmailer/install/sql/disable.sql

@@ -1,2 +1,11 @@
+
 -- settings
-DELETE FROM `top`, `toc`, `to` USING `sys_options_types` AS `top` LEFT JOIN `sys_options_categories` AS `toc` ON `top`.`id`=`toc`.`type_id` LEFT JOIN `sys_options` AS `to` ON `toc`.`id`=`to`.`category_id` WHERE `top`.`name`='bx_smtp';
+
+DELETE FROM `top`, `toc`, `to` USING `sys_options_types` AS `top` LEFT JOIN `sys_options_categories` AS `toc` ON `top`.`id`=`toc`.`type_id` LEFT JOIN `sys_options` AS `to` ON `toc`.`id`=`to`.`category_id` WHERE `top`.`name` = 'bx_smtp';
+
+-- alerts
+
+SET @iHandler := (SELECT `id` FROM `sys_alerts_handlers` WHERE `name` = 'bx_smtp' LIMIT 1);
+DELETE FROM `sys_alerts` WHERE `handler_id` = @iHandler;
+DELETE FROM `sys_alerts_handlers` WHERE `id` = @iHandler;
+

+ 13 - 1
modules/boonex/smtpmailer/install/sql/enable.sql

@@ -1,4 +1,6 @@
+
 -- settings
+
 SET @iTypeOrder = (SELECT MAX(`order`) FROM `sys_options_types` WHERE `group` = 'modules');
 INSERT INTO `sys_options_types` (`group`, `name`, `caption`, `icon`, `order`) VALUES 
 ('modules', 'bx_smtp', '_bx_smtp_adm_stg_cpt_type', 'bx_smtpmailer@modules/boonex/smtpmailer/|std-mi.png', IF(NOT ISNULL(@iTypeOrder), @iTypeOrder + 1, 1));
@@ -18,4 +20,14 @@ INSERT INTO `sys_options` (`name`, `value`, `category_id`, `caption`, `type`, `c
 ('bx_smtp_secure', 'Not Secure', @iCategId, 'Is your SMTP server requires secure connection', 'select', '', '', 7, 'Not Secure,SSL,TSL'), 
 ('bx_smtp_from_name', '', @iCategId, '''From'' name of the message', 'digit', '', '', 8, ''),
 ('bx_smtp_from_email', '', @iCategId, 'Override default sender email address', 'digit', '', '', 9, ''),
-('bx_smtp_send_attachments', '', @iCategId, 'Attach every outgoing email all files from ''modules/boonex/smtpmailer/data/attach/'' folder', 'checkbox', '', '', 10, '');
+('bx_smtp_send_attachments', '', @iCategId, 'Attach every outgoing email all files from ''modules/boonex/smtpmailer/data/attach/'' folder', 'checkbox', '', '', 10, '');
+
+-- alerts
+
+INSERT INTO `sys_alerts_handlers` (`name`, `class`, `file`, `service_call`) VALUES 
+('bx_smtp', 'BxSMTPAlertsResponse', 'modules/boonex/smtpmailer/classes/BxSMTPAlertsResponse.php', '');
+SET @iHandler := LAST_INSERT_ID();
+
+INSERT INTO `sys_alerts` (`unit`, `action`, `handler_id`) VALUES
+('system', 'before_send_mail', @iHandler);
+