<?php

/* ========================================================================
 * $Id: mail.php 2483 2017-09-17 15:13:46Z onez $
 * 
 * Email: www@onez.cn
 * QQ: 6200103
 * HomePage: http://www.onezphp.com
 * ========================================================================
 * Copyright 2016-2017 佳蓝科技.
 * 
 * ======================================================================== */

!defined('IN_ONEZ') && exit('Access Denied');
#名称：邮件发送
#标识：mail

class onezphp_mail extends onezphp{
  var $emails=array();
  var $files=array();
  function __construct(){
    
  }
  function addemail($email,$name=false){
    $this->emails[]=array($email,$name);
    return $this;
  }
  function addfile($file,$name=false){
    $this->files[]=array($file,$name);
    return $this;
  }
  function send($subject,$content){
    include_once(dirname(__FILE__).'/lib/PHPMailerAutoload.php');

    $mail = new PHPMailer;

    //$mail->SMTPDebug = 3;                               // Enable verbose debug output

    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = $this->getvar('mail_host');  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = $this->getvar('mail_username');                 // SMTP username
    $mail->Password = $this->getvar('mail_password');                           // SMTP password
    if($this->getvar('mail_ssl')){
      $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
    }
    $mail->Port = (int)$this->getvar('mail_port');                                    // TCP port to connect to

    $mail->setFrom($this->getvar('mail_from_email'), $this->getvar('mail_from_name'));
    //$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
    //$mail->addAddress($this->getvar('mail_email'));               // Name is optional
    //$mail->addReplyTo('info@example.com', 'Information');
    //$mail->addCC('cc@example.com');
    //$mail->addBCC('bcc@example.com');
    
    foreach($this->emails as $v){
      if($v[1]===false){
        $mail->addAddress($v[0]); 
      }else{
        $mail->addAddress($v[0],$v[1]); 
      }
    }
    foreach($this->files as $v){
      if($v[1]===false){
        $mail->addAttachment($v[0]);
      }else{
        $mail->addAttachment($v[0],$v[1]);
      }
    }
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = $subject;
    $mail->Body    = $content;
    $mail->AltBody = strip_tags($content);

    if(!$mail->send()) {
      $this->status=array(
        'status'=>'error',
        'message'=>$mail->ErrorInfo,
      );
    } else {
      $this->status=array(
        'status'=>'success',
      );
    }
    return $this;
  }
}