BxSMTPModule.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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/class.phpmailer.php");
  12. require_once (BX_DIRECTORY_PATH_PLUGINS . "phpmailer/class.smtp.php");
  13. class BxSMTPModule extends BxDolModule
  14. {
  15. function __construct(&$aModule)
  16. {
  17. parent::__construct($aModule);
  18. }
  19. function serviceSend ($sRecipientEmail, $sMailSubject, $sMailBody, $sMailHeader, $sMailParameters, $isHtml, $aRecipientInfo = array(), $aCustomHeaders = array())
  20. {
  21. $iRet = true;
  22. if ($sRecipientEmail) {
  23. $mail = new PHPMailer(true);
  24. if ('on' == getParam('bx_smtp_on'))
  25. $mail->IsSMTP();
  26. // $mail->SMTPDebug = 2;
  27. $mail->CharSet = 'utf8';
  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. try {
  89. $mail->Send();
  90. } catch (phpmailerException $e) {
  91. $iRet = false;
  92. $this->log("Mailer Error ($sRecipientEmail): " . $e->getMessage());
  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. );
  105. bx_alert('profile', 'send_mail', $aRecipientInfo && isset($aRecipientInfo['ID']) ? $aRecipientInfo['ID'] : 0, '', $aAlertData);
  106. //--- create system event [ end ]
  107. return $iRet;
  108. }
  109. function formTester()
  110. {
  111. $sMsg = '';
  112. if (isset($_POST['tester_submit']) && $_POST['tester_submit']) {
  113. $isHTML = bx_get('html') == 'on' ? true : false;
  114. $sRecipient = bx_process_pass(bx_get('recipient'));
  115. $sSubj = bx_process_pass(bx_get('subject'));
  116. $sBody = bx_process_pass(bx_get('body'), $isHTML ? BX_DATA_HTML : BX_DATA_TEXT);
  117. if (sendMail($sRecipient, $sSubj, $sBody, 0, array(), BX_EMAIL_SYSTEM, $isHTML ? 'html' : ''))
  118. $sMsg = MsgBox(_t('_bx_smtp_send_ok'));
  119. else
  120. $sMsg = MsgBox(_t('_bx_smtp_send_fail'));
  121. }
  122. $aForm = array(
  123. 'form_attrs' => array(
  124. 'method' => 'post',
  125. ),
  126. 'inputs' => array (
  127. 'recipient' => array(
  128. 'type' => 'text',
  129. 'name' => 'recipient',
  130. 'caption' => _t('_bx_smtp_recipient'),
  131. 'value' => '',
  132. ),
  133. 'subject' => array(
  134. 'type' => 'text',
  135. 'name' => 'subject',
  136. 'caption' => _t('_bx_smtp_subject'),
  137. 'value' => '',
  138. ),
  139. 'body' => array(
  140. 'type' => 'textarea',
  141. 'name' => 'body',
  142. 'caption' => _t('_bx_smtp_body'),
  143. 'value' => '',
  144. ),
  145. 'html' => array(
  146. 'type' => 'checkbox',
  147. 'name' => 'html',
  148. 'caption' => _t('_bx_smtp_is_html'),
  149. 'checked' => false,
  150. ),
  151. 'Submit' => array(
  152. 'type' => 'submit',
  153. 'name' => 'tester_submit',
  154. 'value' => _t("_Submit"),
  155. ),
  156. )
  157. );
  158. $oForm = new BxTemplFormView($aForm);
  159. return $sMsg . $oForm->getCode();
  160. }
  161. function log ($s)
  162. {
  163. $fn = BX_DIRECTORY_PATH_ROOT . "logs/smtp_mailer.log";
  164. $f = @fopen ($fn, 'a');
  165. if (!$f)
  166. return;
  167. fwrite ($f, date(DATE_RFC822) . "\t" . $s . "\n");
  168. fclose ($f);
  169. }
  170. }
  171. /** @} */