PageRenderTime 35ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/b2b/core/model/system/mdl.email.php

http://phpfor.googlecode.com/
PHP | 244 lines | 167 code | 29 blank | 48 comment | 22 complexity | ac4e9d387cc4ad20d6cedf65b04702de MD5 | raw file
  1. <?PHP
  2. class mdl_email{
  3. var $hasTitle = true; //?????
  4. var $maxtime = 300; //?????? ,??:?
  5. var $maxbodylength =300; //????
  6. var $allowMultiTarget=false; //???????
  7. var $targetSplit = ',';
  8. var $Sendmail = "/usr/sbin/sendmail";
  9. function ready($config){
  10. $system = &$GLOBALS['system'];
  11. $this->smtp = &$system->loadModel('utility/smtp');
  12. if($config['sendway']=='smtp'){
  13. if(!$this->SmtpConnect($config)) return false;
  14. }
  15. return true;
  16. }
  17. /**
  18. * finish
  19. * ????????????
  20. *
  21. * @param mixed $config
  22. * @access public
  23. * @return void
  24. */
  25. function finish($config){
  26. if($config['sendway']=='smtp'){
  27. $this->SmtpClose();
  28. }
  29. }
  30. /**
  31. * send
  32. * ????,?????
  33. *
  34. * config???getOptions???????????
  35. *
  36. * @param mixed $to
  37. * @param mixed $message
  38. * @param mixed $config
  39. * @access public
  40. * @return void
  41. */
  42. function send($to, $subject, $body, $config){
  43. $system = &$GLOBALS['system'];
  44. $this->Sender = $config['usermail'];
  45. $this->Subject = $this->inlineCode($subject);
  46. $header = array(
  47. 'Return-path'=>'<'.$config['usermail'].'>',
  48. 'Date'=>date('r'),
  49. 'From'=>$this->inlineCode($system->getConf('system.shopname')).'<'.$config['usermail'].'>',
  50. 'MIME-Version'=>'1.0',
  51. 'Subject'=>$this->Subject,
  52. 'To'=>$to,
  53. 'Content-Type'=>'text/html; charset=UTF-8; format=flowed',
  54. 'Content-Transfer-Encoding'=>'base64'
  55. );
  56. $body = chunk_split(base64_encode($body));
  57. $header = $this->buildHeader($header);
  58. $config['sendway']=($config['sendway'])?$config['sendway']:'smtp';
  59. switch($config['sendway'])
  60. {
  61. case "sendmail":
  62. $result = $this->SendmailSend($to,$header, $body);
  63. break;
  64. case "mail":
  65. $result = $this->MailSend($to,$header, $body);
  66. break;
  67. case "smtp":
  68. $result = $this->SmtpSend($to,$header, $body,$config);
  69. break;
  70. default:
  71. trigger_error('mailer_not_supported',E_ERROR);
  72. $result = false;
  73. break;
  74. }
  75. return $result;
  76. }
  77. function inlineCode($str){
  78. $str = trim($str);
  79. return $str?'=?UTF-8?B?'.base64_encode($str).'?= ':'';
  80. }
  81. function buildHeader($headers){
  82. $ret = '';
  83. foreach($headers as $k=>$v){
  84. $ret.=$k.': '.$v."\n";
  85. }
  86. return $ret;
  87. }
  88. /**
  89. * Sends mail using the $Sendmail program.
  90. * @access private
  91. * @return bool
  92. */
  93. function SendmailSend($header, $body) {
  94. if ($this->Sender != "")
  95. $sendmail = sprintf("%s -oi -f %s -t", $this->Sendmail, $this->Sender);
  96. else
  97. $sendmail = sprintf("%s -oi -t", $this->Sendmail);
  98. if(!@$mail = popen($sendmail, "w"))
  99. {
  100. $this->__maillog();
  101. return false;
  102. }
  103. fputs($mail, $header);
  104. fputs($mail, $body);
  105. $result = pclose($mail) >> 8 & 0xFF;
  106. if($result != 0)
  107. {
  108. $this->__maillog();
  109. return false;
  110. }
  111. return true;
  112. }
  113. /**
  114. * Sends mail using the PHP mail() function.
  115. * @access private
  116. * @return bool
  117. */
  118. function MailSend($to,$header, $body) {
  119. if (strlen(ini_get("safe_mode"))< 1)
  120. {
  121. $old_from = ini_get("sendmail_from");
  122. ini_set("sendmail_from", $this->Sender);
  123. $params = sprintf("-oi -f %s", $this->Sender);
  124. $rt = mail($to, $this->Subject, $body,
  125. $header, $params);
  126. }
  127. else
  128. $rt = mail($to, $this->Subject, $body, $header);
  129. if (isset($old_from))
  130. ini_set("sendmail_from", $old_from);
  131. if(!$rt)
  132. {
  133. trigger_error("instantiate");
  134. return false;
  135. }
  136. return true;
  137. }
  138. /**
  139. * Sends mail via SMTP using PhpSMTP (Author:
  140. * Chris Ryan). Returns bool. Returns false if there is a
  141. * bad MAIL FROM, RCPT, or DATA input.
  142. * @access private
  143. * @return bool
  144. */
  145. function __maillog(){
  146. $this->errorinfo = $this->smtp->error;
  147. if(MAIL_LOG){
  148. error_log(print_r($this->smtp->error,true)."\n", 3, HOME_DIR."/log.txt");
  149. }
  150. }
  151. function SmtpSend($to,$header, $body,$config) {
  152. $system = &$GLOBALS['system'];
  153. $smtp_from = $this->Sender;
  154. if(!$this->smtp->Mail($smtp_from))
  155. {
  156. $this->__maillog();
  157. trigger_error("from_failed");
  158. $this->smtp->Reset();
  159. return false;
  160. }
  161. if(!$this->smtp->Recipient($to)){
  162. $this->__maillog();
  163. trigger_error("recipients_failed". $to);
  164. $this->smtp->Reset();
  165. return false;
  166. }
  167. if(!$this->smtp->Data($header ."\n". $body))
  168. {
  169. $this->__maillog();
  170. $this->smtp->Reset();
  171. return false;
  172. }
  173. $this->smtp->Reset();
  174. //$this->SmtpClose();
  175. return true;
  176. }
  177. /**
  178. * Initiates a connection to an SMTP server. Returns false if the
  179. * operation failed.
  180. * @access private
  181. * @return bool
  182. */
  183. function SmtpConnect($config) {
  184. $this->smtp->do_debug = $this->debug;
  185. $index = 0;
  186. $connection = ($this->smtp->Connected());
  187. if($this->smtp->Connect($config['smtpserver'], $config['smtpport'],20))
  188. {
  189. $this->smtp->Hello($_SERVER['SERVER_NAME']?$_SERVER['SERVER_NAME']:'localhost.localdomain');
  190. if($config['smtpuname'] && !$this->smtp->Authenticate($config['smtpuname'],$config['smtppasswd'])){
  191. trigger_error("authenticate");
  192. $this->smtp->Reset();
  193. $connection = false;
  194. }
  195. $connection = true;
  196. }
  197. if(!$connection) trigger_error("connect_host");
  198. return $connection;
  199. }
  200. /**
  201. * Closes the active SMTP session if one exists.
  202. * @return void
  203. */
  204. function SmtpClose() {
  205. if($this->smtp != NULL)
  206. {
  207. if($this->smtp->Connected())
  208. {
  209. $this->smtp->Quit();
  210. $this->smtp->Close();
  211. }
  212. }
  213. }
  214. }
  215. ?>