PageRenderTime 62ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/components/qdmail/QdmailSender.class.php

https://bitbucket.org/stk2k/charcoalphp2.1
PHP | 250 lines | 173 code | 33 blank | 44 comment | 23 complexity | 964c9d9ff93162689e8cf4226dd80801 MD5 | raw file
  1. <?php
  2. /**
  3. * ????????????
  4. *
  5. * PHP version 5
  6. *
  7. * @package components.mail
  8. * @author CharcoalPHP Development Team
  9. * @copyright 2008 - 2012 CharcoalPHP Development Team
  10. */
  11. require_once( 'qdmail.php' );
  12. require_once( 'qdsmtp.php' );
  13. require_once( 'QdmailException' . CHARCOAL_CLASS_FILE_SUFFIX );
  14. require_once( 'QdmailSmtpException' . CHARCOAL_CLASS_FILE_SUFFIX );
  15. require_once( 'QdmailAddress' . CHARCOAL_CLASS_FILE_SUFFIX );
  16. class Charcoal_QdmailSender extends Charcoal_CharcoalComponent implements Charcoal_IComponent
  17. {
  18. private $qdmail;
  19. /*
  20. * ???????
  21. */
  22. public function __construct()
  23. {
  24. parent::__construct();
  25. $this->qdmail = new Qdmail('utf-8' , 'base64');
  26. $this->qdmail->unitedCharset( 'UTF-8' );
  27. $this->qdmail->lineFeed( "\n" ); // ??????\n??????????????
  28. $this->qdmail->is_qmail = FALSE; // ??????\n??????????????
  29. }
  30. /*
  31. * Initialize instance
  32. *
  33. * @param Charcoal_Config $config configuration data
  34. */
  35. public function configure( Charcoal_Config $config )
  36. {
  37. // =========================================
  38. // QdMail??
  39. // =========================================
  40. // ????
  41. $qdmail_log_level = $config->getInteger( s('qdmail.log_level'), i(0) )->getValue();
  42. $qdmail_log_path = $config->getString( s('qdmail.log_path'), s('') )->getValue();
  43. $qdmail_log_filename = $config->getString( s('qdmail.log_filename'), s('') )->getValue();
  44. $this->qdmail->logLevel( $qdmail_log_level );
  45. $this->qdmail->logPath( $qdmail_log_path );
  46. $this->qdmail->logFilename( "/" . $qdmail_log_filename );
  47. // ???????
  48. $qdmail_error_display = $config->getBoolean( s('qdmail.error_display'), b(FALSE) )->getValue();
  49. $qdmail_error_log_level = $config->getInteger( s('qdmail.error_log_level'), i(0) )->getValue();
  50. $qdmail_error_log_path = $config->getString( s('qdmail.error_log_path'), s('') )->getValue();
  51. $qdmail_error_log_filename = $config->getString( s('qdmail.error_log_filename'), s('') )->getValue();
  52. $this->qdmail->errorDisplay( $qdmail_error_display );
  53. $this->qdmail->errorlogLevel( $qdmail_error_log_level );
  54. $this->qdmail->errorlogPath( $qdmail_error_log_path );
  55. $this->qdmail->errorlogFilename( "/" . $qdmail_error_log_filename );
  56. // =========================================
  57. // QdSMTP??
  58. // =========================================
  59. $qdsmtp = $config->getBoolean( s('qdsmtp'), b(FALSE) );
  60. if ( $qdsmtp->isFalse() ){
  61. $this->qdmail->smtp(false);
  62. }
  63. else{
  64. // ????
  65. $qdsmtp_log_level = $config->getInteger( s('qdsmtp.log_level'), i(0) )->getValue();
  66. $qdsmtp_log_filename = $config->getString( s('qdsmtp.log_filename'), s('') )->getValue();
  67. $qdsmtp_error_log_level = $config->getInteger( s('qdsmtp.error_log_level'), i(0) )->getValue();
  68. $qdsmtp_error_log_filename = $config->getString( s('qdsmtp.error_log_filename'), s('') )->getValue();
  69. $qdsmtp_error_display = $config->getBoolean( s('qdsmtp.error_display'), b(FALSE) )->getValue();
  70. $this->qdmail->smtpObject()->logLevel( $qdsmtp_log_level );
  71. $this->qdmail->smtpObject()->logFilename( $qdsmtp_log_filename );
  72. $this->qdmail->smtpObject()->errorlogLevel( $qdsmtp_error_log_level );
  73. $this->qdmail->smtpObject()->errorlogFilename( $qdsmtp_error_log_filename );
  74. $this->qdmail->smtpObject()->error_display = $qdsmtp_error_display;
  75. // ?????
  76. $qdsmtp_host = $config->getString( s('qdsmtp.host'), s('') )->getValue();
  77. $qdsmtp_port = $config->getString( s('qdsmtp.port'), s('') )->getValue();
  78. $qdsmtp_from = $config->getString( s('qdsmtp.from'), s('') )->getValue();
  79. $qdsmtp_protocol = $config->getString( s('qdsmtp.protocol'), s('') )->getValue();
  80. $qdsmtp_user = $config->getString( s('qdsmtp.user'), s('') )->getValue();
  81. $qdsmtp_pass = $config->getString( s('qdsmtp.pass'), s('') )->getValue();
  82. $options = array(
  83. 'host' => $qdsmtp_host,
  84. 'port' => $qdsmtp_port,
  85. 'from' => $qdsmtp_from,
  86. 'protocol' => $qdsmtp_protocol,
  87. 'user' => $qdsmtp_user,
  88. 'pass' => $qdsmtp_pass,
  89. );
  90. $this->qdmail->smtp(true);
  91. $result = $this->qdmail->smtpServer( $options );
  92. if ( $result === FALSE ){
  93. _throw( new Charcoal_QdmailException($this->qdmail) );
  94. }
  95. }
  96. log_debug( "debug, qdmail_sender", "component configs are:" . print_r($config,true) );
  97. }
  98. /*
  99. * ?????????
  100. */
  101. private function _getAddressList( $data )
  102. {
  103. $out = array();
  104. if ( is_string($data) || $data instanceof Charcoal_String ){
  105. // ???<???????>?????
  106. mb_regex_encoding('UTF-8');
  107. $pattern = '/(?P<label>.*)\<(?P<address>.*)\>/';
  108. $ret = preg_match( $pattern, $data, $match );
  109. log_debug( "debug, qdmail_sender", "preg_match data:" . print_r($data,true) );
  110. log_debug( "debug, qdmail_sender", "preg_match($ret):" . print_r($match,true) );
  111. if ( $ret ){
  112. // ?????
  113. $label = $match['label'];
  114. $address = $match['address'];
  115. $valid = Charcoal_MailUtil::validateAddress( s($address) );
  116. if ( !$valid ){
  117. _throw( new Charcoal_InvalidMailAddressException( s($address) ) );
  118. }
  119. $out[] = array( $address, $label );
  120. }
  121. else{
  122. // ???????????????
  123. $valid = Charcoal_MailUtil::validateAddress( s($data) );
  124. if ( !$valid ){
  125. _throw( new Charcoal_InvalidMailAddressException( s($data) ) );
  126. }
  127. $out[] = us($data);
  128. }
  129. }
  130. else if ( $data instanceof Charcoal_QdmailAddress ){
  131. // ??????????QdmailAddress???????
  132. $address = $data;
  133. if ( $address->hasLabel() ){
  134. $out[] = array( $address->getAddress(), $address->getLabel() );
  135. }
  136. else{
  137. $out[] = us( $address->getAddress() );
  138. }
  139. }
  140. else if ( is_array($data) ){
  141. // ?????
  142. foreach( $data as $item )
  143. {
  144. if ( is_string($item) || $item instanceof Charcoal_String ){
  145. $item = trim($item);
  146. // ???<???????>?????
  147. mb_regex_encoding('UTF-8');
  148. $pattern = '/(?P<label>.*)\<(?P<address>.*)\>/';
  149. $ret = preg_match( $pattern, $item, $match );
  150. log_debug( "debug, qdmail_sender", "preg_match item:" . print_r($item,true) );
  151. log_debug( "debug, qdmail_sender", "preg_match($ret):" . print_r($match,true) );
  152. if ( $ret ){
  153. // ?????
  154. $label = $match['label'];
  155. $address = $match['address'];
  156. $valid = Charcoal_MailUtil::validateAddress( s($address) );
  157. if ( !$valid ){
  158. _throw( new Charcoal_InvalidMailAddressException( s($address) ) );
  159. }
  160. $out[] = array( $address, $label );
  161. }
  162. else{
  163. // ?????
  164. $valid = Charcoal_MailUtil::validateAddress( s($item) );
  165. if ( !$valid ){
  166. _throw( new Charcoal_InvalidMailAddressException( s($item) ) );
  167. }
  168. $out[] = us($item);
  169. }
  170. }
  171. else if ( $item instanceof Charcoal_QdmailAddress ){
  172. // QdmailAddress???????
  173. $address = $item;
  174. if ( $address->hasLabel() ){
  175. $out[] = array( us($address->getAddress()), us($address->getLabel()) );
  176. }
  177. else{
  178. $out[] = us( $address->getAddress() );
  179. }
  180. }
  181. }
  182. }
  183. return $out;
  184. }
  185. /*
  186. * ?????????
  187. */
  188. public function sendMail( $from, $to, $subject, $body, $cc = NULL, $bcc = NULL)
  189. {
  190. log_debug( "debug, qdmail_sender", __CLASS__ . ":" . __METHOD__ . " start." );
  191. // ???
  192. $from_list = $this->_getAddressList( $from );
  193. $to_list = $this->_getAddressList( $to );
  194. if ( $cc ){
  195. $cc_list = $this->_getAddressList( $cc );
  196. }
  197. if ( $bcc ){
  198. $bcc_list = $this->_getAddressList( $bcc );
  199. }
  200. $this->qdmail->to( $to_list );
  201. $this->qdmail->from( $from_list );
  202. $this->qdmail->subject( $subject );
  203. $this->qdmail->text( $body );
  204. log_debug( "debug, qdmail_sender", "to:" . print_r($to_list,true) );
  205. log_debug( "debug, qdmail_sender", "from_list:" . print_r($from_list,true) );
  206. log_debug( "debug, qdmail_sender", "subject:" . print_r($subject,true) );
  207. log_debug( "debug, qdmail_sender", "body:" . print_r($body,true) );
  208. // ??
  209. $result = $this->qdmail->send();
  210. log_debug( "debug, qdmail_sender", "result:" . print_r($result,true) );
  211. if ( $result === FALSE ){
  212. _throw( new Charcoal_QdmailSmtpException($this->qdmail->smtpObject()) );
  213. }
  214. log_debug( "debug, qdmail_sender", __CLASS__ . ":" . __METHOD__ . " success." );
  215. }
  216. }
  217. return __FILE__;