BxSMTPModule.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup SMTPMailer SMTP Mailer
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. require_once (BX_DIRECTORY_PATH_PLUGINS . "phpmailer/Exception.php");
  12. require_once (BX_DIRECTORY_PATH_PLUGINS . "phpmailer/PHPMailer.php");
  13. require_once (BX_DIRECTORY_PATH_PLUGINS . "phpmailer/SMTP.php");
  14. class BxSMTPModule extends BxDolModule
  15. {
  16. function __construct(&$aModule)
  17. {
  18. parent::__construct($aModule);
  19. }
  20. function serviceSend ($sRecipientEmail, $sMailSubject, $sMailBody, $sMailHeader, $sMailParameters, $isHtml, $aRecipientInfo = array(), $aCustomHeaders = array())
  21. {
  22. $iRet = true;
  23. $sErrorMessage = '';
  24. if ($sRecipientEmail) {
  25. try {
  26. $mail = new PHPMailer\PHPMailer\PHPMailer(true);
  27. if ('on' == getParam('bx_smtp_on'))
  28. $mail->IsSMTP();
  29. // $mail->SMTPDebug = 2;
  30. $mail->CharSet = 'UTF-8';
  31. // smtp server auth or not
  32. $mail->SMTPAuth = 'on' == getParam('bx_smtp_auth') ? true : false;
  33. // from settings, smtp server secure ssl/tls
  34. $sParamSecure = getParam('bx_smtp_secure');
  35. if ('SSL' == $sParamSecure || 'TLS' == $sParamSecure) {
  36. $mail->SMTPSecure = strtolower($sParamSecure);
  37. if ('on' == getParam('bx_smtp_allow_selfsigned')) {
  38. $mail->SMTPOptions = array(
  39. 'ssl' => array(
  40. 'verify_peer' => false,
  41. 'verify_peer_name' => false,
  42. 'allow_self_signed' => true
  43. )
  44. );
  45. }
  46. }
  47. // from settings, smtp server
  48. $sParamHost = getParam('bx_smtp_host');
  49. if ($sParamHost)
  50. $mail->Host = $sParamHost;
  51. // smtp port 25, 465
  52. $sParamPort = getParam('bx_smtp_port');
  53. if ((int)$sParamPort > 0)
  54. $mail->Port = $sParamPort;
  55. // from settings, username and passord of smtp server
  56. $mail->Username = getParam ('bx_smtp_username');
  57. $mail->Password = getParam ('bx_smtp_password');
  58. if (!isset($aCustomHeaders['From'])) {
  59. $sParamSender = trim(getParam('bx_smtp_from_email'));
  60. if ($sParamSender)
  61. $mail->From = $sParamSender;
  62. else
  63. $mail->From = getParam('site_email_notify');
  64. // get site name or some other name as sender's name
  65. $mail->FromName = getParam ('bx_smtp_from_name');
  66. }
  67. if (!isset($aCustomHeaders['Subject']))
  68. $mail->Subject = $sMailSubject;
  69. if ($isHtml) {
  70. $mail->Body = $sMailBody;
  71. $mail->AltBody = $isHtml ? strip_tags($sMailBody) : $sMailBody;
  72. } else {
  73. $mail->Body = $sMailBody;
  74. }
  75. $mail->WordWrap = 50; // set word wrap
  76. $mail->AddAddress($sRecipientEmail);
  77. $mail->IsHTML($isHtml ? true : false);
  78. foreach ($aCustomHeaders as $sHeaderName => $sHeaderValue) {
  79. if ('From' == $sHeaderName) {
  80. if (preg_match('/(.+)<(.+)>/', $sHeaderValue, $m)) {
  81. $mail->setFrom($m[2], trim($m[1]), false);
  82. }
  83. else {
  84. $mail->setFrom($sHeaderValue, '', false);
  85. }
  86. }
  87. else {
  88. $mail->addCustomHeader($sHeaderName, $sHeaderValue);
  89. }
  90. }
  91. $mail->Send();
  92. } catch (PHPMailer\PHPMailer\Exception $e) {
  93. $iRet = false;
  94. $sErrorMessage = $e->getMessage();
  95. $this->log("Mailer Error ($sRecipientEmail): " . $sErrorMessage);
  96. }
  97. }
  98. //--- create system event [begin]
  99. $aAlertData = array(
  100. 'email' => $sRecipientEmail,
  101. 'subject' => $sMailSubject,
  102. 'body' => $sMailBody,
  103. 'header' => $sMailHeader,
  104. 'params' => $sMailParameters,
  105. 'recipient' => $aRecipientInfo,
  106. 'html' => $isHtml,
  107. 'ret' => $iRet,
  108. 'error_message' => $sErrorMessage,
  109. );
  110. bx_alert('profile', 'send_mail', $aRecipientInfo && isset($aRecipientInfo['ID']) ? $aRecipientInfo['ID'] : 0, '', $aAlertData);
  111. //--- create system event [ end ]
  112. return $iRet;
  113. }
  114. function formTester()
  115. {
  116. $sMsg = '';
  117. if (isset($_POST['tester_submit']) && $_POST['tester_submit']) {
  118. $isHTML = bx_get('html') == 'on' ? true : false;
  119. $sRecipient = bx_process_pass(bx_get('recipient'));
  120. $sSubj = bx_get('subject');
  121. $sBody = bx_process_pass(bx_get('body'), $isHTML ? BX_DATA_HTML : BX_DATA_TEXT);
  122. if (sendMail($sRecipient, $sSubj, $sBody, 0, array(), BX_EMAIL_SYSTEM, $isHTML ? 'html' : ''))
  123. $sMsg = MsgBox(_t('_bx_smtp_send_ok'));
  124. else
  125. $sMsg = MsgBox(_t('_bx_smtp_send_fail'));
  126. }
  127. $aForm = array(
  128. 'form_attrs' => array(
  129. 'method' => 'post',
  130. ),
  131. 'inputs' => array (
  132. 'recipient' => array(
  133. 'type' => 'text',
  134. 'name' => 'recipient',
  135. 'caption' => _t('_bx_smtp_recipient'),
  136. 'value' => '',
  137. ),
  138. 'subject' => array(
  139. 'type' => 'text',
  140. 'name' => 'subject',
  141. 'caption' => _t('_bx_smtp_subject'),
  142. 'value' => '',
  143. ),
  144. 'body' => array(
  145. 'type' => 'textarea',
  146. 'name' => 'body',
  147. 'caption' => _t('_bx_smtp_body'),
  148. 'value' => '',
  149. ),
  150. 'html' => array(
  151. 'type' => 'checkbox',
  152. 'name' => 'html',
  153. 'caption' => _t('_bx_smtp_is_html'),
  154. 'checked' => false,
  155. ),
  156. 'Submit' => array(
  157. 'type' => 'submit',
  158. 'name' => 'tester_submit',
  159. 'value' => _t("_Submit"),
  160. ),
  161. )
  162. );
  163. $oForm = new BxTemplFormView($aForm);
  164. return $sMsg . $oForm->getCode();
  165. }
  166. function log ($s)
  167. {
  168. bx_log('bx_smtp_mailer', $s);
  169. }
  170. }
  171. /** @} */