Client.php 1.53 KB
<?php

/**
 * Email发送
 * @author tongdesheng
 *
 */
require_once 'phpmailer/class.phpmailer.php';

class YHMUtils_Email_Client {
	
	private $_mailer = null;
	
	/**
	 * 对参数进行初始化
	 */
	public function __construct() {
		$this->_mailer = new PHPMailer();
	}
	
	/**
	 * 初始化发送参数
	 * @param array $smtpParam
	 */
	private function init($smtpParam) {
		$this->_mailer->isSMTP();
		$this->_mailer->Host = $smtpParam['host'];
		$this->_mailer->Port = $smtpParam['port'];
		$this->_mailer->SMTPAuth = $smtpParam['auth'];
		$this->_mailer->Username = $smtpParam['username'];
		$this->_mailer->Password = $smtpParam['password'];
		$this->_mailer->From = $smtpParam['from'];
		$this->_mailer->FromName = $smtpParam['fromname'];
		$this->_mailer->CharSet = 'utf-8';
		$this->_mailer->Encoding = "base64";
		
		
	}
	
	/**
	 * 通过模板发送邮件
	 * @param string $toMail
	 * @param string $tplName 模板名称
	 * @param string $tplData 模板数据
	 */
	public function sendByTpl($toMail, $tplName, $tplData = array()) {
		$tplParam = YHMConfig_Email::$tplData[$tplName];
		$smtpParam = YHMConfig_Email::${$tplParam['smtp']};
		$this->init($smtpParam);
		$this->_mailer->Subject = strtr($tplParam['title'], $tplData);
		$this->_mailer->Body = strtr($tplParam['content'], $tplData);
		$this->_mailer->isHTML(empty($tplParam['is_html']) ? false : $tplParam['is_html']);
		$this->_mailer->addAddress($toMail);
		$ret =  $this->_mailer->send();
		if(empty($ret)) {
			throw new Exception($this->_mailer->ErrorInfo);
		}
		return true;
	}
	
}