【opencart3源码分析】邮件类mail.php

<?php
/**
 * @package		OpenCart
 * @author		Daniel Kerr
 * @copyright	Copyright (c) 2005 - 2017, OpenCart, Ltd. (https://www.opencart.com/)
 * @license		https://opensource.org/licenses/GPL-3.0
 * @link		https://www.opencart.com
*/

/**
* 邮件类 
*/
class Mail {
	protected $to;
	protected $from;
	protected $sender;
	protected $reply_to;
	protected $subject;
	protected $text;
	protected $html;
	protected $attachments = array();
	public $parameter;

	/**
	 * Constructor
	 * 构造方法
	 * @param	string	$adaptor
	 *
 	*/
	public function __construct($adaptor = 'mail') {
		$class = 'Mail\\' . $adaptor;
        // 如果该类存在
		if (class_exists($class)) {
		    // 实例化类
			$this->adaptor = new $class();
		} else {
			trigger_error('Error: Could not load mail adaptor ' . $adaptor . '!');
			exit();
		}
	}

	/**
     *
     * 收件人
     * @param	mixed	$to
     */
	public function setTo($to) {
		$this->to = $to;
	}

	/**
     *
     * 发件人
     * @param	string	$from
     */
	public function setFrom($from) {
		$this->from = $from;
	}

	/**
     *
     * 发送
     * @param	string	$sender
     */
	public function setSender($sender) {
		$this->sender = $sender;
	}

	/**
     *
     * 设置回复
     * @param	string	$reply_to
     */
	public function setReplyTo($reply_to) {
		$this->reply_to = $reply_to;
	}

	/**
     *
     * 设置主题
     * @param	string	$subject
     */
	public function setSubject($subject) {
		$this->subject = $subject;
	}

	/**
     *
     * 设置文本
     * @param	string	$text
     */
	public function setText($text) {
		$this->text = $text;
	}

	/**
     *
     * 设置html
     * @param	string	$html
     */
	public function setHtml($html) {
		$this->html = $html;
	}

	/**
     *
     * 添加附件
     * @param	string	$filename
     */
	public function addAttachment($filename) {
		$this->attachments[] = $filename;
	}

	/**
     *
     * 发送
     */
	public function send() {
		if (!$this->to) {
			throw new \Exception('Error: E-Mail to required!');
		}

		if (!$this->from) {
			throw new \Exception('Error: E-Mail from required!');
		}

		if (!$this->sender) {
			throw new \Exception('Error: E-Mail sender required!');
		}

		if (!$this->subject) {
			throw new \Exception('Error: E-Mail subject required!');
		}

		if ((!$this->text) && (!$this->html)) {
			throw new \Exception('Error: E-Mail message required!');
		}
        // 遍历对象属性
		foreach (get_object_vars($this) as $key => $value) {
			$this->adaptor->$key = $value;
		}

		$this->adaptor->send();
	}
}

猜你喜欢

转载自blog.csdn.net/qq2942713658/article/details/81429361