1. 程式人生 > >TempletMail 實現傳送郵件

TempletMail 實現傳送郵件

話不多說,實現功能為主

 1、引入phpmailer,

  2、初始化(TP框架中)

if ((new TempletMail())->OrdertenantSuccessful($info['email'], '', [])) {

} else {

}
public function OrdertenantSuccessful($address, $recipientName='', $replace=array())
{
    if(!$address) return false;
    $subject = '車友端傳送郵件成功';
    $subject = 'system';

    $replace['subject'] = $subject;
    $this->view->assign($replace);
    $body = $this->view->fetch('
[email protected]
/OrdertenantSuccessful' . $this->lang); return SendMail::Send($address, $recipientName, $subject, $body, '', ''); }
public function fetch($template = '', $vars = [], $config = [], $renderContent = false)
{
    // 模板變數
    $vars = array_merge(self::$var, $this->data, $vars);

    // 頁面快取
    ob_start();
    ob_implicit_flush(0);

    // 渲染輸出
    try {
        $method = $renderContent ? 'display' : 'fetch';
        $this->engine->$method($template, $vars, $config);
    } catch (\Exception $e) {
        ob_end_clean();
        throw $e;
    }
    $content = ob_get_clean();
    Container::get('hook')->listen('view_filter', $content);

    return $content;
}
 public static function Send($address, $recipientName='', $subject='', $body, $from='', $senderName='')
    {
        is_null(self::$init) && self::boot();
        if(!$address || !$body)
        {
            return false;
        }
        
        /**
         * 發件人地址 && 姓名
         */
        $from = $from?$from:self::$SYSTME_FROM;
        $senderName = $senderName?$senderName:self::$SYSTME_SENDERNAME;
        
        try
        {
            $mail = new PHPMailer();
            $mail->Charset = 'UTF-8';
            
            $mail->isSMTP();
            $mail->Username = self::$SMTP_USERNAME;
            $mail->Password = self::$SMTP_PASSWORD;
            $mail->Host = self::$SMTP_HOST; 
            $mail->setFrom($from, $senderName);
            $mail->addAddress($address, $recipientName);//收件人郵箱
            $mail->Subject = "=?utf-8?B?" . base64_encode("$subject") . "?=";
            $mail->Body = $body;//收件內容
            $mail->SMTPAuth = true;
            $mail->SMTPSecure = 'tls';
            $mail->Port = self::$SMTP_PORT;
            $mail->isHTML(true);
            $mail->AltBody = self::$ALT_BODY;
            $ret = $mail->send();
        }catch (\Exception $e)
        {
            Log::error($e->getMessage());
            exception($message);
        }
        
        if(!$ret)
        {
            $message = 'Message could not be sent. Mailer Error: '. $mail->ErrorInfo;
            Log::error($message);
            return false;
        }
        return true;
    }

最近實現各種操作完成後郵件簡訊功能,正好有空來更新自己的部落格。

此篇文章是實現郵件的功能,等時間充足再來更新簡訊功能。