BxSMTPModule.php 6.8 KB

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