BxSMTPModule.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. if (getParam('bx_smtp_test_mode')) {
  74. $aCustomHeaders['X-Original-To'] = $sRecipientEmail;
  75. if (isset($aCustomHeaders['Subject']))
  76. $aCustomHeaders['Subject'] = getParam('bx_smtp_test_subj') . $aCustomHeaders['Subject'];
  77. else
  78. $mail->Subject = getParam('bx_smtp_test_subj') . $mail->Subject;
  79. }
  80. $mail->AddAddress(getParam('bx_smtp_test_mode') ? getParam('bx_smtp_test_email') : $sRecipientEmail);
  81. $mail->IsHTML($isHtml ? true : false);
  82. foreach ($aCustomHeaders as $sHeaderName => $sHeaderValue) {
  83. if ('From' == $sHeaderName) {
  84. if (preg_match('/(.+)<(.+)>/', $sHeaderValue, $m)) {
  85. $mail->setFrom($m[2], trim($m[1]), false);
  86. }
  87. else {
  88. $mail->setFrom($sHeaderValue, '', false);
  89. }
  90. }
  91. else {
  92. $mail->addCustomHeader($sHeaderName, $sHeaderValue);
  93. }
  94. }
  95. $mail->Send();
  96. } catch (PHPMailer\PHPMailer\Exception $e) {
  97. $iRet = false;
  98. $sErrorMessage = $e->getMessage();
  99. $this->log("Mailer Error ($sRecipientEmail): " . $sErrorMessage);
  100. }
  101. }
  102. //--- create system event [begin]
  103. $aAlertData = array(
  104. 'test_mode' => (bool)getParam('bx_smtp_test_mode'),
  105. 'email' => $sRecipientEmail,
  106. 'subject' => $sMailSubject,
  107. 'body' => $sMailBody,
  108. 'header' => $sMailHeader,
  109. 'params' => $sMailParameters,
  110. 'recipient' => $aRecipientInfo,
  111. 'html' => $isHtml,
  112. 'ret' => $iRet,
  113. 'error_message' => $sErrorMessage,
  114. );
  115. /**
  116. * @hooks
  117. * @hookdef hook-profile-send_mail 'profile', 'send_mail' - hook in $oAccount->isConfirmed check
  118. * - $unit_name - equals `profile`
  119. * - $action - equals `send_mail`
  120. * - $object_id - not used
  121. * - $sender_id - recipient profile_id
  122. * - $extra_params - array of additional params with the following array keys:
  123. * - `test_mode` - [bool] test mode or not
  124. * - `email` - [string] email
  125. * - `subject` - [string] letter subject
  126. * - `body` - [string] letter body
  127. * - `header` - [string] letter header
  128. * - `params` - [string] letter params
  129. * - `recipient` - [array] recipient info
  130. * - `html` - [bool] is html letter or not
  131. * - `ret` - [bool] sent successfuly or not
  132. * - `error_message` - [string] error message
  133. * @hook @ref hook-profile-send_mail
  134. */
  135. bx_alert('profile', 'send_mail', $aRecipientInfo && isset($aRecipientInfo['ID']) ? $aRecipientInfo['ID'] : 0, '', $aAlertData);
  136. //--- create system event [ end ]
  137. return $iRet;
  138. }
  139. function formTester()
  140. {
  141. $sMsg = '';
  142. if (isset($_POST['tester_submit']) && $_POST['tester_submit']) {
  143. $isHTML = bx_get('html') == 'on' ? true : false;
  144. $sRecipient = bx_process_pass(bx_get('recipient'));
  145. $sSubj = bx_get('subject');
  146. $sBody = bx_process_pass(bx_get('body'), $isHTML ? BX_DATA_HTML : BX_DATA_TEXT);
  147. if (sendMail($sRecipient, $sSubj, $sBody, 0, array(), BX_EMAIL_SYSTEM, $isHTML ? 'html' : ''))
  148. $sMsg = MsgBox(_t('_bx_smtp_send_ok'));
  149. else
  150. $sMsg = MsgBox(_t('_bx_smtp_send_fail'));
  151. }
  152. $aForm = array(
  153. 'form_attrs' => array(
  154. 'method' => 'post',
  155. ),
  156. 'inputs' => array (
  157. 'recipient' => array(
  158. 'type' => 'text',
  159. 'name' => 'recipient',
  160. 'caption' => _t('_bx_smtp_recipient'),
  161. 'value' => '',
  162. ),
  163. 'subject' => array(
  164. 'type' => 'text',
  165. 'name' => 'subject',
  166. 'caption' => _t('_bx_smtp_subject'),
  167. 'value' => '',
  168. ),
  169. 'body' => array(
  170. 'type' => 'textarea',
  171. 'name' => 'body',
  172. 'caption' => _t('_bx_smtp_body'),
  173. 'value' => '',
  174. ),
  175. 'html' => array(
  176. 'type' => 'checkbox',
  177. 'name' => 'html',
  178. 'caption' => _t('_bx_smtp_is_html'),
  179. 'checked' => false,
  180. ),
  181. 'Submit' => array(
  182. 'type' => 'submit',
  183. 'name' => 'tester_submit',
  184. 'value' => _t("_Submit"),
  185. ),
  186. )
  187. );
  188. $oForm = new BxTemplFormView($aForm);
  189. return $sMsg . $oForm->getCode();
  190. }
  191. function log ($s)
  192. {
  193. bx_log('bx_smtp_mailer', $s, BX_LOG_ERR);
  194. }
  195. }
  196. /** @} */