PageRenderTime 85ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/src/system/application/libraries/qdmail.php

https://bitbucket.org/seezoo/seezoo/
PHP | 4120 lines | 3564 code | 216 blank | 340 comment | 530 complexity | e2ec42afa2bdbd6048c4784be5743f2f MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * Qdmail ver 1.2.6b
  4. * E-Mail for multibyte charset
  5. *
  6. * PHP versions 4 and 5 (PHP4.3 upper)
  7. *
  8. * Copyright 2008, Spok in japan , tokyo
  9. * hal456.net/qdmail : http://hal456.net/qdmail/
  10. * & CPA-LAB/Technical : http://www.cpa-lab.com/tech/
  11. * Licensed under The MIT License License
  12. *
  13. * @copyright Copyright 2008, Spok.
  14. * @link http://hal456.net/qdmail/
  15. * @version 1.2.6b
  16. * @lastmodified 2008-10-23
  17. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  18. *
  19. * Qdmail is sending e-mail library for multibyte language ,
  20. * easy , quickly , usefull , and you can specify deeply the details.
  21. * Copyright (C) 2008 spok
  22. */
  23. //-------------------------------------------
  24. // For CakePHP , extended 'Object' Class ,
  25. // if including in CakePHP Component .
  26. // At normal PHP Script or another Framework ,
  27. // 'QdmailBranch' means Dummy Base Class .
  28. //-------------------------------------------
  29. if (!defined('QD_DS')) {
  30. define('QD_DS', DIRECTORY_SEPARATOR);
  31. }
  32. if ( defined('CAKE_CORE_INCLUDE_PATH') || defined('CAKE')) {
  33. class QdmailBranch extends Object{
  34. }
  35. }else{
  36. class QdmailBranch{
  37. }
  38. }
  39. if( !function_exists( 'qd_send_mail' ) ){
  40. function qd_send_mail( $type , $to = null, $subject = null , $content = null , $other_header = array() , $attach = null, $debug = 0 ){
  41. $type_org = $type;
  42. $mail = Qdmail::getInstance();
  43. $mail->debug = $debug;
  44. if(!is_array($type)){
  45. $type = array('TYPE'=>$type);
  46. }
  47. list( $type , $link ) = $mail->keyUpper($type);
  48. $option = array();
  49. $return = array();
  50. $type = array_change_key_case( $type , CASE_UPPER ) ;
  51. $option = (isset($type['OPTION']) && is_array($type['OPTION'])) ? $type['OPTION'] : array(); $return = (isset($type['RETURN']) && is_array($type['RETURN'])) ? $type['RETURN'] : array();
  52. if(isset($type['SMTP'])){
  53. $option = array_merge($option,array('SMTP'=>true,'smtpServer'=>$type['SMTP']));
  54. }
  55. $type = isset($type['TYPE']) ? $type['TYPE']:'text';
  56. $_type=array('TEXT'=>'Text','HTML'=>'Html','DECO'=>'Deco' ,'DECOTEMPLATE'=>'DecoTemplate');
  57. $easy_method = isset($_type[strtoupper($type)]) ? 'easy'.$_type[strtoupper($type)]:'_';
  58. if(!method_exists($mail,$easy_method)){
  59. $mail -> errorGather('Illegal type \''.$type.'\'',__LINE__);
  60. return false;
  61. }
  62. $ret = $mail->{$easy_method}( $to , $subject , $content , $other_header , $attach , $option );
  63. foreach($return as $method => $value ){
  64. if(method_exists($mail,$method)){
  65. $type_org[$link['RETURN']][$method] = $mail -> {$method}($value);
  66. }
  67. }
  68. if(0!==count($return)){
  69. $type_org[$link['RETURN']]['qd_send_mail'] = $ret;
  70. $ret = $type_org;
  71. }
  72. return $ret;
  73. }
  74. }
  75. class QdmailBase extends QdmailBranch{
  76. //----------------------------
  77. // Default Language
  78. // If you do not Japanese
  79. // Please change this propaty for your Language and Encoding
  80. //----------------------------
  81. var $lang_def = "ja";
  82. var $encoding_def = "utf-8";
  83. var $detect_def = array('ASCII','JIS','UTF-8','EUC-JP','SJIS');
  84. var $mb_parameter_stack = null;
  85. var $united_charset = null;
  86. var $mime_encode_max = 75;
  87. //------------------------
  88. // Time Zone , Message Id
  89. //------------------------
  90. var $time_zone = null; // '+0900' in Japan
  91. var $message_id = true;
  92. var $salt = 'qdmail';
  93. var $message_id_right = null;
  94. //----------------------------
  95. // Line Feed Character & kana
  96. //----------------------------
  97. var $LFC = "\r\n";// Notice: CRLF ,If you failed, change to "\n"
  98. var $LFC_Qmail = null;
  99. var $is_qmail = null;
  100. var $language = 'ja';
  101. var $kana = false; // kana header
  102. //----------
  103. // sysytem
  104. //----------
  105. var $kana_content_relation = false;
  106. var $name = 'Qdmail';
  107. var $version = '1.2.6b';
  108. var $xmailer = 'PHP-Qdmail';
  109. var $license = 'The_MIT_License';
  110. //--------------------
  111. // charset , encoding
  112. //--------------------
  113. var $charset_header = 'iso-2022-jp';
  114. var $charset_content = 'iso-2022-jp';
  115. var $charset_attach_filename = 'iso-2022-jp';
  116. var $content_transfer_enc_text = '7bit';
  117. var $content_transfer_enc_html = '7bit';
  118. var $detect_order = false;
  119. // var $detect_order = "iso-2022-jp,eucJP-win,UTF-8,SJIS-win,jis,ASCII";
  120. var $qdmail_system_charset = 'utf-8';
  121. var $force_change_charset = false;
  122. var $corres_charset = array(
  123. 'HEADER'=>'charset_header',
  124. 'BODY'=>'charset_content',
  125. 'ATTACH'=>'charset_attach_filename',
  126. 'TEXT'=>'content_transfer_enc_text',
  127. 'HTML'=>'content_transfer_enc_html',
  128. 'DETECT'=>'detect_order',
  129. 'SYSTEM'=>'qdmail_system_charset',
  130. ) ;
  131. //--------------------------
  132. // for address
  133. //--------------------------
  134. var $varidate_address_regex = '/[^@]+@[^@]+/';
  135. var $allow_blank_header = false;
  136. var $addr_many = array(
  137. 'TO' => true,
  138. 'CC' => true,
  139. 'BCC' => true,
  140. 'FROM' => false,
  141. 'REPLYTO'=> false
  142. );
  143. var $addr_head_name = array(
  144. 'TO' => 'To',
  145. 'CC' => 'Cc',
  146. 'BCC' => 'Bcc',
  147. 'FROM' => 'From',
  148. 'REPLYTO'=>'Reply-To'
  149. );
  150. var $header_must =array( 'TO' , 'FROM' , 'SUBJECT' );
  151. var $body_empty_allow = false;
  152. var $tokey = array(
  153. '_ADDR' => 'mail',
  154. '_NAME' => 'name',
  155. );
  156. //--------------
  157. // content_id
  158. //--------------
  159. var $content_id_include_attach_path = false ;
  160. var $content_id_only_filename = true ;
  161. //---------------
  162. // Once mode
  163. //---------------
  164. var $body_structure = array();
  165. var $body_build_once = false;
  166. var $body_already_build = false;
  167. var $attach_build_once = true;
  168. var $attach_already_build = false;
  169. //------------------------------
  170. // simple replace
  171. //------------------------------
  172. var $simple_replace = false;
  173. var $replace = array();
  174. var $replace_with_to_priority= true;
  175. var $replace_def = array();
  176. // simple replace command prefix
  177. var $rep_prefix = null;
  178. //---------
  179. // wordwrap
  180. //---------
  181. var $wordwrap_allow = false;
  182. var $wrap_prohibit_allow = false;
  183. var $wordwrap_length= 45 ;
  184. // inteligent wordwrap
  185. // false is that the word exist in the line ,
  186. // true is that the word must be the beginning of a line
  187. var $wrap_except = array(
  188. 'http://'=>false,
  189. 'code'=>true,
  190. );
  191. var $wrap_prohibit_top=',.;:–?!‼、。.))]}}〕〉》」』】〙〗〟’”»ヽヾーァィゥェォッャュョヮヵヶぁぃぅぇぉっゃゅょゎ‐〜?!';
  192. var $wrap_prohibit_end='(([{{〔〈《「『【〘〖‘“« ';
  193. var $wrap_prohibit = array();
  194. // multibyte wordwrap , by wordcount or by wordwidth
  195. var $wrap_width = true;
  196. // multibyte wordwidth compare by ascii
  197. var $mb_strwidth_magni = 2;
  198. //------------------
  199. // To Separate mode
  200. //------------------
  201. var $to_separate = false ;
  202. //----------------------------
  203. // html mail
  204. //----------------------------
  205. var $is_html = null ;
  206. var $auto_both = true ; // text & html
  207. var $inline_mode = false;
  208. var $deco_kind = null ; // number of $this->deco_def
  209. var $auto_deco_judge= false;
  210. var $no_inline_attach_structure = 0;
  211. var $deco_def_default = 0;
  212. var $deco_def =array(
  213. array(
  214. 'OPTION_NAME' => array( 'MHTML' , 'INLINE' , 'PC' ),
  215. 'STRUCTURE' => 1,
  216. '_CHARSET' => 'iso-2022-jp' ,
  217. 'ENC_TEXT' => '7bit',
  218. 'ENC_HTML' => 'QUOTED-PRINTABLE',
  219. 'HTML_EXTERNAL' => false,
  220. 'DISPOSITION' => true,
  221. ),
  222. array(
  223. 'OPTION_NAME' => array( 'DC' , 'DOCOMO' ),
  224. 'STRUCTURE' => 2,
  225. '_CHARSET' => 'iso-2022-jp',
  226. 'ENC_TEXT' => '7bit',
  227. 'ENC_HTML' => 'QUOTED-PRINTABLE',
  228. 'HTML_EXTERNAL' => array('this','stripCrlf'),
  229. 'DISPOSITION' => false,
  230. ),
  231. array(
  232. 'OPTION_NAME' => array( 'AU' ,'EZ', 'EZWEB'),
  233. 'STRUCTURE' => 3,
  234. '_CHARSET' => 'iso-2022-jp',
  235. 'ENC_TEXT' => '7bit',
  236. 'ENC_HTML' => 'QUOTED-PRINTABLE',
  237. 'HTML_EXTERNAL' => array('this','stripCrlf'),
  238. 'DISPOSITION' => true,
  239. ),
  240. array(
  241. 'OPTION_NAME' => array( 'SB' , 'SOFTBANK' ),
  242. 'STRUCTURE' => 4,
  243. '_CHARSET' => 'iso-2022-jp',
  244. 'ENC_TEXT' => '7bit',
  245. 'ENC_HTML' => 'QUOTED-PRINTABLE',
  246. 'HTML_EXTERNAL' => array('this','stripCrlf') ,
  247. 'DISPOSITION' => true ,
  248. ),
  249. array(
  250. 'OPTION_NAME' => array( 'EM','EMOBILE' ,'EMNET'),
  251. 'STRUCTURE' => 2,
  252. '_CHARSET' => 'iso-2022-jp',
  253. 'ENC_TEXT' => '7bit',
  254. 'ENC_HTML' => 'QUOTED-PRINTABLE',
  255. 'HTML_EXTERNAL' => array('this','stripCrlf') ,
  256. 'DISPOSITION' => true ,
  257. ),
  258. array(
  259. 'OPTION_NAME' => array( 'WL','WILLCOM' ,'POCKET'),
  260. 'STRUCTURE' => 2,
  261. '_CHARSET' => 'iso-2022-jp',
  262. 'ENC_TEXT' => '7bit',
  263. 'ENC_HTML' => 'QUOTED-PRINTABLE',
  264. 'HTML_EXTERNAL' => array('this','stripCrlf') ,
  265. 'DISPOSITION' => true ,
  266. ),
  267. array(
  268. 'OPTION_NAME' => array( 'TEMPLATE_DC' , 'TEMPLATE_DOCOMO' ,'TPL_DC'),
  269. 'STRUCTURE' => 5,
  270. '_CHARSET' => 'Shift_JIS',
  271. 'ENC_TEXT' => '8bit',
  272. 'ENC_HTML' => '8bit',
  273. 'HTML_EXTERNAL' => array('this','stripCrlf'),
  274. 'DISPOSITION' => false,
  275. 'BOUNDARY' => 'mime9DC9bdary',
  276. 'TOP' => array('Decomail-Template'),
  277. 'CID_PREFIX' => 'img_',
  278. 'CID_NUM_COL' => 3,
  279. ),
  280. array(
  281. 'OPTION_NAME' => array( 'TEMPLATE_AU','TPL_AU' ,'TPL_AU_2_0'),
  282. 'STRUCTURE' => 6,
  283. '_CHARSET' => 'iso-2022-jp',
  284. 'ENC_TEXT' => '7bit',
  285. 'ENC_HTML' => '7bit',
  286. 'HTML_EXTERNAL' => array('this','stripCrlf'),
  287. 'DISPOSITION' => false,
  288. 'BOUNDARY' => '--=_KDDI_NEXT_PART_0000',
  289. 'TOP' => array('KDDI_HTML_MAIL_2_0'),
  290. 'CID_PREFIX' => 'img_cid_',
  291. 'CID_NUM_COL' => 3,
  292. ),
  293. array(
  294. 'OPTION_NAME' => array( 'TEMPLATE_AU_1_0','TPL_AU_1_0' ,'TPL_AU_1_0'),
  295. 'STRUCTURE' => 6,
  296. '_CHARSET' => 'iso-2022-jp',
  297. 'ENC_TEXT' => '7bit',
  298. 'ENC_HTML' => '7bit',
  299. 'HTML_EXTERNAL' => array('this','stripCrlf'),
  300. 'DISPOSITION' => false,
  301. 'BOUNDARY' => '--=_KDDI_NEXT_PART_0000',
  302. 'TOP' => array('KDDI_HTML_MAIL_1_0'),
  303. 'CID_PREFIX' => 'img_cid_',
  304. 'CID_NUM_COL' => 3,
  305. ),
  306. array(
  307. 'OPTION_NAME' => array( 'TEMPLATE_SB','TPL_SB' ),
  308. 'STRUCTURE' => 5,
  309. '_CHARSET' => 'Shift_JIS',
  310. 'ENC_TEXT' => '8bit',
  311. 'ENC_HTML' => '8bit',
  312. 'HTML_EXTERNAL' => array('this','stripCrlf'),
  313. 'DISPOSITION' => false,
  314. 'BOUNDARY' => 'aremejkj15a14',
  315. 'TOP' => array('HTMLMail-Template-Version:1.0',
  316. 'HTMLMail-Template-Title:HTMLMail-Template',
  317. ''
  318. ),
  319. 'CID_PREFIX' => '',
  320. 'CID_NUM_COL' => 2,
  321. 'CID_AFTER' => '@areme.jp',
  322. ),
  323. );
  324. var $structure =array(
  325. // no inline attachment
  326. 0 => array(
  327. 'multipart/mixed' => array(
  328. 'multipart/alternative'=>array(
  329. 'html' => 1,
  330. 'plain' => 1,
  331. 'OMIT' => true,
  332. ),
  333. 'image' => 'BOTH', // Available Inline
  334. 'OMIT' => true,
  335. ),
  336. 'OMIT' => true,
  337. ),
  338. // PC inline HTML
  339. 1 => array(
  340. 'multipart/mixed' => array(
  341. 'multipart/alternative'=>array(
  342. 'multipart/related' => array(
  343. 'html' => 1,
  344. 'image' => 'INLINE',
  345. 'OMIT' => true,
  346. ),
  347. 'plain' => 1,
  348. 'OMIT' => true,
  349. ),
  350. 'image' => 'NOT_INLINE', // not inline
  351. 'OMIT' => true,
  352. ),
  353. 'OMIT' => true,
  354. ),
  355. 2 => array(
  356. 'multipart/mixed' => array(
  357. 'multipart/related'=>array(
  358. 'multipart/alternative' => array(
  359. 'plain' => 1,
  360. 'html' => 1,
  361. 'OMIT' => false,
  362. ),
  363. 'image' => 'INLINE',
  364. 'OMIT' => false,
  365. ),
  366. 'OMIT' => false,
  367. 'image' => 'NOT_INLINE',
  368. ),
  369. 'OMIT' => false,
  370. ),
  371. 3 => array(
  372. 'multipart/mixed' => array(
  373. 'multipart/alternative' => array(
  374. 'plain' => 1,
  375. 'html' => 1,
  376. 'OMIT' => false,
  377. ),
  378. 'image' => 'BOTH',
  379. 'OMIT' => false,
  380. ),
  381. 'OMIT' => false,
  382. ),
  383. 4 => array(
  384. 'multipart/related'=>array(
  385. 'multipart/alternative' => array(
  386. 'plain' => 1,
  387. 'html' => 1,
  388. 'OMIT' => false,
  389. ),
  390. 'image' => 'INLINE',
  391. 'OMIT' => false,
  392. ),
  393. 'image' => 'NOT_INLINE',
  394. 'OMIT' => false,
  395. ),
  396. 5 => array(
  397. 'multipart/related'=>array(
  398. 'plain' => 1,
  399. 'html' => 1,
  400. 'image' => 'INLINE',
  401. 'OMIT' => false,
  402. ),
  403. 'OMIT' => false,
  404. ),
  405. 6 => array(
  406. 'multipart/mixed'=>array(
  407. 'plain' => 1,
  408. 'html' => 1,
  409. 'image' => 'INLINE',
  410. 'OMIT' => false,
  411. ),
  412. 'OMIT' => false,
  413. ),
  414. );
  415. var $deco_judge = array(
  416. 'docomo.ne.jp' => 'DC',
  417. 'softbank.ne.jp' => 'SB',
  418. 'i.softbank.ne.jp' => 'SB',
  419. 'disney.ne.jp' => 'SB',
  420. 'vodafone.ne.jp' => 'SB',
  421. 'ezweb.ne.jp' => 'AU',
  422. 'emnet.ne.jp' => 'EM',
  423. 'pdx.ne.jp' => 'WL',
  424. 'gmail.com' => 'DC',
  425. );
  426. //------------------
  427. // using address and content
  428. //------------------
  429. var $to = array() ;
  430. var $from = array() ;
  431. var $cc = array() ;
  432. var $bcc = array() ;
  433. var $done = array() ;
  434. var $undone = array() ;
  435. var $replyto = array() ;
  436. var $recipient = array() ;
  437. var $allways_bcc = null ;
  438. var $header = array() ;
  439. var $other_header = array() ;
  440. var $header_content_type = array();
  441. var $content = array(
  442. 'TEXT'=>array(
  443. 'CONTENT' => null,
  444. 'LENGTH' => null,
  445. '_CHARSET' => null,
  446. 'ENC' => null,
  447. '_ORG_CHARSET' => null,
  448. ),
  449. 'HTML'=>array(
  450. 'CONTENT' => null,
  451. 'ORG_CONTENT' => null,
  452. 'LENGTH' => null,
  453. '_CHARSET' => null,
  454. 'ENC' => null,
  455. '_ORG_CHARSET' => null,
  456. ),
  457. );
  458. var $header_for_mailfunction_to;
  459. var $header_for_mailfunction_subject;
  460. var $header_for_mailfunction_other;
  461. var $content_for_mailfunction;
  462. var $header_for_smtp_array;
  463. var $content_all_for_smtp;
  464. var $header_for_smtp;
  465. //--------------
  466. // attachament
  467. //--------------
  468. var $attach = array();
  469. var $attach_path = null;
  470. var $auto_ext = true ; // mimetypes
  471. var $content_id_fix = false;
  472. //------------------------
  473. // Mailer
  474. //-------------------------
  475. var $mailer = 'mail';
  476. //------------------------
  477. // Sendmail
  478. //-------------------------
  479. var $sendmail = false ;
  480. var $sendmail_path = null;
  481. //------------------------
  482. // SMTP
  483. //-------------------------
  484. var $smtp = false ;
  485. var $smtp_object = null;
  486. var $smtp_loglevel_link = false;
  487. var $smtp_server = array(
  488. 'host' => null ,
  489. 'port' => 25 ,
  490. 'from' => null,
  491. 'user' => null,
  492. 'pass' => null,
  493. 'protocol' => null,
  494. 'pop_host' => null,
  495. 'pop_user' => null,
  496. 'pop_pass' => null,
  497. );
  498. //------------------------
  499. // render Mode
  500. //------------------------
  501. var $render_mode = false;
  502. var $size = array();
  503. //------------------------
  504. // Priority
  505. //------------------------
  506. var $priority = null;
  507. var $priority_def =array(
  508. 'X-Priority' => array( 'HIGH' => 1 , 'NORMAL' => 3 , 'LOW' =>5 ),
  509. 'X-MsMail-Priotiry' => array( 'HIGH'=>'High' , 'NORMAL'=>'Normal' , 'LOW'=>'Low' ),
  510. 'Priotiry' => array( 'HIGH'=>'urgent' , 'NORMAL' => 'normal' , 'LOW'=> 'non-urgent' ),
  511. 'Importance' => array( 'HIGH' =>'High' , 'NORMAL'=>'Normal' ,'LOW' =>'Low' ),
  512. );
  513. //------------------------
  514. // signed
  515. //------------------------
  516. var $sign = false;
  517. var $smime = false;
  518. var $pgp = false;
  519. var $private_key_file = 'private.pem';
  520. var $certificate_file = 'cert.pem';
  521. var $certificate_pass = null;
  522. var $certificate_file_path = null;
  523. var $certificate_temp_path = null;
  524. var $certificate_file_path_win = 'c:\cert';
  525. var $certificate_temp_path_win = 'c:\temp';
  526. var $certificate_file_path_unix = '/user/local/cert';
  527. var $certificate_temp_path_unix = '/tmp';
  528. //------------------------
  529. // etc
  530. //------------------------
  531. var $temporary_path = null;
  532. var $simple_attach = false;
  533. var $keep_parameter = array(false);
  534. var $mta_option = null ;
  535. var $is_create = false;
  536. var $address_validation_method = array('this','validateAddr');
  537. var $boundary_base_degit= 2 ;
  538. var $stack_construct = null ;
  539. var $start_time = null;
  540. var $framework = null;
  541. //-------------------------------
  542. // logs
  543. // 0 is nolog,
  544. // 1 is simple(Message 'Success' & recipt e-mail@address ),
  545. // 2 is including header data,
  546. // 3 is including fulldata,
  547. //------------------------------
  548. var $log_level = 0 ;
  549. var $log_level_max = 3 ;
  550. var $log_path = './';
  551. var $log_filename = 'qdmail.log';
  552. var $log_append = 'a' ;
  553. var $log_dateformat = 'Y-m-d H:i:s';
  554. var $log_LFC = "\n";
  555. // -------------------------------
  556. // error & error logs
  557. // 0 is nolog,
  558. // 1 is simple,
  559. // 2 is including header data,
  560. // 3 is inc fulldata
  561. //--------------------------------
  562. var $error = array();
  563. var $error_stack = array();
  564. var $error_display = true;
  565. var $errorlog_level = 0 ;
  566. var $errorlog_level_max = 3 ;
  567. var $errorlog_path = './';
  568. var $errorlog_filename= 'qbmail_error.log';
  569. var $errorlog_append= 'a' ;
  570. var $ignore_error = false ;
  571. //----------------
  572. // debug
  573. // 0 is no debug mode & really sending ,
  574. // 1 is showing header&body & really sending ,
  575. // 2 is no sending & showing header&body and some vars
  576. //----------------
  577. var $debug = 0 ;
  578. var $debug_report = false;
  579. var $debug_report_path = './';
  580. var $debug_echo_charset= true;
  581. //****************************************************
  582. // Methods
  583. //****************************************************
  584. //--------------------------------
  585. // constructor set error display
  586. // $charset_def = null,
  587. // $error_display = true
  588. // $mail -> (&) new Qdmail( Charset , Encoding , DetectOrder , error_display );
  589. //--------------------------------
  590. function __construct( $param = null ){
  591. $this->stack_construct = $param ;
  592. if( !empty( $param[0] ) && !empty( $param[1] ) ){
  593. $this->charset( $param[0] , $param[1] );
  594. }elseif( !empty( $param[0] ) ){
  595. $this->charset( $param[0] );
  596. }
  597. if( !empty( $param[2] ) ){
  598. $this->detect_order = $param[1];
  599. }
  600. if( false !== $this->detect_order ){
  601. $this->qd_detect_order( $this->detect_order );
  602. }
  603. if( !empty( $param[3] ) ){
  604. $this->error_display = $param[2];
  605. }
  606. if(is_null($this->LFC)){
  607. $this->LFC = chr(13) . chr(10);
  608. }
  609. if(is_null($this->LFC_Qmail)){
  610. $this->LFC_Qmail = chr(10);
  611. }
  612. if($this->isQmail()){
  613. $this->LFC = $this->LFC_Qmail;
  614. }
  615. $this->optionNameLink();
  616. $this->wordwrapProhibitConstruct();
  617. $this->sendmail_path = ini_get("sendmail_path");
  618. }
  619. function & getInstance(){
  620. static $instance = array();
  621. if( isset($instance[0]) && is_object($instance[0]) ){
  622. $keep = $instance[0]->keep_parameter;
  623. if( is_string($keep[0]) ){
  624. $stack = array();
  625. foreach($keep as $method){
  626. if( !is_string( $method ) || !method_exists( $instance[0] , $method ) ){
  627. continue;
  628. }
  629. $stack[$method] = $instance[0]->{$method}();
  630. }
  631. $instance[0] -> reset();
  632. foreach($stack as $method => $value){
  633. $instance[0]->{$method}($value);
  634. }
  635. }elseif( true !== $keep[0] ){
  636. $instance[0] -> reset();
  637. }
  638. return $instance[0];
  639. }
  640. $instance[0] = new Qdmail();
  641. return $instance[0];
  642. }
  643. //--------------------------
  644. // Decoration Mail Template
  645. //--------------------------
  646. function makeDecoTemplate( $deco_kind , $content ){
  647. if(false===($this->deco_kind=$this->decoSelect( $deco_kind ))){
  648. return $this->errorGather('Illegal Decoration Kind \''.$deco_kind.'\'',__LINE__);
  649. }
  650. $DECO = new QdDeco;
  651. $DECO -> template($content);
  652. $DECO -> decode();
  653. $content = $DECO -> get('HTML');
  654. $attach = $DECO -> get('ATTACH');
  655. $this -> renderMode( true );
  656. $this -> to('dummy@example.com');
  657. $this -> from('dummy@example.com');
  658. $this -> subject('dummy_subject');
  659. $this->body = null;
  660. $this->after_id = null;
  661. $this->content_id_fix = true;
  662. $this->is_html = 'HTML';
  663. $count = 0;
  664. $content = $this->qd_convert_encoding($content,'utf-8',$this->qd_detect_encoding($content));
  665. $content=preg_replace('/\r?\n/','',$content);
  666. foreach($attach as $key => $att){
  667. if( empty( $attach[$key]['CONTENT-ID'] ) ){
  668. continue;
  669. }
  670. $aft = isset($this->deco_def[$this->deco_kind]['CID_AFTER']) ? $this->deco_def[$this->deco_kind]['CID_AFTER']:'';
  671. $prefix = isset($this->deco_def[$this->deco_kind]['CID_PREFIX']) ? $this->deco_def[$this->deco_kind]['CID_PREFIX']:'';
  672. $col_num = isset($this->deco_def[$this->deco_kind]['CID_NUM_COL']) ? $this->deco_def[$this->deco_kind]['CID_NUM_COL']:3;
  673. $ct = '00'.$count++;
  674. $start = (strlen($ct)-$col_num) < 0 ? 0:strlen($ct)-$col_num;
  675. $end = strlen($ct)-$start;
  676. $new_cid = $prefix
  677. . substr($ct,$start,$end)
  678. . $aft;
  679. $content=preg_replace('/<\s*IMG\s+SRC\s*=\s*"cid:'.$attach[$key]['CONTENT-ID'].'"/is','<IMG SRC="cid:'.$new_cid.'"',$content);
  680. $attach[$key]['CONTENT-ID'] = $new_cid;
  681. }
  682. $this->html( $content , null , null , 'utf-8' );
  683. if( 0 < count($attach) ){
  684. $this->attach( $attach );
  685. }
  686. $this->createMail(
  687. $this->deco_def[$this->deco_kind]['BOUNDARY'],
  688. true
  689. );
  690. $header = '';
  691. foreach($this->deco_def[$this->deco_kind]['TOP'] as $line){
  692. $header .= $line .$this->LFC;
  693. }
  694. $header .= 'MIME-Version: 1.0' . $this->LFC
  695. . 'Content-type: ' . key($this->structure[$this->deco_def[$this->deco_kind]['STRUCTURE']])
  696. . '; boundary="'.$this->deco_def[$this->deco_kind]['BOUNDARY'] . '"'
  697. . $this->LFC;
  698. return $header . $this->LFC . $this -> smtpDataBody() . $this->LFC ;
  699. }
  700. //-------------------
  701. // Easy Base
  702. //-------------------
  703. function easy( $type , $to , $subject , $content , $other_header = array() , $attach = null ){
  704. if(is_null($other_header)){
  705. $other_header=array();
  706. }
  707. $this->resetHeaderBody();
  708. $option_return = array();
  709. if( is_array($type) ){
  710. $type = array_change_key_case( $type , CASE_UPPER );
  711. if( isset( $type['SMTP'] ) ){
  712. $this->smtp( true );
  713. $this->smtpServer( $type['SMTP'] );
  714. }
  715. if(isset( $type['OPTION'] )){
  716. $type['OPTION'] = array_change_key_case( $type['OPTION'] , CASE_UPPER );
  717. foreach($type['OPTION'] as $method => $param ){
  718. if(method_exists($this,$method)){
  719. $option_return[$method] = $this->{$method}($param);
  720. }
  721. }
  722. }
  723. $type = isset( $type['TYPE'] ) ? $type['TYPE'] : 'TEXT' ;
  724. }
  725. if( (empty($to) && ( !empty($subject) || !empty($content) ))){
  726. return $this->errorGather('Parameter Specified Error',__LINE__);
  727. }elseif( empty($to) ){
  728. return $option_return;
  729. }
  730. if( 'TEXT' == strtoupper( $type ) || 'HTML' == strtoupper( $type ) ){
  731. $type=strtolower( $type );
  732. }else{
  733. $this->error[]='Illegal spcify \'type\' in '.$type.' .'.__LINE__;
  734. return $this->errorGather();
  735. }
  736. $to = is_string($to) ? array($to) : $to ;
  737. $other_header = is_string($other_header) ? array('From' => $other_header) : $other_header ;
  738. list($other_header_temp , $link ) = $this->keyUpper( $other_header );
  739. if(!isset($other_header_temp['FROM'])){
  740. $fromAddr = null;
  741. if( isset($other_header[0]) ){
  742. $fromAddr = $other_header[0];
  743. unset($other_header[0]);
  744. }
  745. $fromName = null;
  746. if(isset($other_header[1])){
  747. $fromName = $other_header[1];
  748. unset($other_header[1]);
  749. }
  750. if(!empty($fromAddr)){
  751. $other_header = array_merge( $other_header,array('FROM'=>array( $fromAddr , $fromName )));
  752. }
  753. }
  754. $other_header = array_merge(array('TO'=>$to),$other_header);
  755. $section = array('TO'=>'to','CC'=>'cc','BCC'=>'bcc','REPLY-TO'=>'replyto','FROM'=>'from');
  756. list($other_header_temp , $link ) = $this->keyUpper( $other_header );
  757. foreach($other_header_temp as $key => $other_head){
  758. if(isset($section[$key])){
  759. $method = $section[$key];
  760. $this -> {$method}( $other_head , null );
  761. }else{
  762. $this -> addHeader( $link[$key] , $other_head );
  763. }
  764. }
  765. $this->subject( $subject );
  766. $this->{$type}( $content );
  767. if( isset( $attach ) ){
  768. $this->attach( $attach , $add = false , $this->inline_mode );
  769. }
  770. return $this->send();
  771. }
  772. function easyText( $to , $subject , $content , $other_header = array() , $attach = null , $option = array() ){
  773. return $this->easy( array('TYPE'=>'text','OPTION'=>$option) , $to , $subject , $content , $other_header , $attach );
  774. }
  775. function easyHtml( $to , $subject , $content , $other_header = array() , $attach = null , $option = array() ){
  776. return $this->easy( array('TYPE'=>'html','OPTION'=>$option) , $to , $subject , $content , $other_header , $attach );
  777. }
  778. function easyReplace( $to , $subject , $content , $other_header = array() , $attach = null , $option = array() ){
  779. $this->simpleReplace( true );
  780. $type = 'text';
  781. if(0!==count($option)){
  782. $option = array_change_key_case( $option , CASE_UPPER );
  783. $type = ( 'HTML' === strtoupper( $option['TYPE']) ) ? 'html' : $type ;
  784. }
  785. $this->easy( array('TYPE'=>$type,'OPTION'=>$option) , $to , $subject , $content , $other_header , $attach );
  786. }
  787. function easyDeco( $to , $subject , $content , $other_header = array() , $attach = null , $option = array() ){
  788. if( isset( $attach ) ){
  789. $this->inline_mode=true;
  790. }
  791. $this->autoDecoJudge( true );
  792. $this->toSeparate( true );
  793. return $this->easy( array('TYPE'=>'html','OPTION'=>$option) , $to , $subject , $content , $other_header , $attach );
  794. }
  795. function easyDecoTemplate( $to , $subject , $template , $other_header = array() , $attach = null , $option = array() ){
  796. if(is_null($attach)){
  797. $attach = array();
  798. }
  799. $DECO = new QdDeco;
  800. $DECO -> template($template);
  801. $DECO -> decode();
  802. $content = $DECO -> get('HTML');
  803. $text = $DECO -> get('PLAIN');
  804. if(!empty($text)){
  805. $this->text($text);
  806. }
  807. $att = $DECO ->get('ATTACH');
  808. $attach = array_merge($att,$attach);
  809. return $this->easyDeco( $to , $subject , $content , $other_header , $attach , $option );
  810. }
  811. function easyDecoRep( $to , $subject , $content , $other_header = array() , $attach = null , $option = array() ){
  812. $this->simpleReplace( true );
  813. return $this->easyDeco($to , $subject , $content , $attach , $option , $option);
  814. }
  815. function easyOption( $to , $subject = null , $content = null , $other_header = array() , $attach = null , $option = array() ){
  816. if(!is_array($to)){
  817. $option = array( $to => $subject );
  818. }else{
  819. $option = $to;
  820. }
  821. return $this->easy( array('TYPE'=>'option','OPTION'=>$option) , $to , $subject , $content , $other_header , $attach );
  822. }
  823. /*
  824. * Notice: Before use $this->optionNameLink(); by Constractor
  825. */
  826. //
  827. //---------------------------------------
  828. // something change mode
  829. //---------------------------------------
  830. // Keys must lowercase , because of PHP4's
  831. var $property_type = array(
  832. 'auto_both' => 'bool' ,
  833. 'to_separate' => 'bool' ,
  834. 'simple_replace' => 'bool' ,
  835. 'auto_deco_judge' => 'bool' ,
  836. 'auto_ext' => 'bool' ,
  837. 'body_empty_allow' => 'bool' ,
  838. 'ignore_error' => 'bool' ,
  839. 'wrap_width' => 'bool' ,
  840. 'wordwrap_allow' => 'bool' ,
  841. 'wrap_prohibit_allow'=> 'bool' ,
  842. 'force_change_charset' => 'bool' ,
  843. 'error_display' => 'bool' ,
  844. 'sendmail' => 'bool' ,
  845. 'smtp' => 'bool' ,
  846. 'smtp_loglevel_link'=> 'bool' ,
  847. 'inline_mode' => 'bool' ,
  848. 'replace_with_to_priority'=> 'bool' ,
  849. 'attach_build_once' => 'bool' ,
  850. 'body_build_once' => 'bool' ,
  851. 'kana' => 'bool' ,
  852. 'render_mode' => 'bool' ,
  853. 'smime' => 'bool' ,
  854. 'pgp' => 'bool' ,
  855. 'simple_attach' => 'bool' ,
  856. 'message_id' => 'bool' ,
  857. 'allow_blank_header'=> 'bool' ,
  858. 'sign' => 'string' ,
  859. 'keep_parameter' => 'array' ,
  860. 'attach_path' => 'string' ,
  861. 'mta_option' => 'string' ,
  862. 'rep_prefix' => 'string' ,
  863. 'log_path' => 'string' ,
  864. 'errorlog_path' => 'string' ,
  865. 'log_filename' => 'string' ,
  866. 'errorlog_filename' => 'string' ,
  867. 'allways_bcc' => 'string' ,
  868. 'wrap_prohibit_top' => 'string' ,
  869. 'wrap_prohibit_end' => 'string' ,
  870. 'framework' => 'string' ,
  871. 'priority' => 'string' ,
  872. 'certificate_file' => 'string' ,
  873. 'certificate_file_path' => 'string' ,
  874. 'certificate_temp_path' => 'string' ,
  875. 'time_zone' => 'string' ,
  876. 'private_key_file' => 'string' ,
  877. 'certificate_pass' => 'string' ,
  878. 'message_id_right' => 'string' ,
  879. 'sendmail_path' => 'string' ,
  880. 'temporary_path' => 'string' ,
  881. 'united_charset' => 'string' ,
  882. 'varidate_address_regex'=> 'string' ,
  883. 'mb_strwidth_magni' => 'numeric' ,
  884. 'log_dateformat' => 'numeric' ,
  885. 'log_level' => 'numeric' ,
  886. 'errorlog_level' => 'numeric' ,
  887. 'mime_encode_max' => 'numeric' ,
  888. 'smtp_server' => 'array' ,
  889. 'address_validation_method'=> 'array',
  890. );
  891. var $method_property = array();
  892. function optionNameLink(){
  893. foreach($this->property_type as $prop => $type ){
  894. $method_low = strtolower( str_replace( '_' , '' , $prop ) );
  895. $this->method_property[$method_low] = $prop;
  896. }
  897. }
  898. function option( $option , $line = null , $min = null , $max = null ){
  899. $ret = array();
  900. if( !is_null( $line ) ){
  901. $line = '-' . $line ;
  902. }
  903. if(!is_array($option)){
  904. return $this->errorSpecify( __FUNCTION__, __LINE__ );
  905. }
  906. foreach( $option as $key => $value ){
  907. if( !isset( $this->method_property[strtolower($key)] ) ){
  908. return $this->errorSpecify( __FUNCTION__ . '-' .$key , __LINE__ . $line );
  909. }
  910. $property_name = $this->method_property[strtolower($key)];
  911. if( is_null( $value ) ){
  912. $ret[] = $this->{$property_name} ;
  913. continue ;
  914. }
  915. $err = false;
  916. switch( $this->property_type[$property_name] ){
  917. case 'bool':
  918. if( is_bool( $value ) ){
  919. $this->{$property_name} = $value ;
  920. $ret[0] = true ;
  921. }else{
  922. return $this->errorSpecify( __FUNCTION__ . '-' .$key , __LINE__ . $line );
  923. }
  924. break;
  925. case 'string':
  926. if( '' === $value ){
  927. $this->{$property_name} = null ;
  928. $ret[0] = true ;
  929. break ;
  930. }
  931. if( is_string( $value ) ){
  932. $this->{$property_name} = $value ;
  933. $ret[0] = true ;
  934. }else{
  935. return $this->errorSpecify( __FUNCTION__ . '-' .$key , __LINE__ . $line );
  936. }
  937. break;
  938. case 'numeric':
  939. if( !is_numeric( $value ) || ( isset( $min ) && ( $value < $min ) ) || ( isset( $max ) && ( $value > $max ) ) ){
  940. return $this->errorSpecify( __FUNCTION__ . '-' .$key , __LINE__ . $line );
  941. }else{
  942. $this->{$property_name} = $value ;
  943. $ret[0] = true ;
  944. }
  945. break;
  946. case 'array':
  947. if( !is_array( $value ) ){
  948. $value = array( $value );
  949. }
  950. if( true===$min ){
  951. $this->{$property_name} = array_merge( $this->{$property_name} , $value );
  952. }else{
  953. $this->{$property_name} = $value ;
  954. }
  955. $ret[0] = true ;
  956. if( true === $max ){
  957. $this->{$property_name} = array_change_key_case( $this->{$property_name} , CASE_UPPER );
  958. }
  959. break;
  960. default:
  961. return $this->errorSpecify( __FUNCTION__ . '-' .$key , __LINE__ . $line );
  962. break;
  963. }
  964. }
  965. if( 0 === count( $ret ) ){
  966. return $this->errorSpecify( __FUNCTION__ , __LINE__ );
  967. }elseif( 1 === count( $ret ) ){
  968. return array_shift( $ret );
  969. }else{
  970. return $ret ;
  971. }
  972. }
  973. function autoBoth( $bool = null ){
  974. return $this->option( array( __FUNCTION__ => $bool ) ,__LINE__);
  975. }
  976. function toSeparate( $bool = null ){
  977. return $this->option( array( __FUNCTION__ => $bool ) ,__LINE__);
  978. }
  979. function simpleReplace( $bool = null ){
  980. $this->toSeparate( $bool );
  981. return $this->option( array( __FUNCTION__ => $bool ) ,__LINE__);
  982. }
  983. function autoDecoJudge( $bool = null ){
  984. $this->attachBuildOnce( !$bool );
  985. return $this->option( array( __FUNCTION__ => $bool ) ,__LINE__);
  986. }
  987. function autoExt( $bool = null ){
  988. return $this->option( array( __FUNCTION__ => $bool ) ,__LINE__);
  989. }
  990. function bodyEmptyAllow( $bool = null ){
  991. return $this->option( array( __FUNCTION__ => $bool ) ,__LINE__);
  992. }
  993. function ignoreError( $bool = null ){
  994. return $this->option( array( __FUNCTION__ => $bool ) ,__LINE__);
  995. }
  996. function forceChangeCharset( $bool = null ){
  997. return $this->option( array( __FUNCTION__ => $bool ) ,__LINE__);
  998. }
  999. function errorDisplay( $bool = null ){
  1000. return $this->option( array( __FUNCTION__ => $bool ) ,__LINE__);
  1001. }
  1002. function sendmail( $bool = null ){
  1003. return $this->option( array( __FUNCTION__ => $bool ) ,__LINE__);
  1004. }
  1005. function smtp( $bool = null ){
  1006. return $this->option( array( __FUNCTION__ => $bool ) ,__LINE__);
  1007. }
  1008. function smtpLoglevelLink( $bool = null ){
  1009. return $this->option( array( __FUNCTION__ => $bool ) ,__LINE__);
  1010. }
  1011. function inlineMode( $bool = null ){
  1012. return $this->option( array( __FUNCTION__ => $bool ) ,__LINE__);
  1013. }
  1014. function replaceWithToPriority( $bool = null ){
  1015. return $this->option( array( __FUNCTION__ => $bool ) ,__LINE__);
  1016. }
  1017. function attachBuildOnce( $bool = null ){
  1018. return $this->option( array( __FUNCTION__ => $bool ) ,__LINE__);
  1019. }
  1020. function bodyBuildOnce( $bool = null ){
  1021. return $this->option( array( __FUNCTION__ => $bool ) ,__LINE__);
  1022. }
  1023. function kana( $bool = null ){
  1024. return $this->option( array( __FUNCTION__ => $bool ) ,__LINE__);
  1025. }
  1026. function keepParameter( $bool = null ){
  1027. return $this->option( array( __FUNCTION__ => $bool ) ,__LINE__);
  1028. }
  1029. function renderMode( $bool = null ){
  1030. return $this->option( array( __FUNCTION__ => $bool ) ,__LINE__);
  1031. }
  1032. function simpleAttach( $bool = null ){
  1033. return $this->option( array( __FUNCTION__ => $bool ) ,__LINE__);
  1034. }
  1035. function messageId( $bool = null ){
  1036. return $this->option( array( __FUNCTION__ => $bool ) ,__LINE__);
  1037. }
  1038. function allowBlankHeader( $bool = null ){
  1039. return $this->option( array( __FUNCTION__ => $bool ) ,__LINE__);
  1040. }
  1041. function smime( $bool = null ){
  1042. $fg = $this->option( array( __FUNCTION__ => $bool ) ,__LINE__);
  1043. if(!extension_loaded ( 'openssl' )){
  1044. $this->smime = false;
  1045. if(!$bool){
  1046. return false;
  1047. }
  1048. }
  1049. return $fg;
  1050. }
  1051. function pgp( $bool = null ){
  1052. // future
  1053. return true;
  1054. }
  1055. function sign( $string = null ){
  1056. if(false===$string){
  1057. $this->smime = false;
  1058. $this->pgp = false;
  1059. $this->sign = false;
  1060. return true;
  1061. }
  1062. if(empty($string)){
  1063. return $this->sign;
  1064. }
  1065. $string = strtoupper($string);
  1066. if('S/MIME'==$string){
  1067. $this->smime(true);
  1068. }elseif('PGP'==$string){
  1069. $this->pgp(true);
  1070. }else{
  1071. return false;
  1072. }
  1073. $fg = $this->option( array( __FUNCTION__ => $string ) ,__LINE__);
  1074. return $fg;
  1075. }
  1076. function size( $kind = null ){
  1077. if(empty($this->header_for_smtp)){
  1078. $stack = $this->render_mode;
  1079. $this->render_mode = true;
  1080. $fg = $this->send();
  1081. $this->render_mode = $stack;
  1082. }
  1083. $this->size['ALL'] = strlen( bin2hex( $this->header_for_smtp . $this->LFC . $this->content_for_mailfunction )) / 2;
  1084. $this->size['HEADER'] = strlen( bin2hex( $this->header_for_smtp )) / 2;
  1085. $this->size['BODY'] = strlen( bin2hex( $this->content_for_mailfunction )) / 2;
  1086. if(is_null($kind)){
  1087. return $this->size;
  1088. }
  1089. $kind = strtoupper( $kind );
  1090. if(isset($this->size[$kind])){
  1091. return $this->size[$kind];
  1092. }
  1093. return false;
  1094. }
  1095. function sizeAll(){
  1096. return $this->size('ALL');
  1097. }
  1098. function sizeHeader(){
  1099. return $this->size('HEADER');
  1100. }
  1101. function sizeBody(){
  1102. return $this->size('BODY');
  1103. }
  1104. function smtpData(){
  1105. if(empty($this->header_for_smtp)){
  1106. $stack = $this->render_mode;
  1107. $this->render_mode = true;
  1108. $fg = $this->send();
  1109. $this->render_mode = $stack;
  1110. }
  1111. return $this->header_for_smtp . $this->LFC . $this->content_for_mailfunction ;
  1112. }
  1113. function smtpDataBody(){
  1114. if(empty($this->content_for_mailfunction)){
  1115. $stack = $this->render_mode;
  1116. $this->render_mode = true;
  1117. $fg = $this->send();
  1118. $this->render_mode = $stack;
  1119. }
  1120. return $this->content_for_mailfunction ;
  1121. }
  1122. function isQmail(){
  1123. if(!is_null($this->is_qmail)){
  1124. return $this->is_qmail;
  1125. }
  1126. $this->is_qmail = false;
  1127. $ret = ini_get ( 'sendmail_path' );
  1128. if(false !== strpos($ret,'qmail')){
  1129. $this->is_qmail = true;
  1130. }
  1131. $sendmail_path = ini_get('sendmail_path');
  1132. if(false !== @system($sendmail_path.' -d0.1 < /dev/null > /dev/null',$ret)){
  1133. if(is_array($ret)){
  1134. $ret = reset($ret);
  1135. }
  1136. $code = (int) substr($ret,0,3);
  1137. if( 100 === $code || 111 === $code){
  1138. $this->is_qmail = true;
  1139. }
  1140. }
  1141. return $this->is_qmail ;
  1142. }
  1143. function lineFeed( $LFC = null ){
  1144. if(is_null($LFC)){
  1145. return $this->LFC;
  1146. }
  1147. if(preg_match('/[\r|\n|\r\n]/is',$LFC)){
  1148. $this->LFC = $LFC;
  1149. return true;
  1150. }else{
  1151. return false;
  1152. }
  1153. }
  1154. function isWin(){
  1155. return false!==strpos(PHP_OS,'WIN');
  1156. }
  1157. //---------------------------------------
  1158. // something change mode
  1159. //---------------------------------------
  1160. function whichTextHtml( $which ){
  1161. $which = strtoupper( $which );
  1162. if( 'TEXT' == $which ){
  1163. $this->is_html='TEXT';
  1164. }elseif( 'HTML' == $which ){
  1165. $this->is_html='HTML';
  1166. }elseif( 'BOTH' == $which ){
  1167. $this->is_html='BOTH';
  1168. }
  1169. }
  1170. function allwaysBcc( $option = null ){
  1171. if( is_null( $option ) ){
  1172. return $this->allways_bcc ;
  1173. }
  1174. if( $this->option( array( __FUNCTION__ => $option ) ,__LINE__) ){
  1175. $fg = $this->extractAddr( $this->allways_bcc ) ;
  1176. }
  1177. if( $this->errorGather() && $fg && !empty($this->allways_bcc) ){
  1178. return true ;
  1179. }else{
  1180. $this->allways_bcc = array();
  1181. return false ;
  1182. }
  1183. }
  1184. function priority( $option = null ){
  1185. $fg=$this->option( array( __FUNCTION__ => $option ) ,__LINE__);
  1186. $priority = strtoupper($option);
  1187. if(empty($priority)){
  1188. return $fg;
  1189. }
  1190. $kind = array('HIGH'=>1,'NORMAL'=>1,'LOW'=>1);
  1191. if( !isset( $kind[$priority] ) ){
  1192. return $this->errorGather('Illegal Priority Name \''.$option.'\'',__LINE__);
  1193. }
  1194. foreach($this->priority_def as $header_name => $values){
  1195. $this->addHeader($header_name,$values[$priority]);
  1196. }
  1197. return $this->errorGather();
  1198. }
  1199. function certificatePass( $option = null ){
  1200. return $this->option( array( __FUNCTION__ => $option ) ,__LINE__);
  1201. }
  1202. function certificateFilePath( $option = null ){
  1203. return $this->option( array( __FUNCTION__ => $option ) ,__LINE__);
  1204. }
  1205. function certificateTempPath( $option = null ){
  1206. return $this->option( array( __FUNCTION__ => $option ) ,__LINE__);
  1207. }
  1208. function certificateFile( $option = null ){
  1209. return $this->option( array( __FUNCTION__ => $option ) ,__LINE__);
  1210. }
  1211. function privateKeyFile( $option = null ){
  1212. return $this->option( array( __FUNCTION__ => $option ) ,__LINE__);
  1213. }
  1214. function framework( $option = null ){
  1215. return $this->option( array( __FUNCTION__ => $option ) ,__LINE__);
  1216. }
  1217. function attachPath( $option = null ){
  1218. return $this->option( array( __FUNCTION__ => $option ) ,__LINE__);
  1219. }
  1220. function timeZone( $option = null ){
  1221. return $this->option( array( __FUNCTION__ => $option ) ,__LINE__);
  1222. }
  1223. function messageIdRight( $option = null ){
  1224. return $this->option( array( __FUNCTION__ => $option ) ,__LINE__);
  1225. }
  1226. function mtaOption( $option = null ){
  1227. return $this->option( array( __FUNCTION__ => $option ) ,__LINE__);
  1228. }
  1229. function unitedCharset( $option = null ){
  1230. return $this->option( array( __FUNCTION__ => $option ) ,__LINE__);
  1231. }
  1232. function varidateAddressRegex( $option = null ){
  1233. return $this->option( array( __FUNCTION__ => $option ) ,__LINE__);
  1234. }
  1235. function sendmailPath( $option = null ){
  1236. return $this->option( array( __FUNCTION__ => $option ) ,__LINE__);
  1237. }
  1238. function temporaryPath( $option = null ){
  1239. return $this->option( array( __FUNCTION__ => $option ) ,__LINE__);
  1240. }
  1241. function logPath( $option = null ){
  1242. return $this->option( array( __FUNCTION__ => $option ) ,__LINE__);
  1243. }
  1244. function errorlogPath( $option = null ){
  1245. return $this->option( array( __FUNCTION__ => $option ) ,__LINE__);
  1246. }
  1247. function logDateFormat( $option = null ){
  1248. return $this->option( array( __FUNCTION__ => $option ) ,__LINE__);
  1249. }
  1250. function logFilename( $option = null ){
  1251. return $this->option( array( __FUNCTION__ => $option ) ,__LINE__);
  1252. }
  1253. function errorlogFilename( $option = null ){
  1254. return $this->option( array( __FUNCTION__ => $option ) ,__LINE__);
  1255. }
  1256. function logLevel( $option = null ){
  1257. return $this->option( array( __FUNCTION__ => $option ) ,__LINE__, 0 , $this->log_level_max );
  1258. }
  1259. function errorlogLevel( $option = null ){
  1260. return $this->option( array( __FUNCTION__ => $option ) ,__LINE__, 0 , $this->errorlog_level_max );
  1261. }
  1262. function mimeEncodeMax( $option = null ){
  1263. return $this->option( array( __FUNCTION__ => $option ) ,__LINE__);
  1264. }
  1265. function smtpServer( $array = null ){
  1266. return $this->option( array( __FUNCTION__ => $array ) ,__LINE__, true , true );
  1267. }
  1268. function addressValidationMethod( $array = null ){
  1269. return $this->option( array( __FUNCTION__ => $array ) ,__LINE__, false , true );
  1270. }
  1271. //------------------
  1272. //version
  1273. //------------------
  1274. function version(){
  1275. return $this->version;
  1276. }
  1277. //------------------
  1278. //mb_ wrapper
  1279. //------------------
  1280. function qd_detect_encoding( $word ){
  1281. if(!is_null($this->united_charset)){
  1282. return $this->united_charset;
  1283. }else{
  1284. return mb_detect_encoding( $word , mb_detect_order() , true );
  1285. }
  1286. }
  1287. function qd_convert_encoding( $word , $target_chrset , $org_charset = null ){
  1288. if(empty($org_charset)){
  1289. $org_charset = $this->qd_detect_encoding( $word );
  1290. }
  1291. if(empty($org_charset)){
  1292. return $word;
  1293. }
  1294. if( strtoupper( $target_chrset ) === strtoupper( $org_charset ) ){
  1295. return $word;
  1296. }
  1297. if('ASCII'===strtoupper( $target_chrset ) || 'ASCII'===strtoupper( $org_charset )){
  1298. return $word;
  1299. }
  1300. return mb_convert_encoding( $word , $target_chrset , $org_charset );
  1301. }
  1302. function qd_detect_order( $param=null ){
  1303. if(is_null($param)){
  1304. return mb_detect_order();
  1305. }else{
  1306. return mb_detect_order( $param );
  1307. }
  1308. }
  1309. //-----------------------------------------
  1310. // Address and Name Keys change Opiton
  1311. //-----------------------------------------
  1312. function addressField( $addr = null , $name = null ){
  1313. if( is_null($addr) && is_null($name) ){
  1314. return array( $this->tokey['_ADDR'] , $this->tokey['_NAME'] );
  1315. }
  1316. if( !is_null($addr) && is_array($addr) && 1 < count($addr) ){
  1317. $_addr = array_shift( $addr ) ;
  1318. $name = array_shift( $addr ) ;
  1319. $addr = $_addr;
  1320. }
  1321. if( (!is_null($addr) && !is_string( $addr )) || !is_null($name) && !is_string($name) ){
  1322. return $this->errorGather('Specify Error in addressField',__LINE__);
  1323. }
  1324. $addr = is_null( $addr ) ? $this->tokey['_ADDR'] : $addr ;
  1325. $name = is_null( $name ) ? $this->tokey['_NAME'] : $name ;
  1326. $this->tokey = array(
  1327. '_ADDR' => $addr,
  1328. '_NAME' => $name,
  1329. );
  1330. return true;
  1331. }
  1332. //-----------------------------------------------------------
  1333. // Wordwrap Opiton
  1334. // array( 'except word' => beginning flag )
  1335. // if beginning flag is true , beginning of a line is target
  1336. // if beginning flag is true , the word in line is target
  1337. //-----------------------------------------------------------
  1338. function wordwrapProhibitConstruct(){
  1339. $ret = $this->strToArrayKey( $this->wrap_prohibit_top , true );
  1340. $ret2 = $this->strToArrayKey( $this->wrap_prohibit_end , false );
  1341. $this->wrap_prohibit = array_merge( $ret , $ret2 );
  1342. }
  1343. function strToArrayKey( $word , $value ){
  1344. $ret = array();
  1345. $enc = $this->qd_detect_encoding( $word );
  1346. $length = mb_strlen( $word , $enc );
  1347. for( $i=0 ; $i < $length ; $i++ ){
  1348. $ret[ mb_substr( $word , $i , 1 , $enc ) ] = $value;
  1349. }
  1350. return $ret;
  1351. }
  1352. function wordwrapAllow( $bool = null ){
  1353. return $this->option( array( __FUNCTION__ => $bool ) ,__LINE__);
  1354. }
  1355. function wrapProhibitAllow( $bool = null ){
  1356. return $this->option( array( __FUNCTION__ => $bool ) ,__LINE__);
  1357. }
  1358. function wrapProhibitEnd( $option = null ){
  1359. $this->option( array( __FUNCTION__ => $option ) ,__LINE__);
  1360. $this->wordwrapProhibitConstruct();
  1361. return $this->errorGather();
  1362. }
  1363. function wrapProhibitTop( $option = null ){
  1364. $this->option( array( __FUNCTION__ => $option ) ,__LINE__);
  1365. $this->wordwrapProhibitConstruct();
  1366. return $this->errorGather();
  1367. }
  1368. function wrapWidth( $bool = null ){
  1369. return $this->option( array( __FUNCTION__ => $bool ) ,__LINE__);
  1370. }
  1371. function wordwrapLength( $length = null ){
  1372. if( is_null( $length ) ){
  1373. return $this->wordwrap_length;
  1374. }
  1375. if( !is_numeric( $length ) || ( is_numeric( $length ) && $length < 1 ) ){
  1376. return $this->errorGather('Wordwrap error , length is illegal' ,__LINE__) ;
  1377. }
  1378. $this->wordwrap_length = $length;
  1379. return $this->errorGather() ;
  1380. }
  1381. function mbStrwidthMagni( $option = null ){
  1382. return $this->option( array( __FUNCTION__ => $option ) ,__LINE__, 0 , 10 );
  1383. }
  1384. function wrapExcept( $array = null ){
  1385. if( null === $array ){
  1386. return $this->wrap_except;
  1387. }
  1388. if( is_string( $array ) || is_numeric( $array ) ){
  1389. $this->wrap_except = array( $array =>false );//default false
  1390. }
  1391. if( is_array( $array ) ){
  1392. if( 0 === count( $array ) ){
  1393. $this->wrap_except = array();
  1394. return $this->errorGather() ;
  1395. }
  1396. foreach( $array as $key => $value){
  1397. if( !is_bool( $value ) ){
  1398. $array[$key] = empty( $value ) ? false:true;
  1399. }else{
  1400. $array[$key] = $value ;
  1401. }
  1402. }
  1403. $this->wrap_except = $array;
  1404. return $this->errorGather() ;
  1405. }
  1406. return $this->errorSpecify(__FUNCTION__,__LINE__);
  1407. }
  1408. //------------------------------------------
  1409. // Charset Option
  1410. //------------------------------------------
  1411. function charsetHeader( $charset = null ){
  1412. if(is_null($charset)){
  1413. return $this->charset_header ;
  1414. }
  1415. $stack = $this->charset();
  1416. $stack['HEADER'] = $charset;
  1417. return $this->charset( $stack );
  1418. }
  1419. function charsetAttach( $charset = null ){
  1420. if(is_null($charset)){
  1421. return $this->charset_attach_filename ;
  1422. }
  1423. $stack = $this->charset();
  1424. $stack['HEADER'] = $charset;
  1425. return $this->charset( $stack );
  1426. }
  1427. function charsetBody( $charset = null , $enc = null ){
  1428. if( is_null($charset) && is_null($enc) ){
  1429. return array($this->charset_content , $this->content_transfer_enc_text , content_transfer_enc_html) ;
  1430. }
  1431. if(is_array($charset)){
  1432. $enc = isset($charset[1]) ? $charset[1]:null;
  1433. $charset = $charset[0];
  1434. }
  1435. $stack = $this->charset();
  1436. if( !is_null($charset) ){
  1437. $stack['BODY'] = $charset;
  1438. }
  1439. if( !is_null($enc) ){
  1440. $stack['HTML'] = $stack['TEXT'] = $enc;
  1441. }
  1442. return $this->charset( $stack );
  1443. }
  1444. function charset( $array = null , $op = null ){
  1445. if( is_null( $array ) && is_null( $op ) ){
  1446. foreach( $this->corres_charset as $key => $value ){
  1447. $ret[$key] = $this->{$value} ;
  1448. }
  1449. return $ret;
  1450. }
  1451. if( !is_null($op) && is_string($op) ){
  1452. $this->content_transfer_enc_text = $op ;
  1453. $this->content_transfer_enc_html = $op ;
  1454. return $this->charset( $array );
  1455. }elseif(!is_null($op) && !is_string($op)){
  1456. return $this->errorSpecify( __FUNCTION__ , __LINE__ );
  1457. }
  1458. if( is_array( $array ) ){
  1459. if( 2===count($array) && isset($array[0]) && isset($array[1])){
  1460. return $this->charset($array[0],$array[1]);
  1461. }
  1462. $array = array_change_key_case( $array , CASE_UPPER );
  1463. foreach( $array as $key => $value ){
  1464. if(isset($this->corres_charset[$key])){
  1465. if( is_string($this->corres_charset[$key]) ){
  1466. $this->{$this->corres_charset[$key]} = $value;
  1467. }else{
  1468. return $this->errorSpecify( __FUNCTION__ , __LINE__ );
  1469. }
  1470. }
  1471. }
  1472. }elseif( is_string($array) ){
  1473. $this->charset_header = $this->charset_content = $this->charset_attach_filename = $array;
  1474. }else{
  1475. return $this->errorSpecify( __FUNCTION__ , __LINE__ );
  1476. }
  1477. return $this->errorGather() ;
  1478. }
  1479. function encoding( $enc = null ){
  1480. if(is_null($enc)){
  1481. return $this->corres_charset['TEXT'];
  1482. }
  1483. $this->corres_charset['TEXT'] = $this->corres_charset['HTML'] = $enc;
  1484. return true;
  1485. }
  1486. //--------------------------
  1487. // set Mutibye Parameter
  1488. //--------------------------
  1489. function setMbParameter( $lang = null , $internal_enc = null , $detect = null ){
  1490. if(is_array($lang)){
  1491. mb_language( $lang[0] ) ;
  1492. mb_internal_encoding( $lang[1] ) ;
  1493. $this->qd_detect_order( $lang[2] );
  1494. }elseif( 'STACK'===strtoupper($lang) ){
  1495. $this->mb_parameter_stack = array(mb_language(),mb_internal_encoding(),$this->qd_detect_order());
  1496. mb_language( $this->lang_def );
  1497. mb_internal_encoding( $this->encoding_def );
  1498. $this->qd_detect_order( $this->detect_def );
  1499. }else{
  1500. if( !is_null( $lang ) ){
  1501. mb_language( $lang ) ;
  1502. }
  1503. if( !is_null( $internal_enc ) ){
  1504. mb_internal_encoding( $internal_enc ) ;
  1505. }
  1506. if( !is_null( $detect ) ){
  1507. $this->qd_detect_order( $detect );
  1508. }
  1509. }
  1510. }
  1511. //--------------------------------
  1512. // Decorationable HTML Mail Opiton
  1513. // ( Inline HTML , MHTML )
  1514. // See $this->deco_def Property
  1515. //--------------------------------
  1516. // Change decoration default along to each career
  1517. function decoDef( $value = null ){
  1518. if( is_null( $value ) ){
  1519. return $this->deco_def_default;
  1520. }
  1521. $this->deco_def_default = $value ;
  1522. return $this->errorGather() ;
  1523. }
  1524. // fix Decoration Pattern by STRING means CareerName
  1525. function decoFix( $cari = null ){
  1526. if(is_null($cari)){
  1527. return $this->deco_kind;
  1528. }
  1529. $this->deco_kind = $this->decoSelect( $cari );
  1530. return true;
  1531. }
  1532. function decoSelect( $deco_kind = null ){
  1533. if( is_null( $deco_kind ) ){
  1534. return $this->deco_def_default;//$this->deco_judge
  1535. }
  1536. $deco_kind = strtoupper( $deco_kind );
  1537. $ret = false ;
  1538. foreach( $this->deco_def as $key => $def ){
  1539. if( in_array( $deco_kind , $def['OPTION_NAME'] ) ){
  1540. $ret = $key ;
  1541. $this->inline_mode = true;
  1542. }
  1543. }
  1544. return $ret;
  1545. }
  1546. // Change Decoration Pattern by E-mail Address
  1547. function decoJudge( $addr_array ){
  1548. $addr=$addr_array[$this->tokey['_ADDR']];
  1549. $start = strrpos( $addr , '@');
  1550. if(empty($start)){
  1551. return $this->deco_def_default;
  1552. }
  1553. $right = trim(substr($addr , $start+1));
  1554. $parts = explode('.',$right);
  1555. $ct = count($parts);
  1556. if( 2 > $ct ){
  1557. return $this->deco_def_default;
  1558. }
  1559. $domains = array();
  1560. $domains[] = $parts[$ct-2] . '.' . $parts[$ct-1];
  1561. if( isset($parts[$ct-3]) ){
  1562. $domains[] = $parts[$ct-3] .'.'.$parts[$ct-2] . '.' . $parts[$ct-1];
  1563. }
  1564. if( isset($parts[$ct-3]) && isset($parts[$ct-4]) ){
  1565. $domains[] = $parts[$ct-4] .'.'. $parts[$ct-3] .'.'.$parts[$ct-2] . '.' . $parts[$ct-1];
  1566. }
  1567. $ct = count($domains);
  1568. $domain = reset($domains);
  1569. while( $ct-- > 0){
  1570. if(isset( $this->deco_judge[$domains[$ct]])){
  1571. $domain = $domains[$ct];
  1572. break;
  1573. }
  1574. }
  1575. return $this->decoSelect(isset( $this->deco_judge[$domain] ) ? $this->deco_judge[$domain]:null);
  1576. }
  1577. //------------------------------------
  1578. //
  1579. // Word Replace
  1580. //
  1581. // You can add prefix by $this->rep_prefix proparty
  1582. // OR $this->repPrefix() Method (Recommended)
  1583. // notice: this functino need on utf-8
  1584. // OR $this->qdmail_system_charset need utf-8
  1585. //------------------------------------
  1586. function replaceWord( $array = null , $add = false ){
  1587. if( is_null( $array ) ){
  1588. return $this->replace ;
  1589. }
  1590. if( !is_array( $array ) ){
  1591. $array = array( $array );
  1592. }
  1593. foreach($array as $key => $arr){
  1594. if( !is_array( $arr ) ){
  1595. $array[$key] = array( $arr );
  1596. }
  1597. }
  1598. if( $add ){
  1599. $this->replace = array_merge( $this->replace , $array );
  1600. }else{
  1601. $this->replace = $array ;
  1602. }
  1603. return $this->errorGather() ;
  1604. }
  1605. function replaceDef( $array = null ){
  1606. if(is_null($array)){
  1607. return $this->replace_def;
  1608. }
  1609. if(is_array($array)){
  1610. $this->replace_def = $array;
  1611. }else{
  1612. return false;
  1613. }
  1614. }
  1615. function repPrefix( $option = null ){
  1616. return $this->stringOption( __FUNCTION__ , $option , __LINE__ );
  1617. }
  1618. function replace( $cont , $rep ){
  1619. foreach($rep as $serch => $replace ){
  1620. if( '_' == mb_substr( $serch , 0 , 1 , $this->qd_detect_encoding($serch) ) ){
  1621. continue;
  1622. }
  1623. if( empty($replace) && !empty($this->replace_def[$serch]) ){
  1624. $replace = $this->replace_def[$serch];
  1625. }
  1626. $reg = '/%' . $this->rep_prefix . (string) $serch . '%/is' ;
  1627. $cont = $this->qdmail_preg_replace( $reg , $replace , $cont );
  1628. }
  1629. return $cont;
  1630. }
  1631. function qdmail_preg_replace( $reg , $rep , $cont ){
  1632. $enc = $this->qd_detect_encoding( $cont );
  1633. $_reg = $this->qd_convert_encoding( $reg , $this->qdmail_system_charset , $this->qd_detect_encoding( $reg ) );
  1634. $_rep = $this->qd_convert_encoding( $rep , $this->qdmail_system_charset , $this->qd_detect_encoding( $rep ) );
  1635. $_cont = $this->qd_convert_encoding( $cont , $this->qdmail_system_charset , $enc );
  1636. $cont = preg_replace( $_reg , $_rep , $_cont );
  1637. return $this->qd_convert_encoding($cont , $enc , $this->qdmail_system_charset );
  1638. }
  1639. //------------------------------------
  1640. // OOP User Interface (Recommended)
  1641. //------------------------------------
  1642. function to( $addr = null , $name = null , $add = false ){
  1643. return $this->addrs( 'TO' , $addr , $name , $add );
  1644. }
  1645. function cc( $addr = null , $name = null , $add = false ){
  1646. return $this->addrs( 'CC' , $addr , $name , $add );
  1647. }
  1648. function bcc( $addr = null , $name = null , $add = false ){
  1649. return $this->addrs( 'BCC' , $addr , $name , $add );
  1650. }
  1651. function from( $addr = null , $name = null ){
  1652. return $this->addrs( 'FROM' , $addr , $name , false );
  1653. }
  1654. function replyto( $addr = null , $name = null ){
  1655. return $this->addrs( 'REPLYTO' , $addr , $name , false );
  1656. }
  1657. function addHeader( $header_name = null , $value = null){
  1658. if('REPLY-TO'==strtoupper($header_name)){
  1659. $header_name = 'REPLYTO' ;
  1660. }
  1661. if(isset($this->addr_head_name[strtoupper($header_name)])){
  1662. return $this->{strtolower($header_name)}( $value , null , true );
  1663. } if(is_null($header_name)){
  1664. return $this->other_header;
  1665. }
  1666. if('clear'===strtolower($header_name) && is_null($value)){
  1667. $this->other_header=array();
  1668. return ;
  1669. }
  1670. $this->other_header[$header_name] = $value ;
  1671. }
  1672. function reset( $debugErase = false ){
  1673. if( !$debugErase ){
  1674. $stack_debug = $this->debug ;
  1675. }
  1676. $stack = $this->stack_construct;
  1677. $array = get_class_vars( $this->name ) ;
  1678. foreach($array as $key => $value){
  1679. $this->{$key} = $value ;
  1680. }
  1681. $this->__construct( $stack );
  1682. if( !$debugErase ){
  1683. $this->debug = $stack_debug ;
  1684. }
  1685. }
  1686. function resetHeader(){
  1687. $this->to = array();
  1688. $this->cc = array();
  1689. $this->bcc = array();
  1690. $this->from = array();
  1691. $this->replyto = array();
  1692. $this->other_header=array();
  1693. $this->subject = null;
  1694. }
  1695. function resetBody(){
  1696. $this->body('');
  1697. $this->is_html = null;
  1698. $this->deco_kind = null;
  1699. $this->inline_mode = false;
  1700. $this->attach = array();
  1701. }
  1702. function resetHeaderBody(){
  1703. $this->resetBody();
  1704. $this->resetHeader();
  1705. }
  1706. function _gatherFromArray( $array , $key ){
  1707. $ret = array();
  1708. foreach( $array as $ar ){
  1709. $ret[] = $ar[$key] ;
  1710. }
  1711. return $ret;
  1712. }
  1713. function done(){
  1714. return $this->_gatherFromArray( $this->done , $this->tokey['_ADDR'] );
  1715. }
  1716. function undone(){
  1717. return $this->_gatherFromArray( $this->undone , $this->tokey['_ADDR'] );
  1718. }
  1719. function subject( $subj = null ){
  1720. if( is_null($subj) ){
  1721. return $this->subject;
  1722. }
  1723. if( is_string( $subj ) || is_numeric( $subj ) ){
  1724. $this->subject['CONTENT'] = (string) $subj;
  1725. return $this->errorGather() ;
  1726. }elseif( is_array($subj) ){
  1727. $subj = array_change_key_case( $subj , CASE_UPPER );
  1728. if(isset($subj['CONTENT'])){
  1729. $this->subject = $subj;
  1730. }else{
  1731. $this->subject['CONTENT'] = (string) $subj[0];
  1732. $this->subject['_CHARSET'] = isset($subj[1]) ? $subj[1] : null ;
  1733. $this->subject['_ORG_CHARSET'] = isset($subj[2]) ? $subj[2] : null ;
  1734. }
  1735. return $this->errorGather() ;
  1736. }else{
  1737. return $this->errorSpecify(__FUNCTION__,__LINE__);
  1738. }
  1739. }
  1740. function body( $type =null , $cont = null , $length = null , $charset = null , $enc = null , $org_charset = null ){
  1741. if(is_null($type)){
  1742. return $this->content;
  1743. }
  1744. if(empty($type)){
  1745. $this->content = array(
  1746. 'TEXT'=>array(
  1747. 'CONTENT' => null,
  1748. 'LENGTH' => null,
  1749. '_CHARSET' => null,
  1750. 'ENC' => null,
  1751. '_ORG_CHARSET' => null,
  1752. ),
  1753. 'HTML'=>array(
  1754. 'CONTENT' => null,
  1755. 'ORG_CONTENT' => null,
  1756. 'LENGTH' => null,
  1757. '_CHARSET' => null,
  1758. 'ENC' => null,
  1759. '_ORG_CHARSET' => null,
  1760. ),
  1761. );
  1762. return true;
  1763. }
  1764. $type = strtolower( $type );
  1765. if( 'text'!==$type && 'html'!==$type ){
  1766. return $this->errorGather('You must use \'text\' or \'html\'' ,__LINE__) ;
  1767. }
  1768. if( is_array( $cont ) ){
  1769. $def = array(
  1770. 'CONTENT'=>null,
  1771. 'LENGTH'=>null,
  1772. '_CHARSET'=>null,
  1773. 'ENC'=>null,
  1774. '_ORG_CHARSET'=>null,
  1775. );
  1776. $temp = array_change_key_case( array_merge($def,$cont) , CASE_UPPER);
  1777. }else{
  1778. $temp = array(
  1779. 'CONTENT'=>$cont,
  1780. 'LENGTH'=>$length,
  1781. '_CHARSET'=>$charset,
  1782. 'ENC'=>$enc,
  1783. '_ORG_CHARSET'=>$org_charset,
  1784. );
  1785. }
  1786. $this->content[strtoupper($type)] = array_merge( $this->content[strtoupper($type)] , $temp );
  1787. return $this->errorGather() ;
  1788. }
  1789. function text( $cont , $length = null , $charset = null , $enc = null , $org_charset = null ){
  1790. return $this->body('text', $cont , $length , $charset , $enc , $org_charset );
  1791. }
  1792. function html( $cont , $charset = null , $enc = null , $org_charset = null ){
  1793. return $this->body('html', $cont , null , $charset , $enc , $org_charset );
  1794. }
  1795. //--------------------------
  1796. // assist User Interface
  1797. //--------------------------
  1798. function addrs( $section , $addr = null , $name = null , $add = false ){
  1799. $section = strtolower( $section );
  1800. $ck = array('to'=>true,'from'=>true,'cc'=>true,'bcc'=>true,'replyto'=>true);
  1801. if(empty($ck[$section])){
  1802. return $this->errorGather('Illegal Section Name \''.$section.'\'' ,__LINE__) ;
  1803. }
  1804. if( is_null( $addr ) && is_null( $name )){
  1805. return $this->{$section} ;
  1806. }
  1807. if( false === $addr ){
  1808. $this->{$section} = array();
  1809. return $this->errorGather() ;
  1810. }
  1811. $addr = $this->analyzeAddr( $addr , $name );
  1812. if( !$this->allow_blank_header && empty($addr[0][$this->tokey['_ADDR']]) ){// if addres is empty , no set
  1813. return true;
  1814. }
  1815. if( !$add ){
  1816. $this->{$section} = $addr;
  1817. }else{
  1818. $this->{$section} = array_merge( $this->{$section} , $addr );
  1819. }
  1820. return ( 0 != count( $addr ) );
  1821. }
  1822. function analyzeAddr( $addr , $name ){
  1823. if( is_string( $addr ) ){
  1824. if( empty( $name ) ){
  1825. list( $name , $addr ) = $this->_extractNameAddr( $addr );
  1826. }else{
  1827. $addr = $this->extractAddr( $addr );
  1828. }
  1829. return array(array( $this->tokey['_ADDR'] => $addr , $this->tokey['_NAME'] => $name ));
  1830. }
  1831. // $addr is array
  1832. ## list( $addr , $void ) = $this->keyUpper( $addr );
  1833. $ret = array();
  1834. if( empty( $name ) || !is_array( $name ) ){
  1835. if(isset($addr[$this->tokey['_ADDR']])){
  1836. $addr[$this->tokey['_NAME']] = isset($addr[$this->tokey['_NAME']]) ? $addr[$this->tokey['_NAME']]:null;
  1837. return array( $addr );//ver 0.7.3a
  1838. }elseif( isset( $addr[0] ) && is_array( $addr[0] ) ){
  1839. foreach($addr as $ad){
  1840. ## list( $ad , $void ) = $this->keyUpper( $ad );
  1841. $_addr = isset( $ad[$this->tokey['_ADDR']] ) ? $this->extractAddr( $ad[$this->tokey['_ADDR']] ) : $this->extractAddr( $ad[0] ) ;
  1842. if(isset( $ad[$this->tokey['_NAME']] ) ){
  1843. $_name = $ad[$this->tokey['_NAME']];
  1844. }elseif( isset( $ad[1] ) ){
  1845. $_name = $ad[1];
  1846. }else{
  1847. $_name = null;
  1848. }
  1849. if( empty($_addr) ){
  1850. continue;
  1851. }else{
  1852. $ret[] = array_merge( $ad , array( $this->tokey['_ADDR'] => $_addr , $this->tokey['_NAME'] => $_name ) );
  1853. }
  1854. }
  1855. return $ret;
  1856. }else{
  1857. $_addr = $this->extractAddr( $addr[0] );
  1858. $_name = isset($addr[1]) ? $addr[1]:null;
  1859. $ret[] = array($this->tokey['_ADDR'] => $_addr , $this->tokey['_NAME'] => $_name);
  1860. }
  1861. return $ret; //fool proof
  1862. }else{
  1863. foreach( $addr as $key => $value ){
  1864. $_addr = $this->extractAddr( $value );
  1865. $_name = $name[$key] ;
  1866. if( empty( $_addr ) ){
  1867. continue;
  1868. }else{
  1869. $ret[] = array( $this->tokey['_ADDR'] => $_addr , $this->tokey['_NAME'] => $_name );
  1870. }
  1871. }
  1872. return $ret;
  1873. }
  1874. return $ret; // fool proof
  1875. }
  1876. //--------------------------------------------------------
  1877. // From MutibyteName<example@example.com> To MutibyteName
  1878. //--------------------------------------------------------
  1879. function _extractNameAddr( $addr ){
  1880. $formed_addr = $this->extractAddr( $addr );
  1881. if( empty( $formed_addr ) ){
  1882. return false;
  1883. }
  1884. $addr = trim($addr);
  1885. $addr = str_replace(array('<','>'),'',$addr);
  1886. $temp=strpos($addr,$formed_addr);
  1887. if( false === $temp ){
  1888. return null;
  1889. }
  1890. return array( substr( $addr , 0 , strpos( $addr , $formed_addr )) , $formed_addr );
  1891. }
  1892. function setContentArgs( $type , $param ){
  1893. $method_name = 'text';
  1894. if('HTML' == $type ){
  1895. $method_name = 'html';
  1896. }
  1897. $cont = null;
  1898. if(isset($param[$type])){
  1899. $cont = $param[$type];
  1900. }elseif(isset($param['CONTENT'])){
  1901. $cont = $param['CONTENT'];
  1902. }
  1903. return $this->{$method_name}(
  1904. $cont ,
  1905. isset($param['_CHARSET']) ? $param['_CHARSET']:null,
  1906. isset($param['ENC']) ? $param['ENC']:null,
  1907. isset($param['_ORG_CHARSET']) ? $param['_ORG_CHARSET']:null
  1908. );
  1909. }
  1910. //-------------------------------------------
  1911. // Main Routine Send()
  1912. // Option analyize
  1913. // Is To-Separate Mode ?
  1914. // loop:sendbase
  1915. // Already Created Mail?
  1916. // Create mail
  1917. // Additional Parameter(From User) Analyize (e.g. charset , subject etc...)
  1918. // (Not OOP MODE)
  1919. // Build Header(except Content-type etc) and Must Header Checking
  1920. // Both mode ? text only or html only or both ? or auto both
  1921. // Addition Attachment will do
  1922. // Select Body Structure by Decoration Pattern or else
  1923. // Build Body ( Recursive )
  1924. // Render Body with 'Content-type' Header and Boundary etc..
  1925. // + finalize( Recursive )
  1926. // Pass to the Header,first Content-type etc. that needs by Header Render Routine
  1927. // Set Default Header, MIME 1.0 etc
  1928. // Render Header and Render for SMTP Sender Text(Future)
  1929. // Debug Echo & log & error log will do if you want
  1930. //If error exsist , no sender(except ignore_error Property)
  1931. //-------------------------------------------
  1932. function headerDefault(){
  1933. $this->header['MIME-Version'] = '1.0';
  1934. if($this->debug > 0 ){
  1935. $this->header['X-QdmailDebug'] = trim(chunk_split ( base64_encode($this->iniGather()) , $this->mime_encode_max , $this->LFC."\t" ));
  1936. }
  1937. $this->header['X-'.$this->xmailer] = trim('version-'.$this->version . ' ' . $this->license .' http://hal456.net/qdmail PHPver '.PHP_VERSION);
  1938. if($this->smtp){
  1939. $sendby = 'SMTP';
  1940. }elseif($this->sendmail && !ini_get('SafeMode')){
  1941. $sendby = 'Sendmail';
  1942. }elseif($this->sendmail && ini_get('SafeMode')){
  1943. $sendby = 'MailFunction but Sendmail if no Safemode';
  1944. }else{
  1945. $sendby = 'MailFunction';
  1946. }
  1947. $this->header['X-'.$this->xmailer] .= $this->LFC . chr(9) . 'send-by '.$sendby;
  1948. }
  1949. function makeMessageId(){
  1950. $req_uri = empty($_SERVER['REQUEST_URI']) ? '':$_SERVER['REQUEST_URI'];
  1951. if(is_null($this->message_id_right)){
  1952. $right = 'hal456.net';
  1953. }else{
  1954. $right = $this->message_id_right;
  1955. }
  1956. $id = 'Qdmail.' . $this->version
  1957. . '_' . sha1( microtime() . $this->salt . mt_rand() . $req_uri )
  1958. . '@' . $right ;
  1959. return '<'.$id.'>';
  1960. }
  1961. function send( $option = null ){
  1962. if( is_null( $this->start_time )){
  1963. $this->start_time = microtime();
  1964. }
  1965. // mb language
  1966. if( 'neutral' === mb_language() ){
  1967. $this->setMbParameter('stack');
  1968. }
  1969. if( is_object( $option ) ){
  1970. $this->smtp_object = & $option;
  1971. $this->smtp = true;
  1972. $option = null ;
  1973. }
  1974. // Date: header
  1975. if( !is_null($this->time_zone) ){
  1976. $other = array_change_key_case($this->other_header,CASE_UPPER);
  1977. if( !isset($other['DATE']) ){
  1978. $this->other_header['Date'] = date('D, d M Y h:i:s ') . $this->time_zone;
  1979. }
  1980. }
  1981. $fg = true;
  1982. if( true === $this->toSeparate() ){
  1983. $stack_tos = array( $this->to , $this->cc , $this->bcc );
  1984. $tos = $this->to ;
  1985. # $this->cc( false ) ;
  1986. # $this->bcc( false ) ;
  1987. if( empty( $tos ) ){
  1988. $fg = $this->errorGather('recipient Header is not exsit line' ,__LINE__) ;
  1989. }else{
  1990. // To Separate mode
  1991. foreach($tos as $key => $to){
  1992. if( $this->simple_replace ){
  1993. if($this->replace_with_to_priority){
  1994. $to = array_merge( $this->selectReplace( $to , $key ) ,$to );
  1995. }else{
  1996. $to = array_merge( $to , $this->selectReplace( $to , $key ) );
  1997. }
  1998. }
  1999. $this->to( $to , null , false );
  2000. $this->debugEchoLf($this->to);
  2001. if( $this->auto_deco_judge ){
  2002. $this->deco_kind = $this->decoJudge( $this->to[0] );
  2003. }
  2004. if( $this->sendBase() ){
  2005. $this->is_create = false; // for next to
  2006. continue ;
  2007. }else{
  2008. $this->is_create = false; // for next to
  2009. $fg = $this->errorGather('Error \'TO\' Separate mode in Sendbase function , the Address is -> '.$this->to[0][$this->tokey['_ADDR']] ,__LINE__) ;
  2010. }
  2011. }
  2012. }
  2013. list( $this->to , $this->cc , $this->bcc ) = $stack_tos ;
  2014. }else{
  2015. // normal mode the end
  2016. $fg = $this->sendBase() ;
  2017. $this->is_create = false;
  2018. }
  2019. $this->setMbParameter($this->mb_parameter_stack);
  2020. $this->log();
  2021. //debug
  2022. $this->debugEcho('END');
  2023. if( $fg ){
  2024. return $this->errorGather();
  2025. }else{
  2026. return $this->errorGather('Send Error' ,__LINE__) ;
  2027. }
  2028. }
  2029. function selectReplace( $to , $key ){
  2030. $ret = array();
  2031. if( isset( $this->replace[$to[$this->tokey['_ADDR']]] ) ){
  2032. $ret = $this->replace[$to[$this->tokey['_ADDR']]];
  2033. }elseif( isset( $this->replace[$key] ) ){
  2034. $ret = $this->replace[$key];
  2035. }
  2036. return $ret ;
  2037. }
  2038. function sendBase(){
  2039. // stack bcc for allways bcc
  2040. unset( $stack_bcc ) ;
  2041. if( 0 != count( $this->allways_bcc ) ){
  2042. $stack_bcc = $this->bcc ;
  2043. $this->bcc( $this->allways_bcc , null , true );
  2044. }
  2045. // Message Id
  2046. if( $this->message_id){
  2047. $other = array_change_key_case($this->other_header,CASE_UPPER);
  2048. if(!isset($other['MESSAGE-ID'])){
  2049. $this->other_header['Message-Id'] = $this->makeMessageId();
  2050. }
  2051. }
  2052. if( !$this->is_create ){
  2053. $this->body = null;
  2054. $this->after_id = null;
  2055. $this->createMail();
  2056. }
  2057. if( isset($option) && !empty($option) ){
  2058. list( $option , $void ) = $this->keyUpper( $option );
  2059. }
  2060. // for smtp and sendmail
  2061. $this->extractrecipient() ;
  2062. $fg = true;
  2063. $fg_debug = ( 2 > $this->debug ) && !$this->render_mode;
  2064. if( $fg_debug && ( ( 0 === count( $this->error ) ) && ( 0 === count( $this->error_stack ) ) ) || $this->ignore_error ) {
  2065. //
  2066. // mail or SMTP or sendmail
  2067. //
  2068. if( $this->smtp ){
  2069. $fg = $this->sendBySmtp();
  2070. }elseif( $this->sendmail && !ini_get('safe_mode') ){
  2071. $fg = $this->sendBySendmail();
  2072. }elseif( ini_get('safe_mode') ){
  2073. $fg = mail(
  2074. trim( $this->header_for_mailfunction_to )
  2075. , trim( $this->header_for_mailfunction_subject )
  2076. , $this->content_for_mailfunction
  2077. , trim( $this->header_for_mailfunction_other )
  2078. );
  2079. }else{
  2080. $fg = mail(
  2081. trim( $this->header_for_mailfunction_to )
  2082. , trim( $this->header_for_mailfunction_subject )
  2083. , $this->content_for_mailfunction
  2084. , trim( $this->header_for_mailfunction_other )
  2085. , trim( $this->mta_option )
  2086. );
  2087. }
  2088. if( $fg ){
  2089. $this->done = array_merge( $this->done , $this->to , $this->cc , $this->bcc ) ;
  2090. }else{
  2091. $this->undone = array_merge( $this->undone , $this->to , $this->cc , $this->bcc ) ;
  2092. $err_mes = $this->smtp ? 'SMTP mail method':'PHP mail function';
  2093. $err_mes = $this->sendmail ? 'sendmail of localhost':$err_mes;
  2094. $fg =$this->errorGather('No send . Because '.$err_mes.' replied error' ,__LINE__);
  2095. }
  2096. }elseif( $fg_debug ){
  2097. $this->undone = array_merge( $this->undone , $this->to , $this->cc , $this->bcc ) ;
  2098. $fg = $this->errorGather('Error happen, see upper' ,__LINE__);;
  2099. }else{
  2100. $this->undone = array_merge( $this->undone , $this->to , $this->cc , $this->bcc ) ;
  2101. $fg = true ;
  2102. }
  2103. //debug
  2104. $bcc = null;
  2105. if( !empty($this->header_for_smtp_bcc )){
  2106. $bcc = '('.$this->header_for_smtp_bcc.')';
  2107. }
  2108. $this->debugEchoLf(
  2109. $bcc ,
  2110. $this->content_all_for_smtp,
  2111. // $this->header_for_smtp,
  2112. // $this->content_for_mailfunction,
  2113. $this->LFC.$this->LFC ,
  2114. date('Y-m-d H:i:s')
  2115. );
  2116. if($this->debug_report){
  2117. $this->debugReport('FILE');
  2118. }
  2119. if( isset( $stack_bcc ) ){
  2120. $this->bcc = $stack_bcc ;
  2121. }
  2122. return $this->errorGather() && $fg;
  2123. }
  2124. //-----------------
  2125. // checking
  2126. //-----------------
  2127. function mustCheck(){
  2128. if( 0 == count( $this->header_must ) ){
  2129. return $this->errorGather() ;
  2130. }
  2131. $must = true;
  2132. foreach( $this->header_must as $hdn ){
  2133. $header_upp = array_change_key_case( $this->header , CASE_UPPER );
  2134. if( ( !$this->smtp && empty( $header_upp[strtoupper($hdn)] ) ) || ( $this->smtp && empty( $header_upp[strtoupper($hdn)] ) && !isset( $this->smtp_server['FROM'] ) ) ){
  2135. $must = $this->errorGather('Must Header is not exist \''.$hdn.'\'' ,__LINE__) ;
  2136. }
  2137. }
  2138. return $must;
  2139. }
  2140. //-----------------------------------------------
  2141. // Create one mail
  2142. //-----------------------------------------------
  2143. function createMail( $boundary = null , $boundary_fix = false){
  2144. $this->_charsetDefFix();
  2145. //
  2146. // content(body) force convert to utf-8 ,
  2147. // because some system function can't do collectlly whitout utf-8,ex preg_replace,striptags
  2148. //
  2149. $this->content = $this->convertCharsetRecursive( $this->content , $this->qdmail_system_charset );
  2150. $this->buildHeader();
  2151. if(!$this->mustCheck()){
  2152. return false;
  2153. };
  2154. // Text only or Html Only or both ?
  2155. if( empty( $this->is_html ) ){
  2156. if( $this->issetContent( $this->content['HTML'] ) && $this->issetContent( $this->content['TEXT'] ) ){
  2157. $this->is_html = 'BOTH' ;
  2158. }elseif( $this->issetContent( $this->content['HTML'] ) && $this->auto_both ){
  2159. $this->content['TEXT'] = array(
  2160. 'content'=>$this->htmlToText( $this->content['HTML']['CONTENT'] )
  2161. );
  2162. $this->is_html = 'BOTH';
  2163. }elseif( $this->issetContent( $this->content['HTML'] ) && !$this->auto_both ){
  2164. $this->is_html = 'HTML';
  2165. }else{
  2166. $this->is_html = 'TEXT';
  2167. }
  2168. }
  2169. // Select Body Structure
  2170. if( !isset( $this->deco_kind ) ){
  2171. $structure_no = 0 ;
  2172. }else{
  2173. $structure_no = $this->deco_def[$this->deco_kind]['STRUCTURE'];
  2174. }
  2175. // Short cut on many recipients , samebody
  2176. if( !$this->body_build_once && $this->attach_build_once && $this->attach_already_build){
  2177. //only text and html making
  2178. $this->replaceBodyStructure('TEXT');
  2179. $this->replaceBodyStructure('HTML');
  2180. }elseif(
  2181. ($this->body_build_once && !$this->body_already_build)
  2182. ||
  2183. ( !$this->body_build_once && ( !$this->attach_build_once || ( $this->attach_build_once && !$this->attach_already_build ) ) )
  2184. ){
  2185. $this->body_structure = $this->buildBody( $this->structure[$structure_no] ,$boundary, false , $boundary_fix );
  2186. }
  2187. if( !$this->body_build_once || ($this->body_build_once && !$this->body_already_build) ){
  2188. $this->renderBody();//including Content-type Header
  2189. }
  2190. $this->header = array_merge($this->header , $this->header_content_type);
  2191. // user added header
  2192. $this->headerDefault();
  2193. $this->renderHeader();
  2194. //
  2195. // signed
  2196. //
  2197. if( false===$this->sign ){
  2198. $this->content_all_for_smtp = $this->header_for_smtp . $this->LFC . $this->content_for_mailfunction;
  2199. }elseif($this->pgp){
  2200. // future PGP
  2201. }else{
  2202. // S/MIME
  2203. $this->content_all_for_smtp = $this->signSmime();
  2204. if(false===$this->content_all_for_smtp){
  2205. return $this->errorGather('Sign Error S/MIME',__LINE__);
  2206. }
  2207. }
  2208. $this->is_create=true;
  2209. }
  2210. function signSmime(){
  2211. if( !$this->smtp ){
  2212. return $this->errorGather('S/MIME needs SMTP Send,now You spcify no smtp',__LINE__);
  2213. }
  2214. // Path to certificate file , by Win or other OS
  2215. if(empty($this->certificate_file_path)){
  2216. $this->certificate_file_path = $this->isWin() ? $this->certificate_file_path_win : $this->certificate_file_path_unix;
  2217. }
  2218. if(empty($this->certificate_temp_path)){
  2219. $this->certificate_file_path = $this->isWin() ? $this->certificate_file_path_win : $this->certificate_file_path_unix;
  2220. }
  2221. $path = $this->certificate_file_path . QD_DS ;
  2222. if('PFX'===strtoupper(substr($this->certificate_file,-3))){
  2223. if(!function_exists('openssl_pkcs12_read')){
  2224. return $this->errorGather('You can not specify *.pfx type, please *.pem type becuase your PHP Version do not support \'openssl_pkcs12_read\'',__LINE__);
  2225. }
  2226. if(openssl_pkcs12_read(file_get_contents($path.$this->certificate_file),$ret,$this->certificate_pass)){
  2227. $private_key = $ret['pkey'];
  2228. $certificate = $ret['cert'];
  2229. }else{
  2230. return $this->errorGather('Illegal Certificate File \''.$path.$this->certificate_file.'\' or Incorrect Password ',__LINE__);
  2231. }
  2232. }else{
  2233. $private_key = file_get_contents( $path . $this->private_key_file );
  2234. $certificate = file_get_contents( $path . $this->certificate_file );
  2235. }
  2236. $temp = sha1($this->content_for_mailfunction).'.txt';
  2237. $temp_filename = $this->certificate_temp_path.QD_DS.'temp'.$temp;
  2238. $temp_signed_filename = $this->certificate_temp_path.QD_DS.'temp_signed'.$temp;
  2239. $fp = fopen( $temp_filename , "w" );
  2240. fputs( $fp , $this->content_for_mailfunction . $this->LFC );
  2241. fclose($fp);
  2242. unset($this->header_for_smtp_array['MIME-Version']);
  2243. openssl_pkcs7_sign(
  2244. $temp_filename,
  2245. $temp_signed_filename,
  2246. $certificate,
  2247. array($private_key, $this->certificate_pass),
  2248. $this->header_for_smtp_array
  2249. );
  2250. $ret = file_get_contents($temp_signed_filename);
  2251. unlink ( $temp_signed_filename );
  2252. unlink ( $temp_filename );
  2253. return $ret;
  2254. }
  2255. function replaceBodyStructure( $kind ){
  2256. $content_type = ( 'TEXT' === $kind ) ? 'text/plain':'text/html';
  2257. $false = false;
  2258. $rep = & $this->serchBodyStructure( $content_type , $this->body_structure , $false );
  2259. if( false === $rep ){
  2260. return false;
  2261. }
  2262. list( $content , $charset , $enc ) = $this->makeContentText( $this->content[$kind] , $kind );
  2263. $rep['CONTENT'] = $content;
  2264. $rep['HEADER']['Content-Type'] = $content_type.'; charset="' . $charset . '"';
  2265. $rep['HEADER']['Content-Transfer-Encoding'] = $enc ;
  2266. }
  2267. function & serchBodyStructure( $content_type , & $bbs , & $false ){
  2268. foreach($bbs as $fkey => $bs){
  2269. if( isset( $bs['HEADER']) && ( 0 < count($bs['HEADER']))) {
  2270. $len = strlen($content_type);
  2271. foreach( $bs['HEADER'] as $key => $cont ){
  2272. if( ('CONTENT-TYPE' === strtoupper($key)) && ($content_type === substr($cont,0,$len)) ){
  2273. return $bbs[$fkey];
  2274. }
  2275. }
  2276. }
  2277. if( !isset( $bs['CONTENT']) || ( isset( $bs['CONTENT']) && !is_array( $bs['CONTENT']))){
  2278. continue;
  2279. }
  2280. $ret = & $this->serchBodyStructure( $content_type , $bbs[$fkey]['CONTENT'] , $false );
  2281. return $ret;
  2282. }
  2283. return $false;
  2284. }
  2285. //except Content-type,user option
  2286. function buildHeader(){
  2287. $header = array();
  2288. foreach( $this->addr_many as $section => $many ){
  2289. if( 0 == count( $this->{strtolower( $section )} ) ){
  2290. continue;
  2291. }
  2292. foreach( $this->{strtolower($section)} as $one ){
  2293. $mime=$this->mimeEncode(
  2294. $one[$this->tokey['_NAME']],
  2295. isset($one['_CHARSET']) ? $one['_CHARSET'] : $this->charset_header,
  2296. isset($one['_ORG_CHARSET']) ? $one['_ORG_CHARSET'] : null,
  2297. strlen($section)+2
  2298. );
  2299. // bcc header is not allowed MimeName
  2300. if( empty( $mime ) || 'BCC'===strtoupper( $section ) ){
  2301. $header[$this->addr_head_name[$section]][] = $one[$this->tokey['_ADDR']];
  2302. }else{
  2303. $header[$this->addr_head_name[$section]][] = $mime.' <'.$one[$this->tokey['_ADDR']].'>';
  2304. }
  2305. }
  2306. if( !$many ){
  2307. $header[$this->addr_head_name[$section]] = array( array_pop( $header[$this->addr_head_name[$section]] ) );
  2308. }
  2309. }
  2310. if( !empty( $this->subject ) ){
  2311. //replace
  2312. if( $this->simple_replace ){
  2313. $subj = $this->replace( $this->subject['CONTENT'] , $this->to[0] );
  2314. }else{
  2315. $subj = $this->subject['CONTENT'] ;
  2316. }
  2317. $header['Subject']=$this->mimeEncode(
  2318. $subj ,
  2319. isset($this->subject['_CHARSET']) ? $this->subject['_CHARSET']:$this->charset_header,
  2320. isset($this->subject['_ORG_CHARSET']) ? $this->subject['_ORG_CHARSET'] : null,
  2321. 9 //strlen(subject)+2
  2322. );
  2323. }
  2324. $this->header = array_merge( $header , $this->other_header ) ;
  2325. }
  2326. function renderHeader(){
  2327. if(isset($this->header['To'])){
  2328. $this->header_for_mailfunction_to = implode( ','.$this->LFC.' ' , $this->header['To'] );
  2329. unset( $this->header['To'] ) ;
  2330. }
  2331. if(isset($this->header['Subject'])){
  2332. $this->header_for_mailfunction_subject = $this->header['Subject'];
  2333. unset( $this->header['Subject'] ) ;
  2334. }
  2335. $this->header_for_mailfunction_other = null;
  2336. $header_for_smtp = array();
  2337. $this->header_for_smtp_bcc = null;
  2338. $header_for_smtp['To'] = $this->header_for_mailfunction_to;
  2339. $header_for_smtp['Subject'] = $this->header_for_mailfunction_subject;
  2340. foreach( $this->header as $key => $value ){
  2341. if( is_array( $value ) ){
  2342. $add = implode( ',' . $this->LFC . chr(9) , $value );
  2343. }else{
  2344. $add = $value;
  2345. }
  2346. if( 'BCC' !== strtoupper($key) ){
  2347. $header_for_smtp[$key] = $add ;
  2348. }else{
  2349. $this->header_for_smtp_bcc = $key . ': ' . $add . $this->LFC ;
  2350. }
  2351. $this->header_for_mailfunction_other .= $key . ': ' . $add . $this->LFC;
  2352. unset( $this->header[$key] );
  2353. }
  2354. $this->header_for_smtp = '';
  2355. foreach($header_for_smtp as $key => $value){
  2356. $this->header_for_smtp .= $key.': '.$value.$this->LFC;
  2357. }
  2358. if($this->smime){
  2359. $this->header_for_smtp_array = $header_for_smtp;
  2360. }
  2361. }
  2362. //-------------------------
  2363. // $ret = array(
  2364. // 'BOUNDARY' =>
  2365. // 'HEADER' =>
  2366. // 'CONTENT' =>array(
  2367. // (Recursive)
  2368. // )
  2369. // )
  2370. //-------------------------
  2371. function isInlineImage($filename){
  2372. if(!empty($this->content['HTML']['ORG_CONTENT'])){
  2373. $cont = $this->content['HTML']['ORG_CONTENT'];
  2374. }
  2375. if(!empty($this->content['HTML']['CONTENT'])){
  2376. $cont = $this->content['HTML']['CONTENT'];
  2377. }
  2378. if(empty($cont)){
  2379. return false;
  2380. }
  2381. $enc = $this->qd_detect_encoding($cont);
  2382. $cont = $this->qd_convert_encoding($cont,'UTF-8',$enc);
  2383. $name = $filename;
  2384. $enc = $this->qd_detect_encoding($name);
  2385. $name = $this->qd_convert_encoding($filename,'utf-8',$enc);
  2386. if( 0 < preg_match('/"cid:'.$name.'"/is' , $cont ) ){
  2387. return true;
  2388. }else{
  2389. return false;
  2390. };
  2391. }
  2392. function buildBody( $structure , $boundary = null , $rel = false , $boundary_fix = false){
  2393. $ret = array();
  2394. $one = array();
  2395. if( is_null( $boundary ) ){
  2396. $boundary = $this->makeBoundary();
  2397. }
  2398. $ret_boundary = $boundary ;
  2399. foreach($this->attach as $key => $att){
  2400. if($this->isInlineImage(basename($this->attach[$key]['PATH'])) && empty($this->attach[$key]['CONTENT-ID'])){
  2401. $this->attach[$key]['CONTENT-ID'] = basename($this->attach[$key]['PATH']);
  2402. }
  2403. }
  2404. foreach( $structure as $key => $value ){
  2405. $ret_header = array();
  2406. $ret_cont = array();
  2407. if( is_array( $value ) ){
  2408. $next_boundary = $boundary_fix ? $boundary:$this->makeBoundary();
  2409. $ret_header['Content-Type'] = strtolower($key).';' . $this->LFC
  2410. . ' boundary="' . $next_boundary . '"' ;
  2411. $rel = false;
  2412. $ret_cont = $this->buildBody( $value , $next_boundary , $rel);
  2413. if( 0 == count($ret_cont) && $structure['OMIT']){
  2414. continue;
  2415. }elseif( 1 == count($ret_cont) && $structure['OMIT']){
  2416. $one = null;
  2417. $ret_cont[0]['BOUNDARY'] = '--'.$boundary ;
  2418. $ret[] = $ret_cont[0];
  2419. continue;
  2420. }else{
  2421. $one = null;
  2422. $ret_cont[] = array( 'BOUNDARY' => null ,'HEADER' => array() ,'CONTENT' => '--' . $next_boundary . '--' );
  2423. $ret[] = array( 'BOUNDARY' => '--' . $ret_boundary , 'HEADER' => $ret_header , 'CONTENT' => $ret_cont );
  2424. continue;
  2425. }
  2426. }else{
  2427. switch( strtolower($key) ){
  2428. case 'image':
  2429. foreach( $this->attach as $att){
  2430. if( ( 'INLINE' === $value ) && $this->isSetCid( $att ) ){
  2431. $ret_cont[]= $this->buildAttach( $att , $boundary , true ) ;
  2432. }elseif( ( 'NOT_INLINE' === $value ) && !$this->isSetCid( $att )){
  2433. $ret_cont[] = $this->buildAttach( $att , $boundary , false ) ;
  2434. }if( 'BOTH' === $value ){
  2435. $ret_cont[] = $this->buildAttach( $att , $boundary , $this->isSetCid( $att ) ) ;
  2436. }
  2437. }
  2438. break;
  2439. case 'html':
  2440. $this->content['ORG_CONTENT'] = $this->content['HTML'];
  2441. list( $content , $charset , $enc ) = $this->makeContentText( $this->content['HTML'] , 'HTML' );
  2442. $ret_header['Content-Type'] = 'text/html; charset="' . $charset . '"';
  2443. $ret_header['Content-Transfer-Encoding'] = $enc ;
  2444. $ret_cont = $content ;
  2445. break;
  2446. case 'plain':
  2447. list( $content , $charset , $enc ) = $this->makeContentText( $this->content['TEXT'] , 'TEXT' );
  2448. $ret_header['Content-Type'] = 'text/plain; charset="' . $charset . '"';
  2449. $ret_header['Content-Transfer-Encoding'] = $enc ;
  2450. $ret_cont = $content ;
  2451. break;
  2452. case 'omit':
  2453. $one = null;
  2454. break;
  2455. }
  2456. if( !empty($ret_cont) ){
  2457. $ret[] = array( 'BOUNDARY' => '--' . $boundary , 'HEADER' => $ret_header , 'CONTENT' => $ret_cont );
  2458. }
  2459. }
  2460. }
  2461. return $ret ;
  2462. }
  2463. function renderBody(){
  2464. if( ( 0 === count( $this->body_structure ) ) && !$this->body_empty_allow ){
  2465. return $this->errorGather('Empty Body do not allowed. If you want to send empty Mail , use method -> bodyEmptyAllow(true)' ,__LINE__) ;
  2466. }elseif( 0 === count( $this->body_structure ) ){
  2467. $this->body_structure[0]['HEADER'] = array();
  2468. }
  2469. if( !$this->smime ){
  2470. foreach( $this->body_structure[0]['HEADER'] as $key => $value){
  2471. $this->header_content_type[$key]=$value;
  2472. }
  2473. $this->body_structure[0]['HEADER'] = array();
  2474. }
  2475. $this->body_structure[0]['BOUNDARY'] = null;
  2476. $this->content_for_mailfunction = rtrim($this->finalize( $this->body_structure )) ;
  2477. $this->body_already_build = true ;
  2478. }
  2479. function finalize( $array ){
  2480. foreach( $array as $ar ){
  2481. $header = $this->expandHeader( $ar['HEADER'] );
  2482. $bd = isset($ar['BOUNDARY']) ? trim($ar['BOUNDARY']) . $this->LFC : null ;
  2483. if(is_array($ar['CONTENT'])){
  2484. if( !empty( $header ) ){
  2485. $this->body = $this->body . $bd . $header . $this->LFC . $this->LFC ;
  2486. }
  2487. $this->finalize( $ar['CONTENT'] );
  2488. }else{
  2489. if( !empty( $header ) ){
  2490. $header .= $this->LFC . $this->LFC ;
  2491. }
  2492. $add = $bd . $header . $ar['CONTENT'] ;
  2493. $this->body = $this->body . $add . $this->LFC . $this->LFC ;
  2494. }
  2495. }
  2496. return $this->body;
  2497. }
  2498. function expandHeader( $hds ){
  2499. if(empty($hds)){
  2500. return null;
  2501. }
  2502. $header = null;
  2503. foreach( $hds as $key => $value ){
  2504. if( isset( $value ) ){
  2505. $header .= $key . ': ' . $value . $this->LFC;
  2506. }
  2507. }
  2508. return trim($header);
  2509. }
  2510. function makeBoundary(){
  2511. static $rec = 0 ;
  2512. $boundary = '__Next-' . $rec . '-' . $this->qdmail_md( null , 65 , 90 ) . 'UWRtYWlsIEFHUEx2Mw==' . base64_encode( $this->qdmail_md() ) . '__';
  2513. $rec ++ ;
  2514. return $boundary;
  2515. }
  2516. function makeContentText( $content , $is_text = 'TEXT' ){
  2517. $flag_wrp = ( 'TEXT' == $is_text ) ? true:false;
  2518. $enc = ( 'HTML' == $is_text ) ? $this->content_transfer_enc_html : $this->content_transfer_enc_text ;
  2519. if( is_array( $content ) ){
  2520. $content = array_change_key_case( $content , CASE_UPPER );
  2521. $_content = $content['CONTENT'];
  2522. $org_char = $this->qdmail_system_charset ; //already converted to system charaset
  2523. $target_char = isset($content['_CHARSET'])
  2524. ? $content['_CHARSET'] : $this->charset_content;
  2525. $length = isset($content['LENGTH'])
  2526. ? $content['LENGTH'] : $this->wordwrap_length;
  2527. $content_transfer_enc = !empty($content['ENC'])
  2528. ? $content['ENC'] : $enc;
  2529. $content = $_content;
  2530. }else{
  2531. $org_char = $this->qdmail_system_charset ;
  2532. $target_char = $this->charset_content;
  2533. $length = $this->wordwrap_length;
  2534. $content_transfer_enc = $enc;
  2535. }
  2536. // fix crlf
  2537. list($content,$void) = $this->clean($content);
  2538. // Content_replace
  2539. if( $this->simple_replace ){
  2540. $content = $this->replace( $content , $this->to[0] );
  2541. }
  2542. // Content-id replace
  2543. if(!$this->content_id_fix){
  2544. $content = $this->replaceCid( $content );
  2545. }
  2546. // content modify by external function at HTML
  2547. if( 'HTML' == $is_text && isset($this->deco_kind) && isset($this->deco_def[$this->deco_kind]['HTML_EXTERNAL']) ){
  2548. $temp = $this->deco_def[$this->deco_kind]['HTML_EXTERNAL'];
  2549. if( is_array( $temp ) && 'this'==$temp[0]){
  2550. $content = $this->{$temp[1]}($content);
  2551. }elseif( !empty( $temp ) ){
  2552. $content = call_user_func( array($temp[0],$temp[1]) , $content);
  2553. }
  2554. }
  2555. if( $this->wordwrap_allow && $flag_wrp && false !== $length ){
  2556. $content = $this->mbWordwrap( $content , $length );
  2557. }
  2558. $enc_upp = strtoupper($content_transfer_enc);
  2559. if( $this->kana && 'ja'===$this->language && (('BASE64' === $enc_upp && $this->kana_content_relation) || 'BASE64' !== $enc_upp )){
  2560. $content = mb_convert_kana( $content , 'KV' , $org_char );
  2561. }
  2562. $content = $this->qd_convert_encoding( $content , $target_char , $org_char );
  2563. if( 'BASE64' == $enc_upp && !empty( $content ) ){
  2564. $content = chunk_split( base64_encode( $content ) );
  2565. }elseif( ( 'QUOTED-PRINTABLE' == $enc_upp || 'QP' == $enc_upp ) && !empty( $content ) ){
  2566. $content_transfer_enc = 'quoted-printable';
  2567. $content = $this->quotedPrintableEncode( $content );
  2568. }
  2569. return array( $content , $target_char , $content_transfer_enc );
  2570. }
  2571. //--------------
  2572. // html => text
  2573. // must utf-8 because of preg_replace & strip_tags function
  2574. //--------------
  2575. function htmlToText( $html ){
  2576. $_content = str_replace( array( "\r" , "\n" ) , '' , $html );
  2577. $_content = preg_replace( array( '/<br>/i','/<\/p>/i' , '/<br\s*\/>/i' , '/<\/div>/i' , '/<\/h[1-9]>/i' , '/<\/ol>/i' , '/<\/dl>/i' , '/<\/ul>/i' , '/<li>/i' , '/<\/li>/i' , '/<\/dd>/i' , '/<\/blockquote>/i' , '/<hr\s*\/?>/i' , '/<\/tr>/i' , '/<\/caption>/i' ), $this->LFC , $_content );
  2578. $_content = preg_replace( array( '/<\/td>/i' , '/<\/th>/i' ), ' ' , $_content );
  2579. $_content = preg_replace( "/\\r?\\n/", "\n" , $_content );
  2580. $_content = preg_replace( "/[\\n]+/", "\n" , $_content );
  2581. $_content = preg_replace( "/\\n/", $this->LFC , $_content );
  2582. return trim(strip_tags($_content));
  2583. }
  2584. function mimeEncode( $subject , $charset , $org_charset = null , $first_line_front_words_length = 12 ) {
  2585. $enc = isset($org_charset) ? $org_charset:$this->qd_detect_encoding($subject);
  2586. if( empty($subject) || ( strlen(bin2hex($subject))/2 == mb_strlen($subject,$enc) ) ){
  2587. return trim(chunk_split($subject, $this->mime_encode_max, "\r\n "));
  2588. }
  2589. if($this->kana && 'ja'===$this->language){
  2590. $subject = mb_convert_kana( $subject , 'KV' , $enc );
  2591. }
  2592. $subject = $this->qd_convert_encoding( $subject , $charset , $enc );
  2593. $start = "=?" . $charset . "?B?";
  2594. $end = "?=";
  2595. $spacer = $end . $this->LFC . chr(9) . $start;
  2596. $length = $this->mime_encode_max - strlen($start) - strlen($end);
  2597. $pointer = 1;
  2598. $cut_start = 0;
  2599. $line = null;
  2600. $_ret = array();
  2601. $max = mb_strlen( $subject ,$charset );
  2602. while( $pointer <= $max ){
  2603. $line = mb_substr( $subject , $cut_start , $pointer-$cut_start , $charset );
  2604. $bs64len = strlen(bin2hex(base64_encode($line)))/2;
  2605. if( (0!==count($_ret) && $bs64len <= $length) || (0===count($_ret) && $bs64len <= ($length-$first_line_front_words_length)) ){
  2606. $pointer ++;
  2607. }else{
  2608. $_ret[] = base64_encode($line) ;
  2609. $cut_start = $pointer;
  2610. }
  2611. }
  2612. if( strlen( trim( $line ) ) > 0){
  2613. $_ret[] = base64_encode( $line );
  2614. }
  2615. $ret = $start . implode( $spacer , $_ret ) . $end;
  2616. $ret = preg_replace(array('/\0/is','/\r[^\n]/is'),'',$ret);
  2617. return $ret ;
  2618. }
  2619. function extractAddr($addr_including_sclub){
  2620. if( preg_match( '/<([^>]+)>/' , $addr_including_sclub , $match ) == 0){
  2621. $addr = $addr_including_sclub;
  2622. }else{
  2623. $addr = $match[1];
  2624. }
  2625. $temp = $this->address_validation_method;
  2626. if( is_array( $temp ) && 'this'==$temp[0]){
  2627. $fg = $this->{$temp[1]}( $addr );
  2628. $mess ="System";
  2629. }elseif( !empty( $temp ) ){
  2630. $fg = call_user_func( array($temp[0],$temp[1]) , $addr );
  2631. $mess ="USER";
  2632. }
  2633. if( $fg ){
  2634. return $addr ;
  2635. }else{
  2636. return $this->errorGather('Illegal Mail Address'.$mess.'Validete Address Method' ,__LINE__) ;
  2637. }
  2638. }
  2639. //----------------------------------------------------------------
  2640. // Charset ReDecear - if Decoration Pattern needs anather charset
  2641. // (Overload)
  2642. //----------------------------------------------------------------
  2643. function _charsetDefFix(){
  2644. if( !isset( $this->deco_kind ) ){
  2645. return ;
  2646. }
  2647. if(isset($this->deco_def[$this->deco_kind]['_CHARSET'])){
  2648. $this->charset_content = $this->deco_def[$this->deco_kind]['_CHARSET'];
  2649. }
  2650. if(isset($this->deco_def[$this->deco_kind]['ENC_TEXT'])){
  2651. $this->content_transfer_enc_text = $this->deco_def[$this->deco_kind]['ENC_TEXT'];
  2652. }
  2653. if(isset($this->deco_def[$this->deco_kind]['ENC_HTML'])){
  2654. $this->content_transfer_enc_html = $this->deco_def[$this->deco_kind]['ENC_HTML'];
  2655. }
  2656. }
  2657. //------------------------------------------------------------
  2658. // Addition Header(in send( param ) ) set to $this->{to} etc.
  2659. // and return UpperCase keys
  2660. //------------------------------------------------------------
  2661. function setAddr( $header ){
  2662. if( empty( $header ) ){
  2663. return array( $header , array() );
  2664. }
  2665. list( $header , $link_hd )= $this->keyUpper( $header );
  2666. foreach( $this->addr_many as $section => $void){
  2667. if( !isset( $header[strtoupper($section)] ) ){
  2668. continue;
  2669. }
  2670. $this->{strtolower( $section )}( $header[strtoupper($section)] , null , true );
  2671. unset( $header[strtoupper( $section )] );
  2672. }
  2673. // TO Separate mode?
  2674. if( true === $this->toSeparate() ){
  2675. $this->cc(false);
  2676. $this->bcc(false);
  2677. }
  2678. return array( $header , $link_hd );
  2679. }
  2680. function convertCharsetRecursive( $array , $target_enc ){
  2681. if( is_array( $array ) && !empty( $array['_ORG_CHARSET'] ) ){
  2682. foreach($array as $key => $value){
  2683. if( false === strpos( $key , '_CHARSET' ) ){
  2684. $array[$key] = $this->qd_convert_encoding($value , $target_enc ,$array['_ORG_CHARSET'] );
  2685. }
  2686. }
  2687. }elseif( is_string( $array ) || is_numeric( $array ) ){
  2688. $enc = $this->qd_detect_encoding( $array );
  2689. $array = $this->qd_convert_encoding($array , $target_enc , $enc );
  2690. }elseif( is_array( $array ) ){
  2691. foreach( $array as $key => $value ){
  2692. $ret[$key] = $this->convertCharsetRecursive( $value , $target_enc );
  2693. }
  2694. $array = $ret ;
  2695. }elseif( empty( $array ) ){
  2696. $array = null ;
  2697. }else{
  2698. $this->error[]='Error convertCharsetRecursive, invalid type ,line->'.__LINE__;
  2699. }
  2700. return $array ;
  2701. }
  2702. function extractrecipient(){
  2703. $hd = array('to','cc','bcc') ;
  2704. $ret = array();
  2705. foreach( $hd as $hdn ){
  2706. foreach($this->{$hdn} as $addr ){
  2707. $ret[] = $addr[$this->tokey['_ADDR']] ;
  2708. }
  2709. }
  2710. if( 0 === count( $ret ) ){
  2711. return $this->errorGather('No recipient' ,__LINE__) ;
  2712. }else{
  2713. $this->recipient = $ret ;
  2714. return $this->errorGather();
  2715. }
  2716. }
  2717. //------------------------------------------------------------------------
  2718. // Attachment Routine
  2719. // attach - set to $this->attach array
  2720. // attach OneArray(1 array pattern array('path','attacheName'))
  2721. // attach Singe (2 string pattern ('path','attacheName') )
  2722. // attachFull - Base Routine allattch routine call him
  2723. // buildAttach - called buildBody method
  2724. //------------------------------------------------------------------------
  2725. //
  2726. //
  2727. //array('path_filename','attach_name','mime_type','target_charset','org_charset', );
  2728. //
  2729. //
  2730. //
  2731. function attach( $param , $add = false ){
  2732. list( $stack , $this->attach ) = array( $this->attach , array() );
  2733. if(is_string($param)){
  2734. $param = array($param);
  2735. }
  2736. if( ($this->inline_mode || $this->simple_attach) && !is_array($param[0])){
  2737. foreach($param as $one){
  2738. $param_temp[] = array( $one );
  2739. }
  2740. $param = $param_temp;
  2741. }
  2742. $te_st = reset($param);
  2743. if(!is_array($te_st)){
  2744. $param = array( $param );
  2745. }
  2746. foreach($param as $par){
  2747. if(empty($par)){
  2748. continue;
  2749. }
  2750. $path_filename = isset($par['PATH']) ? $par['PATH']:$par[0];
  2751. if(isset($par['NAME'])){
  2752. $attach_name = $par['NAME'];
  2753. }elseif(isset($par[1])){
  2754. $attach_name = $par[1];
  2755. }else{
  2756. $attach_name = basename( $path_filename ) ;
  2757. }
  2758. $mime_type = null;
  2759. if(isset($par['CONTENT-TYPE'])){
  2760. $mime_type = $par['CONTENT-TYPE'];
  2761. }elseif(isset($par['MIME-TYPE'])){
  2762. $mime_type = $par['MIME-TYPE'];
  2763. }elseif(isset($par[2])){
  2764. $mime_type = $par[2];
  2765. }
  2766. $content_id = null;
  2767. if(isset($par['CONTENT-ID'])){
  2768. $content_id = $par['CONTENT-ID'];
  2769. }elseif(isset($par[3])){
  2770. $content_id = $par[3];
  2771. }
  2772. $target_charset = null;
  2773. if(isset($par['_CHARSET'])){
  2774. $target_charset = $par['_CHARSET'];
  2775. }elseif(isset($par[4])){
  2776. $target_charset = $par[4];
  2777. }
  2778. $org_charset = null;
  2779. if(isset($par['_ORG_CHARSET'])){
  2780. $org_charset = $par['_ORG_CHARSET'];
  2781. }elseif(isset($par[5])){
  2782. $org_charset = $par[5];
  2783. }
  2784. $direct = null;
  2785. if(isset($par['DIRECT'])){
  2786. $direct = $par['DIRECT'];
  2787. }elseif(isset($par[6])){
  2788. $direct = $par[6];
  2789. }
  2790. $bare = false;
  2791. if(isset($par['BARE'])){
  2792. $bare = $par['BARE'];
  2793. }elseif(isset($par[7])){
  2794. $bare = $par[7];
  2795. }
  2796. $this->attach[] = array(
  2797. 'PATH' => $path_filename,
  2798. 'NAME' => $attach_name,
  2799. 'CONTENT-TYPE' => $mime_type,
  2800. 'CONTENT-ID' => $content_id,
  2801. '_CHARSET' => $target_charset,
  2802. '_ORG_CHARSET' => $org_charset,
  2803. 'DATA' => $direct,
  2804. 'DIRECT' => isset($direct) ,
  2805. 'BARE' => $bare,
  2806. );
  2807. }
  2808. if($add){
  2809. $this->attach = array_merge( $stack , $this->attach );
  2810. }
  2811. $this->attach_already_build = false ;
  2812. return $this->errorGather() ;
  2813. }
  2814. //--------------------------------------------------------
  2815. // Build attachment one file , called by buildBody method
  2816. // $one is array , no recursive ,must ['PATH'] element
  2817. //--------------------------------------------------------
  2818. function buildAttach( $one , $boundary , $inline){
  2819. $ret_boundary = null;
  2820. $ret_header = array();
  2821. $ret_content = null;
  2822. $one = array_change_key_case( $one , CASE_UPPER);
  2823. if( !isset($one['NAME'] )){
  2824. $one['NAME'] = basename( $one['PATH'] );
  2825. }
  2826. //Content-Type
  2827. if( isset( $one['CONTENT-TYPE'] )){
  2828. $type = $one['CONTENT-TYPE'];
  2829. }elseif( 0 != preg_match( '/\.([^\.]+)$/' , $one['NAME'] , $matches )){
  2830. $type = isset( $this->attach_ctype[strtolower($matches[1])] )
  2831. ? $this->attach_ctype[strtolower($matches[1])] : 'unkown';
  2832. }elseif(0 != preg_match( '/\.([^\.]+)$/' , $one['PATH'] , $matches )){
  2833. $type = isset( $this->attach_ctype[strtolower($matches[1])])
  2834. ? $this->attach_ctype[strtolower($matches[1])] : 'unkown';
  2835. if( $this->auto_ext && 'unkown' != $type ){
  2836. $one['NAME'] .= '.'.$matches[1];
  2837. }
  2838. }else{
  2839. $type = 'unkown';
  2840. }
  2841. if( isset( $one['_CHARSET'] ) ){
  2842. $charset = $one['_CHARSET'];
  2843. }else{
  2844. $charset = $this->charset_attach_filename;
  2845. }
  2846. if( isset( $one['_ORG_CHARSET'] ) ){
  2847. $org_charset = $one['_ORG_CHARSET'];
  2848. }else{
  2849. $org_charset = null;
  2850. }
  2851. $filename = $this->mimeEncode( $one['NAME'] , $charset , $org_charset , 20 );
  2852. //is Inline ?
  2853. if( $inline ){
  2854. $id = $this->content_id_fix ? $one['CONTENT-ID']:$this->makeContentId($one['CONTENT-ID']);
  2855. $content_id = '<' . $id . '>' ;
  2856. $content_disposition = 'inline';//attachment for au?
  2857. }else{
  2858. $content_id = null ;
  2859. $content_disposition = 'attachment';
  2860. }
  2861. // do it need Disposition Heaer ?
  2862. if( isset( $this->deco_kind ) && false===$this->deco_def[$this->deco_kind]['DISPOSITION']){
  2863. $disposition = null;
  2864. }else{
  2865. $disposition = $content_disposition.';'.$this->LFC
  2866. .' filename="'.$filename.'"'
  2867. ;
  2868. }
  2869. $ret_boundary = '--'.$boundary ;
  2870. $ret_header['Content-Type'] = $type.'; name="'.$filename.'"'
  2871. ;
  2872. $ret_header['Content-Transfer-Encoding'] = 'base64' ;
  2873. $ret_header['Content-Id'] = isset($content_id) ? trim( $content_id ) : null ;
  2874. if(!empty($disposition)){
  2875. $ret_header['Content-Disposition'] = $disposition;
  2876. }
  2877. if( !empty( $one['DIRECT'] ) ){
  2878. $cont=$one['DATA'];
  2879. }else{
  2880. $path_filename = $this->attachPathFix( $one['PATH'] );
  2881. if( !file_exists ( $path_filename )){
  2882. $this->error[]='No attach file \''.$path_filename.'\' line->'.__LINE__;
  2883. return false;
  2884. }else{
  2885. $cont=file_get_contents( $path_filename );
  2886. }
  2887. }
  2888. if( isset( $one['BARE'] ) && true === $one['BARE'] ){
  2889. $ret_content = $one['DATA'];
  2890. }else{
  2891. $ret_content = trim(chunk_split(base64_encode($cont)));
  2892. }
  2893. $this->attach_already_build = true;
  2894. return array(
  2895. 'BOUNDARY' =>$ret_boundary ,
  2896. 'HEADER' =>$ret_header ,
  2897. 'CONTENT' =>$ret_content
  2898. );
  2899. }
  2900. function isSetCid( $array ){
  2901. return isset( $array['CONTENT-ID'] ) && ( '' !== $array['CONTENT-ID'] ) ;
  2902. }
  2903. function makeContentId( $id ){
  2904. if( is_null( $this->after_id ) ){
  2905. $fromaddr = isset($this->header['From'][0]) ? $this->extractAddr($this->header['From'][0]):null;
  2906. $this->after_id = mt_rand() . '_'. str_replace(array('@','-','/','.'),'', $fromaddr .'_'. $this->xmailer.'_'.$this->version );
  2907. }
  2908. return str_replace(array('@','-','/','.'),'', 'id_'.$id ).'_@_'. $this->after_id ;
  2909. }
  2910. function replaceCid( $content ){
  2911. if( !isset( $this->deco_kind ) ){
  2912. return $content;
  2913. }
  2914. foreach($this->attach as $att){
  2915. if( $this->isSetCid( $att ) ){
  2916. $orig = preg_quote($att['CONTENT-ID'] ,'/');
  2917. $rep = $this->makeContentId( $att['CONTENT-ID'] );
  2918. $content = preg_replace('/(<\s*img[^>]+src\s*=\s*"cid:)' . $orig . '("[^>]*>)/is','${1}'.$rep.'${2}' ,$content);
  2919. }
  2920. }
  2921. return $content ;
  2922. }
  2923. function attachDirect( $attach_name , $data , $add = false , $mime_type = null , $content_id = null , $target_charset = null , $charset_org = null){
  2924. $_att=array();
  2925. $_att[0]['DIRECT'] = true;
  2926. $_att[0]['DATA'] = $data;
  2927. $_att[0]['PATH'] = null;
  2928. $_att[0]['NAME'] = $attach_name ;
  2929. $_att[0]['MIME_TYPE'] = $mime_type ;
  2930. $_att[0]['CONTENT-ID'] = $content_id ;
  2931. $_att[0]['_CHARSET'] = $target_charset;
  2932. $_att[0]['_ORG_CHARSET'] = $charset_org;
  2933. if( $add ){
  2934. $this->attach = array_merge( $this->attach , $_att );
  2935. }else{
  2936. $this->attach = $_att ;
  2937. }
  2938. return $this->errorGather() ;
  2939. }
  2940. function attachPathFix( $path_filename ){
  2941. $temp = substr($path_filename,0,1);
  2942. if( '/' != $temp && '\\' != $temp ){
  2943. return $this->attach_path . $path_filename;
  2944. }
  2945. return $path_filename;
  2946. }
  2947. //---------------------------------------
  2948. //
  2949. // Inteligent Multibyte Wordwrap
  2950. //
  2951. //---------------------------------------
  2952. function mbWordwrap( $word , $length ){
  2953. if( !is_numeric( $length ) ){
  2954. $length = $this->wordwrap_length;
  2955. }
  2956. if( 1 > $length ){
  2957. $this->error[]='Wordwrap length illegal , need more than 1 line->'.__LINE__;
  2958. }
  2959. $ret = array();
  2960. list( $word , $LFC ) = $this->clean( $word );
  2961. $lines = explode( $LFC , $word ) ;
  2962. foreach($lines as $line){
  2963. $ret []= $this->mbWordwrapLine( $line , $length );
  2964. }
  2965. return implode( $this->LFC , $ret );
  2966. }
  2967. function mbWordwrapLine( $line , $length ){
  2968. $skip = false;
  2969. if( 0 != count( $this->wrap_except ) ){
  2970. foreach($this->wrap_except as $word => $begin_flag ){
  2971. $fg = strpos( $line , $word );
  2972. if( ( ( 0 === $fg ) && $begin_flag) || ( ( false !== $fg ) && !$begin_flag) ){
  2973. $skip = true;
  2974. }
  2975. }
  2976. }
  2977. $enc = $this->qd_detect_encoding( $line );
  2978. $len = mb_strlen( $line , $enc );
  2979. if ( ( $len <= $length ) || $skip ) {
  2980. return $line;
  2981. }
  2982. if( $this->wrap_width ){
  2983. $method = 'widthSubStr';
  2984. }else{
  2985. $method = 'defMbSubStr';
  2986. }
  2987. $ret = array();
  2988. $ln = $length;
  2989. $j = 0;
  2990. for( $i=0; $i < $len ; $i += $ln ){
  2991. list( $r , $ln ) = $this->{$method}( $line , $i , $length , $enc );
  2992. if( 0 !== $j ){
  2993. list( $r , $no_top , $flag )=$this->mbProhibitTop( $r , $enc );
  2994. if( $flag ){
  2995. $ret[$j-1] .= $no_top ;
  2996. $i += mb_strlen( $no_top , $enc );
  2997. list( $r , $ln ) = $this->{$method}( $line , $i , $length , $enc );
  2998. }
  2999. }
  3000. if( ( $i + $ln ) < $len ){
  3001. list( $_r , $ret_count , $flag )=$this->mbProhibitEnd( $r , $enc );
  3002. if( $flag && ( $ret_count < ($length-1) ) ){
  3003. $i -= $ret_count;
  3004. $r = $_r;
  3005. }
  3006. }
  3007. $ret [$j++]= $r ;
  3008. }
  3009. return implode( $this->LFC , $ret ) ;
  3010. }
  3011. function defMbSubStr( $line , $start , $length , $enc ){
  3012. return array( mb_substr( $line , $start , $length , $enc ) ,$length );
  3013. }
  3014. function widthSubStr( $line , $start , $length , $enc ){
  3015. $ret = array();
  3016. $max = mb_strlen( $line , $enc ) ;
  3017. $target = mb_substr( $line , $start , $length , $enc ) ;
  3018. $point = $start + $length;
  3019. // mb_strwidth's lengh means ascii width
  3020. while( ( mb_strwidth( $target , $enc ) <= ( $length-1 ) * $this->mb_strwidth_magni ) && ( $point < $max ) ){
  3021. $target .= mb_substr( $line , $point++ , 1 , $enc ) ;
  3022. }
  3023. return array( $target , mb_strlen( $target , $enc ) ) ;
  3024. }
  3025. function mbProhibitTop( $line , $enc ){
  3026. $flag = false;
  3027. $ret = null;
  3028. $len = mb_strlen( $line , $enc );
  3029. $count = 0 ;
  3030. do{
  3031. $top = mb_substr( $line , $count++ , 1 , $enc );
  3032. }while( isset($this->wrap_prohibit[$top]) && $this->wrap_prohibit[$top] && ( abs($count) < $len ) );
  3033. -- $count ;
  3034. if( 0 < $count ){
  3035. $ret = mb_substr( $line , 0 , $count , $enc );
  3036. $line = mb_substr( $line , $count , $len - $count , $enc );
  3037. $flag = true;
  3038. }
  3039. return array( $line , $ret , $flag );
  3040. }
  3041. function mbProhibitEnd( $line , $enc ){
  3042. $flag = false;
  3043. $len = mb_strlen( $line , $enc );
  3044. $count = 0 ;
  3045. do{
  3046. $end = mb_substr( $line , --$count , 1 , $enc );
  3047. }while( isset($this->wrap_prohibit[$end]) && !$this->wrap_prohibit[$end] && ( abs($count) < $len ) );
  3048. $count = abs( ++$count );
  3049. if( 0 < $count ){
  3050. $line = mb_substr( $line , 0 , $len - $count , $enc );
  3051. $flag = true;
  3052. }
  3053. return array( $line , $count , $flag );
  3054. }
  3055. //------------------------
  3056. // utility
  3057. //------------------------
  3058. function issetContent( $array ){
  3059. if( !isset( $array ) ){
  3060. return false ;
  3061. }
  3062. if( isset( $array['CONTENT'] ) ){
  3063. return true ;
  3064. }
  3065. if( isset( $array ) && is_string( $array ) ){
  3066. return true ;
  3067. }
  3068. return false ;
  3069. }
  3070. function keyUpper( $array ){
  3071. $up_array = array_change_key_case( $array , CASE_UPPER );
  3072. $link = $this->qdmail_array_combine( array_keys( $up_array ) , array_keys( $array ));
  3073. return array( $up_array , $link );
  3074. }
  3075. function qdmail_array_combine( $keys , $values ){//for php4
  3076. if( !is_array( $keys ) || !is_array( $values ) ){
  3077. $this->error[]='array_conbine needs array line =>'.__LINE__;
  3078. }
  3079. $ret = array();
  3080. reset( $values );
  3081. foreach( $keys as $key ){
  3082. $ret[$key] = array_shift( $values ) ;
  3083. }
  3084. return $ret;
  3085. }
  3086. function qdmail_md( $col = null , $start = 33 , $end = 126 ){
  3087. if( is_null( $col ) ){
  3088. $col = $this->boundary_base_degit ;
  3089. }
  3090. $ret = null;
  3091. for( $i = 0 ; $i < $col ; $i++){
  3092. $ret .= chr( mt_rand( $start , $end ) ) ;
  3093. }
  3094. return $ret;
  3095. }
  3096. function clean( $content ){
  3097. if($this->smtp ){
  3098. $LFC = $this->LFC;
  3099. }else{
  3100. $LFC = chr(10);
  3101. }
  3102. return array(rtrim( preg_replace( '/\r?\n/' , $LFC , $content ) ),$LFC);
  3103. }
  3104. function quotedPrintableEncode( $word ){
  3105. if(empty($word)){
  3106. return $word;
  3107. }
  3108. $lines = preg_split("/\r?\n/", $word);
  3109. $out = array() ;
  3110. foreach ($lines as $line){
  3111. $one_line = null ;
  3112. for ($i = 0; $i <= strlen($line) - 1; $i++){
  3113. $char = substr ( $line, $i, 1 );
  3114. $ascii = ord ( $char );
  3115. if ( (32 > $ascii) || (61 == $ascii) || (126 < $ascii) ){
  3116. $char = '=' . strtoupper ( dechex( $ascii ) );
  3117. }
  3118. if ( ( strlen ( $one_line ) + strlen ( $char ) ) >= 76 ){
  3119. $out[]= $one_line . '=' ;
  3120. $one_line = null ;
  3121. }
  3122. $one_line .= $char;
  3123. }
  3124. $out[]= $one_line ;
  3125. }
  3126. return implode( $this->LFC , $out );
  3127. }
  3128. function time(){
  3129. list($start_usec, $start_sec) = explode(' ', $this->start_time );
  3130. list($end_usec, $end_sec) = explode(' ', microtime() );
  3131. return ($end_sec - $start_sec) + (float) ($end_usec - $start_usec);
  3132. }
  3133. //------------------
  3134. // error
  3135. //------------------
  3136. function errorStatment( $type = false , $lf = null ){
  3137. if( $type ){
  3138. return $this->errorRender( $this->error_stack , $lf , false );
  3139. }else{
  3140. return $this->error_stack;
  3141. }
  3142. }
  3143. function errorRender( $error = null , $lf = null , $display = true ){
  3144. if( is_null( $error ) ){
  3145. $error = $this->error;
  3146. }
  3147. if( is_null( $lf ) ){
  3148. $lf = $this->log_LFC ;
  3149. }
  3150. if( !is_array( $error ) ){
  3151. $error = array( $error );
  3152. }
  3153. $out = null ;
  3154. foreach($error as $mes){
  3155. $out .= $this->name . ' error: ' . trim( $mes ) . $lf ;
  3156. }
  3157. if( $this->error_display && $display ){
  3158. $_out = str_replace( $lf ,'<br>' . $lf , $out );
  3159. echo $_out ;
  3160. }
  3161. return $out ;
  3162. }
  3163. function errorGather( $message = null , $line = null){
  3164. if( !is_null( $message ) ){
  3165. if( !is_null( $line ) ){
  3166. $message .= ' line -> '.$line;
  3167. }
  3168. if(0 === count( $this->error_stack )){
  3169. $this->error[] = 'Qdmail Version '.$this->version.' ,PHP Version '.phpversion();
  3170. $this->error[] = $this->iniGather();
  3171. }
  3172. $this->error[] = $message ;
  3173. }elseif( 0 === count( $this->error )){
  3174. return true;
  3175. }
  3176. $er = $this->errorRender();
  3177. $this->error_stack = array_merge( $this->error_stack , $this->error );
  3178. $this->error = array();
  3179. if( !$this->logWrite( 'error' , $er ) ){
  3180. $this->error_stack = array_merge( $this->error_stack , $this->error );
  3181. }
  3182. return false;
  3183. }
  3184. function log( $mes = null ){
  3185. if( is_null( $mes )){
  3186. $addrs = $this->done() ;
  3187. // $this->done = array();
  3188. $spacer = null;
  3189. if( 0 != count( $addrs ) ){
  3190. $mes .= 'Send Success: '.implode(' ',$addrs) ;
  3191. $spacer = $this->log_LFC ;
  3192. }
  3193. $addrs = $this->undone() ;
  3194. // $this->undone = array();
  3195. if( 0 != count( $addrs ) ){
  3196. $mes .= $spacer . 'Send failure: '.implode(' ',$addrs);
  3197. }
  3198. }
  3199. return $this->logWrite( null , trim( $mes ) ) ;
  3200. }
  3201. function logWrite( $type , $message ){
  3202. $tp = ('error' == $type) ? false:true;
  3203. $level = $tp ? $this->log_level:$this->errorlog_level;
  3204. $fg_debug = ( 2 > $this->debug ) && !$this->render_mode;
  3205. if( 0 == $level || !$fg_debug){
  3206. return true;
  3207. }
  3208. $filename = $tp ? $this->log_filename:$this->errorlog_filename;
  3209. $path = $tp ? $this->log_path:$this->errorlog_path;
  3210. $ap = $tp ? $this->log_append:$this->errorlog_append;
  3211. $fp = fopen( $path.$filename , $ap );
  3212. if( !is_resource( $fp ) ){
  3213. $this->error[]='file open error at logWrite() line->'.__LINE__;
  3214. return false;
  3215. }
  3216. $spacer = $tp ? ' ' : $this->log_LFC ;
  3217. fwrite( $fp ,
  3218. date( $this->log_dateformat )
  3219. . $spacer
  3220. . trim( $message )
  3221. . $this->log_LFC
  3222. );
  3223. if( $level > 1 ){
  3224. fwrite( $fp , trim( $this->header_for_smtp ) . $this->log_LFC );
  3225. }elseif( $level > 2 ){
  3226. fwrite( $fp , $this->log_LFC . $this->content_for_mailfunction . $this->log_LFC );
  3227. }
  3228. fclose( $fp ) ;
  3229. return true ;
  3230. }
  3231. function errorSpecify( $func , $line , $add_message = null){
  3232. return $this->errorGather($add_message.'User Specify Error in Method of \''.$func.'\'' , $line ) ;
  3233. }
  3234. //-------------------------------------------
  3235. // MIME Content-type def
  3236. //-------------------------------------------
  3237. var $attach_ctype=array(
  3238. 'txt'=>'text/plain',
  3239. 'csv'=>'text/csv',
  3240. 'xml'=>'text/xml',
  3241. 'htm'=>'text/html',
  3242. 'html'=>'text/html',
  3243. 'gif'=>'image/gif',
  3244. 'jpg'=>'image/jpeg',
  3245. 'jpeg'=>'image/jpeg',
  3246. 'png'=>'image/png',
  3247. 'tif'=>'image/tiff',
  3248. 'tiff'=>'image/tiff',
  3249. 'bmp'=>'image/x-bmp',
  3250. 'ps'=>'appilcation/postscript',
  3251. 'eps'=>'appilcation/postscript',
  3252. 'epsf'=>'appilcation/postscript',
  3253. 'ai'=>'application/postscript',
  3254. 'zip'=>'application/zip',
  3255. 'lzh'=>'application/x-lzh',
  3256. 'lha'=>'application/octet-stream',
  3257. 'tar'=>'application/x-tar',
  3258. 'gzip'=>'application/x-tar',
  3259. 'cpt'=>'application/mac-compactpro',
  3260. 'doc'=>'application/msword',
  3261. 'xls'=>'application/vnd.ms-excel',
  3262. 'ppt'=>'application/vnd.ms-powerpoint',
  3263. 'rtf'=>'application/rtf',
  3264. 'pdf'=>'application/pdf',
  3265. 'css'=>'application/css',
  3266. 'au'=>'audio/basic',
  3267. 'rpm'=>'audio/x-pn-realaudio-plugin',
  3268. 'swa'=>'application/x-director',
  3269. 'mp3'=>'audio/mpeg',
  3270. 'mp4'=>'audio/mp4',
  3271. 'wav'=>'audio/x-wav',
  3272. 'midi'=>'audio/midi',
  3273. 'avi'=>'vide/x-msvideo',
  3274. 'mpeg'=>'video/mpeg',
  3275. 'mpg'=>'video/mpeg',
  3276. 'wmv'=>'video/x-ms-wmv',
  3277. 'flash'=>'application/x-shockwave-flash',
  3278. 'mmf'=>'application/x-smaf ', //softbank chakumero
  3279. 'smaf'=>'application/x-smaf', //softbank chakumero
  3280. 'hdml'=>'text/x-hdml', // HDML au,docomo
  3281. '3gpp2'=>'video/3gpp2', // au chaku-uta,ez-movie
  3282. '3g2'=>'video/3gpp2', // au chaku-uta,ez-movie
  3283. 'amc'=>'video/3gpp2', // au chaku-uta,ez-movie
  3284. 'kjx'=>'application/x-kjx', // au ez-apri
  3285. '3gpp'=>'video/3gpp', // docomo chaku-uta,movie
  3286. '3gp'=>'video/3gpp', // docomo chaku-uta,movie
  3287. 'amr'=>'video/3gpp', // docomo chaku-uta,movie
  3288. 'asf'=>'video/3gpp', // docomo chaku-uta,movie
  3289. 'jam'=>'application/x-jam', // docomo i-apri
  3290. 'jar'=>'application/java-archive', // java apri
  3291. 'jad'=>'text/vnd.sun.j2me.app-descriptor', // java apri
  3292. 'exe'=>'application/octet-stream',
  3293. 'khm'=>'application/x-kddi-htmlmail',// au decoration mail template
  3294. 'dmt'=>'application/x-decomail-template',// nttdocomo decoration mail template
  3295. 'hmt'=>'application/x-htmlmail-template',// softbank decoration mail template
  3296. 'hqx'=>'application/mac-binhex40',
  3297. 'cpt'=>'application/mac-compactpro',
  3298. 'php'=>'application/x-httpd-php',
  3299. 'php4'=>'application/x-httpd-php',
  3300. 'php3'=>'application/x-httpd-php',
  3301. 'phtml'=>'application/x-httpd-php',
  3302. 'phps'=>'application/x-httpd-php-source',
  3303. 'js'=>'application/x-javascript',
  3304. 'swf'=>'application/x-shockwave-flash',
  3305. 'eml'=>'message/rfc822',
  3306. );
  3307. //-------------------------------
  3308. // Debug
  3309. //-------------------------------
  3310. function iniGather(){
  3311. $ret = 'OS '.PHP_OS.' ; PHP Version '.PHP_VERSION.' ; '.$this->name.' version '.$this->version;
  3312. $ret .= $this->LFC.'php.ini status: mb_language = '.mb_language()
  3313. .' ; mb_internal_encoding = '.mb_internal_encoding()
  3314. .' ; mb_detect_order = '.implode(',',mb_detect_order());
  3315. $ret .= $this->LFC . $this->name .' Status debug: '.$this->debug().', log: '.$this->logLevel().', errorlog: '.$this->errorlogLevel();
  3316. return $ret;
  3317. }
  3318. function debug( $level=null ){
  3319. if( is_null( $level ) || !is_numeric($level) ){
  3320. return $this->debug;
  3321. }
  3322. $this->debug = $level ;
  3323. return true;
  3324. }
  3325. function debugEchoLine(){
  3326. $vars = func_get_args();
  3327. $this->debugEcho( false , $vars );
  3328. }
  3329. function debugEchoLf(){
  3330. $vars = func_get_args();
  3331. $this->debugEcho( true , $vars );
  3332. }
  3333. function debugEcho( $lf , $vars = null ){
  3334. static $already_header = false;
  3335. static $already_footer = false;
  3336. if( 1 > $this->debug ){
  3337. return;
  3338. }
  3339. if( true === $this->debug_echo_charset ){
  3340. $this->debug_echo_charset = $this->charset_content ;
  3341. }
  3342. if( !$already_header ){
  3343. $head='<html><head><meta http-equiv="content-type" content="text/html; charset='.$this->debug_echo_charset.'"></head><body>';
  3344. echo $head ;
  3345. echo '<pre>'.$this->name . ' Debug: '.$this->iniGather().'</pre>';
  3346. $already_header = true ;
  3347. }
  3348. if( $already_header && ( 'END' === $lf ) && !$already_footer){
  3349. $foot ='</body></html>';
  3350. echo $foot;
  3351. $already_footer = true;
  3352. return ;
  3353. }
  3354. $out = null;
  3355. if( !is_array( $vars ) ){
  3356. $vars =array( $vars );
  3357. }
  3358. foreach($vars as $var){
  3359. $_out = print_r( $var , true ) ;
  3360. $enc = $this->qd_detect_encoding( $_out );
  3361. if( ( strtoupper( $this->qdmail_system_charset ) !== strtoupper( $enc ) ) && ('ASCII'!==strtoupper( $enc ))){
  3362. $_out = $this->qd_convert_encoding( $_out , $this->qdmail_system_charset , $enc );
  3363. }
  3364. $out .= $_out . $this->LFC;
  3365. }
  3366. $spacer = $this->log_LFC ;
  3367. if( !$lf ){
  3368. $out = preg_replace("/\\r?\\n/",' ',$out);
  3369. $spacer = null ;
  3370. }
  3371. echo "<pre>";
  3372. $out = $this->name . ' Debug: ' . $spacer . trim( $out );
  3373. $out = htmlspecialchars( $out , ENT_QUOTES , $this->qdmail_system_charset);
  3374. $out = $this->qd_convert_encoding($out,$this->debug_echo_charset,$this->qdmail_system_charset);
  3375. echo $out;
  3376. echo "</pre>";
  3377. }
  3378. function debugReport( $var = null ){
  3379. if( is_null( $var ) ){
  3380. return $this->debug_report;
  3381. }
  3382. if( is_bool( $var ) ){
  3383. $this->debug_report = $var;
  3384. return true;
  3385. }
  3386. if( 'FILE' !== $var ){
  3387. return $this->errorSpecify(__FUNCTION__,__LINE__);
  3388. }
  3389. $fg= true;
  3390. $cont = print_r( $this , true );
  3391. $cont .= print_r( $_SERVER , true);
  3392. $date = date("Y_m_d_H_i_s");
  3393. $out = <<<EOF
  3394. Debug Report
  3395. date: {$date}
  3396. name: {$this->name}
  3397. version: {$this->version}
  3398. {$cont}
  3399. EOF;
  3400. $filename = $this->debug_report_path . $this->name.'_debug_report_'.date("Y_m_d_H_i_s") . '.txt';
  3401. if($fp = fopen( $filename , 'w' )){
  3402. fwrite( $fp , $out );
  3403. fclose( $fp );
  3404. }else{
  3405. $this->error[] = 'Can not open file \'' . $filename . '\' line-> ' . __LINE__ ;
  3406. $fg = false;
  3407. }
  3408. return $fg;
  3409. }
  3410. //--
  3411. // this path like this, /home/foo/bar/ or c\:htdocs\foo\bar\ or ./foo/bar/
  3412. // do not forget the last '/' or '\'
  3413. //--
  3414. function debugReportPath( $path = null ){
  3415. if( is_null( $path ) ){
  3416. return $path;
  3417. }
  3418. if( empty( $path ) ){
  3419. $this->debug_report_path = './';
  3420. return true;
  3421. }
  3422. if( is_string( $path ) ){
  3423. $this->debug_report_path = $path;
  3424. return true ;
  3425. }
  3426. return $this->errorSpecify(__FUNCTION__,__LINE__);
  3427. }
  3428. function sendBySmtp( $obj = null ){
  3429. if( !is_null( $obj ) ){
  3430. $this->smtp_object = $obj;
  3431. }
  3432. if( !isset( $this->smtp_object ) || !is_object( $this->smtp_object ) ){
  3433. if( false === ( $this->smtp_object = $this->smtpObject() ) ){
  3434. return $this->errorGather('SMTP Object make \'new\' error',__LINE__);
  3435. }
  3436. }
  3437. $this->smtp_server = array_change_key_case( $this->smtp_server , CASE_UPPER );
  3438. if( !isset( $this->smtp_server['HOST'] ) ){
  3439. $this->smtp_server = array_merge( $this->smtp_server , $this->smtp_object->server() );
  3440. }
  3441. if( !isset( $this->smtp_server['HOST'] ) ){
  3442. return $this->errorGather('No exist SMTP\'s Settings',__LINE__);
  3443. }
  3444. $this->smtp_server['CONTINUE'] = true;
  3445. if( !$this->smtp_object -> server( $this->smtp_server )){
  3446. return $this->errorGather('SMTP Object initialize error',__LINE__);
  3447. }
  3448. if( $this->smtp_loglevel_link ){
  3449. $this->smtp_object -> logLevel( $this->log_level );
  3450. $this->smtp_object -> errorlogLevel( $this->errorlog_level );
  3451. }
  3452. $this->smtp_object -> to( $this->recipient );
  3453. $this->smtp_object -> data( $this->content_all_for_smtp );
  3454. return $this -> smtp_object -> send();
  3455. }
  3456. function sendBySendmail(){
  3457. $temp_name = tempnam ( $this->temporary_path , 'qdmail' );
  3458. if(false===$temp_name){
  3459. return $this->errorGather('Can not make Temporary File ',__LINE__);
  3460. }
  3461. $fp = fopen($temp_name,'w');
  3462. if(false===$temp_name){
  3463. return $this->errorGather('Can not open Temporary File ',__LINE__);
  3464. }
  3465. fputs($fp,$this->content_all_for_smtp);
  3466. fclose($fp);
  3467. $recipient = implode(' ',$this->recipient);
  3468. $sendfg = exec($this->sendmail_path . ' '.$this->mtaOption().' '.$recipient.' < '.$temp_name,$ret);
  3469. $fg = unlink($temp_name);
  3470. if(false===$fg){
  3471. return $this->errorGather('Can not dellete Temporary File ',__LINE__);
  3472. }
  3473. return (false!==$sendfg && empty($sendfg)) || true === $sendfg;
  3474. }
  3475. //------------------------------------------
  3476. // expecting Override on the other FrameWork
  3477. //------------------------------------------
  3478. function & smtpObject( $null = false ){
  3479. if(is_null($null)){
  3480. $this->smtp_object = null;
  3481. return true;
  3482. }
  3483. if( isset( $this->smtp_object ) && is_object( $this->smtp_object ) ){
  3484. return $this->smtp_object;
  3485. }
  3486. if( !class_exists ( 'Qdsmtp' ) && file_exists( 'qdsmtp.php' ) ){
  3487. require_once( 'qdsmtp.php' );
  3488. }elseif( !class_exists ( 'Qdsmtp' ) && !file_exists( 'qdsmtp.php' )){
  3489. return $this->errorGather('Plese load SMTP Program - Qdsmtp http://hal456.net/qdsmtp',__LINE__);
  3490. }
  3491. $this->smtp_object = new Qdsmtp();
  3492. return $this->smtp_object;
  3493. }
  3494. function setSmtpObject( & $obj ){
  3495. if(is_object($obj)){
  3496. $this->smtp_object = & $obj;
  3497. return true;
  3498. }else{
  3499. return false;
  3500. }
  3501. }
  3502. }//the QdmailBase
  3503. class QdmailUserFunc extends QdmailBase{
  3504. function __construct( $param = null ){
  3505. parent::__construct( $param );
  3506. }
  3507. function validateAddr( $addr ){
  3508. if(0==preg_match( $this->varidate_address_regex , $addr , $match )){
  3509. $this->errorGather('Tyr Varidate Error by regex preg_match(\''.$this->varidate_address_regex . '\') the address is ->'.$addr,__LINE__);
  3510. }else{
  3511. return true;
  3512. }
  3513. }
  3514. function stripCrlf( $word ){
  3515. if( $this->force_change_charset ){
  3516. $enc = $this->qd_detect_encoding( $word ) ;
  3517. $word = $this->qd_convert_encoding( $word , $this->qdmail_system_charset , $enc );
  3518. }
  3519. $word = preg_replace( '/\r?\n/i' , '' , $word );
  3520. if( $this->force_change_charset ){
  3521. $word = $this->qd_convert_encoding( $word , $enc , $this->qdmail_system_charset );
  3522. }
  3523. return $word ;
  3524. }
  3525. }
  3526. class Qdmail extends QdmailUserFunc{
  3527. var $name ='Qdmail';
  3528. function Qdmail( $param = null ){
  3529. if( !is_null($param)){
  3530. $param = func_get_args();
  3531. }
  3532. parent::__construct( $param );
  3533. }
  3534. }
  3535. //-------------------------------------------
  3536. // CakePHP Component
  3537. //-------------------------------------------
  3538. class QdmailComponent extends QdmailUserFunc{
  3539. var $framework = 'CakePHP';
  3540. var $view_dir = 'email';
  3541. var $layout_dir = 'email';
  3542. var $layout = 'default';
  3543. var $template = 'default';
  3544. var $view = null;
  3545. function QdmailComponent( $param = null ){
  3546. if( !is_null($param)){
  3547. $param = func_get_args();
  3548. }
  3549. parent::__construct( $param );
  3550. }
  3551. function startup(&$controller) {
  3552. $this->Controller =& $controller;
  3553. if( defined( 'COMPONENTS' ) ){
  3554. $this->logPath(COMPONENTS);
  3555. $this->errorlogPath(COMPONENTS);
  3556. }
  3557. return;
  3558. }
  3559. //----------------------------
  3560. // Override Parent Method
  3561. //----------------------------
  3562. function & smtpObject(){
  3563. if( isset( $this->Qdsmtp ) && is_object( $this->Qdsmtp ) ){
  3564. return $this->Qdsmtp;
  3565. }
  3566. if( !class_exists ( 'QdsmtpComponent' ) ){
  3567. if( !$this->import( 'Component' , 'Qdsmtp' ) ){
  3568. return $this->errorGather('Qdmail<->CakePHP Component Load Error , the name is Qdsmtp',__LINE__);
  3569. }
  3570. }
  3571. $this->Qdsmtp = new QdsmtpComponent();
  3572. if( !is_object( $this->Qdsmtp ) ){
  3573. return $this->errorGather('Qdmail<->CakePHP Component making Instance Error , the name is QdsmtpComponent',__LINE__);
  3574. }
  3575. $this->Qdsmtp -> startup( $this->Controller );
  3576. return $this->Qdsmtp;
  3577. }
  3578. //----------------------------
  3579. // Cake Interface
  3580. //----------------------------
  3581. function import( $kind , $name ){
  3582. if( 1.2 > (float) substr(Configure::version(),0,3) ){
  3583. $function_name = 'load' . $kind ;
  3584. if( function_exists( $function_name ) ){
  3585. return $function_name( $name ) ;
  3586. }else{
  3587. return $this->errorGather('Qdmail<->CakePHP ' .$kind .' Load Error , the name is \'' . $name . '\'',__LINE__);
  3588. }
  3589. }else{
  3590. return App::import( $kind , $name ) ;
  3591. }
  3592. }
  3593. function cakeText( $content , $template = null , $layout = null , $org_charset = null , $target_charset = null , $enc = null , $wordwrap_length = null ){
  3594. $this->template = is_null( $template ) ? $this->template : $template ;
  3595. $this->layout = is_null( $layout ) ? $this->layout : $layout ;
  3596. list( $cont , $target_charset , $org_charset ) = $this->cakeRender( $content , 'TEXT' , $org_charset = null , $target_charset );
  3597. return $this->text( $cont , $wordwrap_length , $target_charset , $enc , $org_charset );
  3598. }
  3599. function cakeHtml( $content , $template = null , $layout = null , $org_charset = null , $target_charset = null , $enc = null ){
  3600. $this->template = is_null( $template ) ? $this->template : $template ;
  3601. $this->layout = is_null( $layout ) ? $this->layout : $layout ;
  3602. list( $cont , $target_charset , $org_charset ) = $this->cakeRender( $content , 'HTML' , $org_charset = null , $target_charset );
  3603. return $this->html( $cont , null , $target_charset , $enc , $org_charset );
  3604. }
  3605. function cakeRender( $content , $type , $org_charset = null , $target_charset = null){
  3606. if( is_null( $target_charset ) ){
  3607. $target_charset = $this->charset_content;
  3608. }
  3609. if( !class_exists ( $this->Controller->view ) ){
  3610. if( !$this->import( 'View' , $this->view ) ){
  3611. return $this->errorGather('Qdmail<->CakePHP View Load Error , the name is \''.$this->view.'\'',__LINE__);
  3612. }
  3613. }
  3614. $type = strtolower( $type );
  3615. $view = new $this->Controller->view( $this->Controller , false );
  3616. $view->layout = $this->layout;
  3617. $mess = null;
  3618. $content = $view->renderElement( $this->view_dir . DS . $type . DS . $this->template , array('content' => $content ) , true );
  3619. if( 1.2 > (float) substr(Configure::version(),0,3) ){
  3620. $view->subDir = $this->layout_dir . DS . $type . DS ;
  3621. }else{
  3622. $view->layoutPath = $this->layout_dir . DS . $type;
  3623. }
  3624. $mess .= $view->renderLayout( $content ) ;
  3625. if( is_null( $org_charset ) ){
  3626. $org_charset = $this->qd_detect_encoding( $mess );
  3627. }
  3628. $mess = $this->qd_convert_encoding( $mess , $target_charset , $org_charset );
  3629. return array( $mess , $target_charset , $org_charset );
  3630. }
  3631. function CakePHP( $param ){
  3632. $param = array_change_key_case( $param , CASE_LOWER );
  3633. extract($param);
  3634. if( isset($type) || 'HTML' == $type ){
  3635. $type ='cakeHtml';
  3636. }else{
  3637. $type = 'cakeText';
  3638. }
  3639. return $this->{$type}( isset($content) ? $content:null, isset($template) ? $template:null , isset($layout) ? $layout:null , isset($org_charset) ? $org_charset: null , isset($target_charset) ? $target_charset:null , isset($enc) ? $enc:null , isset($wordwrap_length) ? $wordwrap_length:null );
  3640. }
  3641. }
  3642. //-------------------------------------------
  3643. // Symfony Addon
  3644. //-------------------------------------------
  3645. class sfQdmail extends QdmailUserFunc{
  3646. var $framework = 'Symfony';
  3647. function __construct( $param = null ){
  3648. if( !is_null($param)){
  3649. $param = func_get_args();
  3650. }
  3651. parent::__construct( $param );
  3652. }
  3653. function setBody( $body ){
  3654. if('HTML'===$this->is_html){
  3655. $this->html( $body );
  3656. }else{
  3657. $this->text( $body );
  3658. }
  3659. }
  3660. function getAltBody(){
  3661. if('HTML'===$this->is_html){
  3662. $content=$this->body();
  3663. return !empty($content['TEXT']['CONTENT']);
  3664. }else{
  3665. return false;
  3666. }
  3667. }
  3668. function setAltBody( $body ){
  3669. $this->text( $body );
  3670. }
  3671. function addStringAttachment($data, $attach_name , $mime_type){
  3672. $this->attachDirect( $attach_name , $data , $add = true , $mime_type );
  3673. }
  3674. function getRawHeader(){
  3675. return $this->header_for_smtp;
  3676. }
  3677. function getRawBody(){
  3678. return $this->$this->content_for_mailfunction;
  3679. }
  3680. function initialize(){
  3681. $this->reset();
  3682. }
  3683. function setCharset($charset){
  3684. $this->charset($charset);
  3685. }
  3686. function getCharset(){
  3687. $ret = $this->charset();
  3688. return $ret['TEXT'];
  3689. }
  3690. function setContentType($content_type){
  3691. if(false===strpos(strtoupper($content_type),'HTML')){
  3692. $this->is_html = 'TEXT';
  3693. }else{
  3694. $this->is_html = 'HTML';
  3695. }
  3696. }
  3697. function getContentType(){
  3698. if('HTML'===$this->is_html){
  3699. return 'text/html';
  3700. }else{
  3701. return 'text/plain';
  3702. }
  3703. }
  3704. function setPriority($priority){
  3705. $pri = array(1=>'high',3=>'normal',5=>'low');
  3706. if(isset($pri[$priority])){
  3707. $this->priority($pri[$priority]);
  3708. return true;
  3709. }else{
  3710. return false;
  3711. }
  3712. }
  3713. function getPriority(){
  3714. $pri = array('HIGH'=>1,'NORMAL'=>3,'LOW'=>5);
  3715. $now_priority = strtoupper($this->priority());
  3716. if(empty($now_priority)){
  3717. return null;
  3718. }
  3719. return $pri[$now_priority];
  3720. }
  3721. function setEncoding($encoding){
  3722. $this->encoding($encoding);
  3723. }
  3724. function getEncoding(){
  3725. return $this->encoding();
  3726. }
  3727. function setSubject($subject){
  3728. $this->subject($subject);
  3729. }
  3730. function getSubject(){
  3731. return $this->subject();
  3732. }
  3733. function getBody(){
  3734. $content=$this->body();
  3735. if('HTML'===$this->is_html){
  3736. return $content['HTML']['CONTENT'];
  3737. }else{
  3738. return $content['TEXT']['CONTENT'];
  3739. }
  3740. }
  3741. function setMailer($type = 'mail', $options = array()){
  3742. switch ($type){
  3743. case 'smtp':
  3744. $this->smtp = true;
  3745. $this->sendmail = false;
  3746. $this->mailer = 'smtp';
  3747. if (isset($options['keep_alive'])){
  3748. $this->keepParameter(true);
  3749. }
  3750. break;
  3751. case 'sendmail':
  3752. $this->sendmail = true;
  3753. $this->smtp = false;
  3754. $this->mailer = 'sendmail';
  3755. break;
  3756. default:
  3757. $this->smtp = false;
  3758. $this->sendmail = false;
  3759. $this->mailer = 'mail';
  3760. break;
  3761. }
  3762. }
  3763. function getMailer(){
  3764. return $this->mailer;
  3765. }
  3766. function setSender($address, $name = null){
  3767. $this->addHeader( 'Return-Path' , $address );
  3768. }
  3769. function getSender(){
  3770. return isset($this->other_header['Return-Path']) ? $this->other_header['Return-Path']:null;
  3771. }
  3772. function setFrom($address, $name = null){
  3773. $this->from( $address , $name );
  3774. }
  3775. function addAddresses($addresses){
  3776. $this->to( $addresses , null , true );
  3777. }
  3778. function addAddress($address, $name = null){
  3779. $this->to( $address , $name , true );
  3780. }
  3781. function addCc($address, $name = null){
  3782. $this->cc( $address , $name , true );
  3783. }
  3784. function addBcc($address, $name = null){
  3785. $this->cc( $address , $name , true );
  3786. }
  3787. function addReplyTo($address, $name = null){
  3788. $this->replyto( $address , $name , true );
  3789. }
  3790. function clearAddresses(){
  3791. $this->to =array();
  3792. }
  3793. function clearCcs(){
  3794. $this->cc =array();
  3795. }
  3796. function clearBccs(){
  3797. $this->bcc =array();
  3798. }
  3799. function clearReplyTos(){
  3800. $this->replyto =array();
  3801. }
  3802. function clearAllRecipients(){
  3803. $this->clearAddresses();
  3804. $this->clearCcs();
  3805. $this->clearBccs();
  3806. $this->clearReplyTos();
  3807. }
  3808. function addAttachment($path, $name = '', $encoding = 'base64', $type = 'application/octet-stream'){
  3809. return $this->attach(array('PATH'=>$path,'NAME'=>$name,'MIME-TYPE'=>$type),true);
  3810. }
  3811. function addEmbeddedImage($path, $cid, $name = '', $encoding = 'base64', $type = 'application/octet-stream'){
  3812. return $this->attach(array('PATH'=>$path,'NAME'=>$name,'MIME-TYPE'=>$type,'CONTENT-ID'=>$cid),true);
  3813. }
  3814. function setAttachments($attachments){}
  3815. function clearAttachments(){
  3816. $this->attach=array();
  3817. }
  3818. function addCustomHeader($name, $value){
  3819. $this->addHeader($name, $value);
  3820. }
  3821. function clearCustomHeaders(){
  3822. $this->other_header=array();
  3823. }
  3824. function prepare(){}
  3825. function smtpClose(){}
  3826. function setDomain($hostname){}
  3827. function getDomain(){}
  3828. function setHostname($hostname){}
  3829. function getHostname(){}
  3830. function setPort($port){}
  3831. function getPort(){}
  3832. function setUsername($username){}
  3833. function getUsername(){}
  3834. function setPassword($password){}
  3835. function getPassword(){}
  3836. function setWordWrap($wordWrap){}
  3837. function getWordWrap(){}
  3838. }
  3839. class QdDeco{
  3840. var $template = null;
  3841. var $data = array();
  3842. function template( $template = null ){
  3843. if(is_null($template)){
  3844. return $this->template;
  3845. }
  3846. $this->template = trim(preg_replace("/\\r?\\n/is","\r\n",$template));
  3847. return true;
  3848. }
  3849. function get( $kind ){
  3850. if(!empty($this->data[$kind])){
  3851. return $this->data[$kind];
  3852. }
  3853. if('ATTACH'===$kind){
  3854. return array();
  3855. }else{
  3856. return null;
  3857. }
  3858. }
  3859. function decode(){
  3860. if(!class_exists('QdmailReceiver')){
  3861. include('qd_mail_receiver.php');
  3862. }
  3863. $num_boundary = strpos(strtolower($this->template),'boundary');
  3864. $num_crlf = strpos($this->template,"\r\n\r\n");
  3865. $template = $this->template;
  3866. while((false!==$num_boundary)&&(false!==$num_crlf)&&($num_boundary > $num_crlf)){
  3867. $template = substr($template,$num_crlf+4);
  3868. $num_crlf = strpos($template,"\r\n\r\n");
  3869. $num_boundary = strpos(strtolower($template),'boundary');
  3870. }
  3871. $receiver = QdmailReceiver::start( 'direct' , $template );
  3872. $this->data['HTML'] = $receiver->bodyAutoSelect() ;
  3873. if(false===$this->data['HTML']){
  3874. $this->data['HTML'] = '';
  3875. }
  3876. $attach = $receiver->attach();
  3877. foreach($attach as $att){
  3878. if(isset($att['content-id'])){
  3879. $cid = rtrim($att['content-id'],'>');
  3880. $cid = ltrim($cid,'<');
  3881. }elseif(isset($att['content_id'])){
  3882. $cid = rtrim($att['content_id'],'>');
  3883. $cid = ltrim($cid,'<');
  3884. }else{
  3885. $cid = null;
  3886. }
  3887. $this->data['ATTACH'][]=array(
  3888. 'PATH' => $att['filename_safe'],
  3889. 'NAME' => $att['filename_safe'],
  3890. 'CONTENT-TYPE' => $att['mimetype'],
  3891. 'CONTENT-ID' => $cid,
  3892. '_CHARSET' => null,
  3893. '_ORG_CHARSET' => null,
  3894. 'DIRECT' => $att['value'],
  3895. 'BARE' => false,
  3896. );
  3897. }
  3898. return true;
  3899. }
  3900. }?>