/b2b/core/model/system/mdl.email.php
PHP | 244 lines | 167 code | 29 blank | 48 comment | 22 complexity | ac4e9d387cc4ad20d6cedf65b04702de MD5 | raw file
- <?PHP
- class mdl_email{
- var $hasTitle = true; //?????
- var $maxtime = 300; //?????? ,??:?
- var $maxbodylength =300; //????
- var $allowMultiTarget=false; //???????
- var $targetSplit = ',';
- var $Sendmail = "/usr/sbin/sendmail";
- function ready($config){
- $system = &$GLOBALS['system'];
- $this->smtp = &$system->loadModel('utility/smtp');
- if($config['sendway']=='smtp'){
- if(!$this->SmtpConnect($config)) return false;
- }
- return true;
- }
- /**
- * finish
- * ????????????
- *
- * @param mixed $config
- * @access public
- * @return void
- */
- function finish($config){
- if($config['sendway']=='smtp'){
- $this->SmtpClose();
- }
- }
-
- /**
- * send
- * ????,?????
- *
- * config???getOptions???????????
- *
- * @param mixed $to
- * @param mixed $message
- * @param mixed $config
- * @access public
- * @return void
- */
- function send($to, $subject, $body, $config){
- $system = &$GLOBALS['system'];
- $this->Sender = $config['usermail'];
- $this->Subject = $this->inlineCode($subject);
- $header = array(
- 'Return-path'=>'<'.$config['usermail'].'>',
- 'Date'=>date('r'),
- 'From'=>$this->inlineCode($system->getConf('system.shopname')).'<'.$config['usermail'].'>',
- 'MIME-Version'=>'1.0',
- 'Subject'=>$this->Subject,
- 'To'=>$to,
- 'Content-Type'=>'text/html; charset=UTF-8; format=flowed',
- 'Content-Transfer-Encoding'=>'base64'
- );
- $body = chunk_split(base64_encode($body));
- $header = $this->buildHeader($header);
- $config['sendway']=($config['sendway'])?$config['sendway']:'smtp';
- switch($config['sendway'])
- {
- case "sendmail":
- $result = $this->SendmailSend($to,$header, $body);
- break;
- case "mail":
- $result = $this->MailSend($to,$header, $body);
- break;
- case "smtp":
- $result = $this->SmtpSend($to,$header, $body,$config);
- break;
- default:
- trigger_error('mailer_not_supported',E_ERROR);
- $result = false;
- break;
- }
- return $result;
- }
- function inlineCode($str){
- $str = trim($str);
- return $str?'=?UTF-8?B?'.base64_encode($str).'?= ':'';
- }
- function buildHeader($headers){
- $ret = '';
- foreach($headers as $k=>$v){
- $ret.=$k.': '.$v."\n";
- }
- return $ret;
- }
- /**
- * Sends mail using the $Sendmail program.
- * @access private
- * @return bool
- */
- function SendmailSend($header, $body) {
- if ($this->Sender != "")
- $sendmail = sprintf("%s -oi -f %s -t", $this->Sendmail, $this->Sender);
- else
- $sendmail = sprintf("%s -oi -t", $this->Sendmail);
- if(!@$mail = popen($sendmail, "w"))
- {
- $this->__maillog();
- return false;
- }
- fputs($mail, $header);
- fputs($mail, $body);
- $result = pclose($mail) >> 8 & 0xFF;
- if($result != 0)
- {
- $this->__maillog();
- return false;
- }
- return true;
- }
- /**
- * Sends mail using the PHP mail() function.
- * @access private
- * @return bool
- */
- function MailSend($to,$header, $body) {
- if (strlen(ini_get("safe_mode"))< 1)
- {
- $old_from = ini_get("sendmail_from");
- ini_set("sendmail_from", $this->Sender);
- $params = sprintf("-oi -f %s", $this->Sender);
- $rt = mail($to, $this->Subject, $body,
- $header, $params);
- }
- else
- $rt = mail($to, $this->Subject, $body, $header);
- if (isset($old_from))
- ini_set("sendmail_from", $old_from);
- if(!$rt)
- {
- trigger_error("instantiate");
- return false;
- }
- return true;
- }
- /**
- * Sends mail via SMTP using PhpSMTP (Author:
- * Chris Ryan). Returns bool. Returns false if there is a
- * bad MAIL FROM, RCPT, or DATA input.
- * @access private
- * @return bool
- */
- function __maillog(){
- $this->errorinfo = $this->smtp->error;
- if(MAIL_LOG){
- error_log(print_r($this->smtp->error,true)."\n", 3, HOME_DIR."/log.txt");
- }
- }
- function SmtpSend($to,$header, $body,$config) {
- $system = &$GLOBALS['system'];
- $smtp_from = $this->Sender;
- if(!$this->smtp->Mail($smtp_from))
- {
- $this->__maillog();
- trigger_error("from_failed");
- $this->smtp->Reset();
- return false;
- }
-
- if(!$this->smtp->Recipient($to)){
- $this->__maillog();
- trigger_error("recipients_failed". $to);
- $this->smtp->Reset();
- return false;
- }
-
- if(!$this->smtp->Data($header ."\n". $body))
- {
- $this->__maillog();
- $this->smtp->Reset();
- return false;
- }
-
- $this->smtp->Reset();
- //$this->SmtpClose();
-
- return true;
- }
- /**
- * Initiates a connection to an SMTP server. Returns false if the
- * operation failed.
- * @access private
- * @return bool
- */
- function SmtpConnect($config) {
- $this->smtp->do_debug = $this->debug;
- $index = 0;
- $connection = ($this->smtp->Connected());
- if($this->smtp->Connect($config['smtpserver'], $config['smtpport'],20))
- {
- $this->smtp->Hello($_SERVER['SERVER_NAME']?$_SERVER['SERVER_NAME']:'localhost.localdomain');
- if($config['smtpuname'] && !$this->smtp->Authenticate($config['smtpuname'],$config['smtppasswd'])){
- trigger_error("authenticate");
- $this->smtp->Reset();
- $connection = false;
- }
- $connection = true;
- }
- if(!$connection) trigger_error("connect_host");
- return $connection;
- }
- /**
- * Closes the active SMTP session if one exists.
- * @return void
- */
- function SmtpClose() {
- if($this->smtp != NULL)
- {
- if($this->smtp->Connected())
- {
- $this->smtp->Quit();
- $this->smtp->Close();
- }
- }
- }
- }
- ?>