Email function using templates. Includes via ob_start and global vars

前端 未结 3 745
无人及你
无人及你 2021-01-14 08:55

I have a simple Email() class. It\'s used to send out emails from my website.


I also h

3条回答
  •  被撕碎了的回忆
    2021-01-14 09:57

    You can find a more elegant solution to your problem in this answer.
    Notice the usage of the PHP extract function to instantiate the template variables.
    In other words, you should move the template parsing logic outside the e-mail sending function.
    For example:

    _tpl = $tpl_name;
        }
    
        public function __set($name, $value) {
            $this->_vars[$name] = $value;
        }
    
        public function setVars($values) {
            $this->_vars = $values;
        }
    
        public function parse() {
            ob_start();
            extract($this->_vars);
            include $this->_tpl;
            return ob_get_clean();
        }
    }
    
    abstract class Email {
        public static function send($to, $subj, $msg, $options = array()) {
            /* ... */
        }
    }
    
    $tpl = new SimpleTemplate('/inc/email/templates/account_created.php');
    $tpl->name = 'Stack Overflow';
    $tpl->SITE_NAME = 'site_name';
    $tpl->SITE_URL = 'localhost';
    Email::send("me@localhost", "Subject", $tpl->parse());
    
    ?>
    

提交回复
热议问题