1
0

BxSMTPModule.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. if (getParam('bx_smtp_oauth_on')) {
  54. $mail->AuthType = 'XOAUTH2';
  55. bx_import('MailAuthorization', $this->_aModule);
  56. bx_import('MailAuthorizationPHPMailer', $this->_aModule);
  57. $mail->setOAuth(BxSMTPMailAuthorizationPHPMailer::getInstance([
  58. 'mailAddress' => getParam('bx_smtp_from_email'),
  59. 'clientId' => getParam('bx_smtp_oauth_client_id'),
  60. 'tenantId' => getParam('bx_smtp_oauth_tenant_id'),
  61. 'tenantName' => getParam('bx_smtp_oauth_tenant_name'),
  62. 'x509Cert' => getParam('bx_smtp_oauth_cert_public'),
  63. 'x509Key' => getParam('bx_smtp_oauth_cert_private'),
  64. ]));
  65. }
  66. else {
  67. $mail->Username = getParam ('bx_smtp_username');
  68. $mail->Password = getParam ('bx_smtp_password');
  69. }
  70. if (!isset($aCustomHeaders['From'])) {
  71. $sParamSender = trim(getParam('bx_smtp_from_email'));
  72. if ($sParamSender)
  73. $mail->From = $sParamSender;
  74. else
  75. $mail->From = getParam('site_email_notify');
  76. // get site name or some other name as sender's name
  77. $mail->FromName = getParam ('bx_smtp_from_name');
  78. }
  79. if (!isset($aCustomHeaders['Subject']))
  80. $mail->Subject = $sMailSubject;
  81. if ($isHtml) {
  82. $mail->Body = $sMailBody;
  83. $mail->AltBody = $isHtml ? strip_tags($sMailBody) : $sMailBody;
  84. } else {
  85. $mail->Body = $sMailBody;
  86. }
  87. $mail->WordWrap = 50; // set word wrap
  88. if (getParam('bx_smtp_test_mode')) {
  89. $aCustomHeaders['X-Original-To'] = $sRecipientEmail;
  90. if (isset($aCustomHeaders['Subject']))
  91. $aCustomHeaders['Subject'] = getParam('bx_smtp_test_subj') . $aCustomHeaders['Subject'];
  92. else
  93. $mail->Subject = getParam('bx_smtp_test_subj') . $mail->Subject;
  94. }
  95. $mail->AddAddress(getParam('bx_smtp_test_mode') ? getParam('bx_smtp_test_email') : $sRecipientEmail);
  96. $mail->IsHTML($isHtml ? true : false);
  97. foreach ($aCustomHeaders as $sHeaderName => $sHeaderValue) {
  98. if ('From' == $sHeaderName) {
  99. if (preg_match('/(.+)<(.+)>/', $sHeaderValue, $m)) {
  100. $mail->setFrom($m[2], trim($m[1]), false);
  101. }
  102. else {
  103. $mail->setFrom($sHeaderValue, '', false);
  104. }
  105. }
  106. else {
  107. $mail->addCustomHeader($sHeaderName, $sHeaderValue);
  108. }
  109. }
  110. $mail->Send();
  111. } catch (PHPMailer\PHPMailer\Exception $e) {
  112. $iRet = false;
  113. $sErrorMessage = $e->getMessage();
  114. $this->log("Mailer Error ($sRecipientEmail): " . $sErrorMessage);
  115. }
  116. }
  117. //--- create system event [begin]
  118. $aAlertData = array(
  119. 'test_mode' => (bool)getParam('bx_smtp_test_mode'),
  120. 'email' => $sRecipientEmail,
  121. 'subject' => $sMailSubject,
  122. 'body' => $sMailBody,
  123. 'header' => $sMailHeader,
  124. 'params' => $sMailParameters,
  125. 'recipient' => $aRecipientInfo,
  126. 'html' => $isHtml,
  127. 'ret' => $iRet,
  128. 'error_message' => $sErrorMessage,
  129. );
  130. /**
  131. * @hooks
  132. * @hookdef hook-profile-send_mail 'profile', 'send_mail' - hook in $oAccount->isConfirmed check
  133. * - $unit_name - equals `profile`
  134. * - $action - equals `send_mail`
  135. * - $object_id - not used
  136. * - $sender_id - recipient profile_id
  137. * - $extra_params - array of additional params with the following array keys:
  138. * - `test_mode` - [bool] test mode or not
  139. * - `email` - [string] email
  140. * - `subject` - [string] letter subject
  141. * - `body` - [string] letter body
  142. * - `header` - [string] letter header
  143. * - `params` - [string] letter params
  144. * - `recipient` - [array] recipient info
  145. * - `html` - [bool] is html letter or not
  146. * - `ret` - [bool] sent successfuly or not
  147. * - `error_message` - [string] error message
  148. * @hook @ref hook-profile-send_mail
  149. */
  150. bx_alert('profile', 'send_mail', $aRecipientInfo && isset($aRecipientInfo['ID']) ? $aRecipientInfo['ID'] : 0, '', $aAlertData);
  151. //--- create system event [ end ]
  152. return $iRet;
  153. }
  154. function formTester()
  155. {
  156. $sMsg = '';
  157. if (isset($_POST['tester_submit']) && $_POST['tester_submit']) {
  158. $isHTML = bx_get('html') == 'on' ? true : false;
  159. $sRecipient = bx_process_pass(bx_get('recipient'));
  160. $sSubj = bx_get('subject');
  161. $sBody = bx_process_pass(bx_get('body'), $isHTML ? BX_DATA_HTML : BX_DATA_TEXT);
  162. if (sendMail($sRecipient, $sSubj, $sBody, 0, array(), BX_EMAIL_SYSTEM, $isHTML ? 'html' : ''))
  163. $sMsg = MsgBox(_t('_bx_smtp_send_ok'));
  164. else
  165. $sMsg = MsgBox(_t('_bx_smtp_send_fail'));
  166. }
  167. $aForm = array(
  168. 'form_attrs' => array(
  169. 'method' => 'post',
  170. ),
  171. 'inputs' => array (
  172. 'recipient' => array(
  173. 'type' => 'text',
  174. 'name' => 'recipient',
  175. 'caption' => _t('_bx_smtp_recipient'),
  176. 'value' => '',
  177. ),
  178. 'subject' => array(
  179. 'type' => 'text',
  180. 'name' => 'subject',
  181. 'caption' => _t('_bx_smtp_subject'),
  182. 'value' => '',
  183. ),
  184. 'body' => array(
  185. 'type' => 'textarea',
  186. 'name' => 'body',
  187. 'caption' => _t('_bx_smtp_body'),
  188. 'value' => '',
  189. ),
  190. 'html' => array(
  191. 'type' => 'checkbox',
  192. 'name' => 'html',
  193. 'caption' => _t('_bx_smtp_is_html'),
  194. 'checked' => false,
  195. ),
  196. 'Submit' => array(
  197. 'type' => 'submit',
  198. 'name' => 'tester_submit',
  199. 'value' => _t("_Submit"),
  200. ),
  201. )
  202. );
  203. $oForm = new BxTemplFormView($aForm);
  204. return $sMsg . $oForm->getCode();
  205. }
  206. function log ($s)
  207. {
  208. bx_log('bx_smtp_mailer', $s, BX_LOG_ERR);
  209. }
  210. }
  211. /** @} */