BxSMTPModule.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup SMTPMailer SMTP Mailer
  7. * @ingroup DolphinModules
  8. *
  9. * @{
  10. */
  11. bx_import('BxDolModule');
  12. bx_import('BxDolPaginate');
  13. bx_import('BxDolAlerts');
  14. require_once (BX_DIRECTORY_PATH_PLUGINS . "phpmailer/class.phpmailer.php");
  15. require_once (BX_DIRECTORY_PATH_PLUGINS . "phpmailer/class.smtp.php");
  16. class BxSMTPModule extends BxDolModule
  17. {
  18. function __construct(&$aModule)
  19. {
  20. parent::__construct($aModule);
  21. }
  22. function serviceSend ($sRecipientEmail, $sMailSubject, $sMailBody, $sMailHeader, $sMailParameters, $isHtml, $aRecipientInfo = array())
  23. {
  24. $iRet = true;
  25. if ($sRecipientEmail) {
  26. $mail = new PHPMailer(true);
  27. if ('on' == getParam('bx_smtp_on'))
  28. $mail->IsSMTP();
  29. //$mail->SMTPDebug = 2;
  30. $mail->CharSet = 'utf8';
  31. // smtp server auth or not
  32. $mail->SMTPAuth = 'on' == getParam('bx_smtp_auth') ? true : false;
  33. // from settings, smtp server secure ssl/tsl
  34. $sParamSecure = getParam('bx_smtp_secure');
  35. if ('SSL' == $sParamSecure || 'TSL' == $sParamSecure)
  36. $mail->SMTPSecure = "ssl";
  37. // from settings, smtp server
  38. $sParamHost = getParam('bx_smtp_host');
  39. if ($sParamHost)
  40. $mail->Host = $sParamHost;
  41. // smtp port 25, 465
  42. $sParamPort = getParam('bx_smtp_port');
  43. if ((int)$sParamPort > 0)
  44. $mail->Port = $sParamPort;
  45. // from settings, username and passord of smtp server
  46. $mail->Username = getParam ('bx_smtp_username');
  47. $mail->Password = getParam ('bx_smtp_password');
  48. $sParamSender = trim(getParam('bx_smtp_from_email'));
  49. if ($sParamSender)
  50. $mail->From = $sParamSender;
  51. else
  52. $mail->From = getParam('site_email_notify');
  53. // get site name or some other name as sender's name
  54. $mail->FromName = getParam ('bx_smtp_from_name');
  55. $mail->Subject = $sMailSubject;
  56. if ($isHtml) {
  57. $mail->Body = $sMailBody;
  58. $mail->AltBody = $isHtml ? strip_tags($sMailBody) : $sMailBody;
  59. } else {
  60. $mail->Body = $sMailBody;
  61. }
  62. $mail->WordWrap = 50; // set word wrap
  63. $mail->AddAddress($sRecipientEmail);
  64. // get attachments from attach directory
  65. if ('on' == getParam ('bx_smtp_send_attachments'))
  66. {
  67. if ($h = opendir(BX_DIRECTORY_PATH_MODULES . "boonex/smtpmailer/data/attach/"))
  68. {
  69. while (false !== ($sFile = readdir($h)))
  70. {
  71. if ($sFile == "." || $sFile == ".." || $sFile[0] == ".") continue;
  72. $mail->AddAttachment(BX_DIRECTORY_PATH_MODULES . "boonex/smtpmailer/data/attach/" . $sFile, $sFile);
  73. }
  74. closedir($h);
  75. }
  76. }
  77. $mail->IsHTML($isHtml ? true : false);
  78. try {
  79. $mail->Send();
  80. } catch (phpmailerException $e) {
  81. $iRet = false;
  82. $this->log("Mailer Error ($sRecipientEmail): " . $e->getMessage());
  83. }
  84. }
  85. //--- create system event [begin]
  86. $aAlertData = array(
  87. 'email' => $sRecipientEmail,
  88. 'subject' => $sMailSubject,
  89. 'body' => $sMailBody,
  90. 'header' => $sMailHeader,
  91. 'params' => $sMailParameters,
  92. 'html' => $isHtml,
  93. );
  94. bx_alert('profile', 'send_mail', $aRecipientInfo ? $aRecipientInfo['ID'] : 0, '', $aAlertData);
  95. //--- create system event [ end ]
  96. return $iRet;
  97. }
  98. function formTester()
  99. {
  100. $sMsg = '';
  101. if (isset($_POST['tester_submit']) && $_POST['tester_submit']) {
  102. $sRecipient = bx_process_pass(bx_get('recipient'));
  103. $sSubj = bx_process_pass(bx_get('subject'));
  104. $sBody = bx_process_pass(bx_get('body'));
  105. $isHTML = bx_get('html') == 'on' ? true : false;
  106. if (sendMail($sRecipient, $sSubj, $sBody, 0, array(), BX_EMAIL_SYSTEM, $isHTML ? 'html' : ''))
  107. $sMsg = MsgBox(_t('_bx_smtp_send_ok'));
  108. else
  109. $sMsg = MsgBox(_t('_bx_smtp_send_fail'));
  110. }
  111. $aForm = array(
  112. 'form_attrs' => array(
  113. 'method' => 'post',
  114. ),
  115. 'inputs' => array (
  116. 'recipient' => array(
  117. 'type' => 'text',
  118. 'name' => 'recipient',
  119. 'caption' => _t('_bx_smtp_recipient'),
  120. 'value' => '',
  121. ),
  122. 'subject' => array(
  123. 'type' => 'text',
  124. 'name' => 'subject',
  125. 'caption' => _t('_bx_smtp_subject'),
  126. 'value' => '',
  127. ),
  128. 'body' => array(
  129. 'type' => 'textarea',
  130. 'name' => 'body',
  131. 'caption' => _t('_bx_smtp_body'),
  132. 'value' => '',
  133. ),
  134. 'html' => array(
  135. 'type' => 'checkbox',
  136. 'name' => 'html',
  137. 'caption' => _t('_bx_smtp_is_html'),
  138. 'checked' => false,
  139. ),
  140. 'Submit' => array(
  141. 'type' => 'submit',
  142. 'name' => 'tester_submit',
  143. 'value' => _t("_Submit"),
  144. ),
  145. )
  146. );
  147. bx_import('BxTemplFormView');
  148. $oForm = new BxTemplFormView($aForm);
  149. return $sMsg . $oForm->getCode();
  150. }
  151. function log ($s)
  152. {
  153. $fn = BX_DIRECTORY_PATH_TMP . "smtp_mailer.log";
  154. $f = @fopen ($fn, 'a');
  155. if (!$f)
  156. return;
  157. fwrite ($f, date(DATE_RFC822) . "\t" . $s . "\n");
  158. fclose ($f);
  159. }
  160. }
  161. /** @} */