<?php
//autoload not use
require_once __DIR__."/PHPMailer/class.webmail.php";
require_once __DIR__."/PHPMailer/class.webmailoauthgoogle.php";
require_once __DIR__."/PHPMailer/class.webmailoauth.php";
require_once __DIR__."/PHPMailer/class.webmailpop3.php";
require_once __DIR__."/PHPMailer/class.webmailsmtp.php";


class Spmail { //simplemail
  public $mail;

  public $charset = "UTF-8";
  public $to_name = "";
  public $to_mail = "";
  public $from_name = "";
  public $from_mail = "";
  public $subject = "";
  public $mailbody = "";

  public $host = "localhost";
  public $port = 25;
  public $secure = "tls";
  public $authlogin = true;
  public $smtp_id = "";
  public $smtp_pwd = "";

  public function __construct(){
    $this->mail = new Frody\Util\PHPMailer;
  }

  private function ness_field( $authlogin=false ){
    if( !$this->to_mail   ){ echo "[to_mail] is necessary!"; return true; }
    if( !$this->from_mail ){ echo "[from_mail] is necessary!"; return true; }
    if( !$this->subject   ){ echo "[subject] is necessary!"; return true; }
    if( !$this->mailbody  ){ echo "[mailbody] is necessary!"; return true; }
    if( $authlogin ){
      if( !$this->smtp_id ){ echo "[smtp_id] is necessary!"; return true; }
      if( !$this->smtp_pwd ){ echo "[smtp_pwd] is necessary!"; return true; }
    }
    return false;
  }

  public function send(){
    if( $this->ness_field() ){
      return false;
    }

    //$mail->isSendmail();
    $this->mail->CharSet = $this->charset;
    $this->mail->Encoding = 'base64';
  
    $this->mail->setFrom($this->from_mail, $this->from_name);
    $this->mail->addReplyTo($this->from_mail, $this->from_name);
    $this->mail->addAddress($this->to_mail, $this->to_name);
    $this->mail->Subject = $this->subject;
    $this->mail->msgHTML($this->mailbody);
    $this->mail->AltBody=strip_tags($this->mailbody);
  
    if (!$this->mail->send()) {
      return false;
    } else {
      return true;
    }
  }

  public function smtp(){
    if( $this->ness_field($this->authlogin) ){
      return false;
    }

    $this->mail->isSMTP();
    $this->mail->SMTPDebug = 0; //1 발송서버메세지, 2 발송/수신서버메세지
    $this->mail->Host = $this->host;
    $this->mail->Port = $this->port;
    $this->mail->SMTPSecure = $this->secure;
    $this->mail->SMTPAuth = $this->authlogin;
    if( $this->authlogin ){
      $this->mail->Username = $this->smtp_id;
      $this->mail->Password = $this->smtp_pwd;
    }
  
    $this->mail->CharSet = $this->charset;
    $this->mail->Encoding = 'base64';
  
    $this->mail->setFrom($this->from_mail, $this->from_name);
    $this->mail->addReplyTo($this->from_mail, $this->from_name);
    $this->mail->addAddress($this->to_mail, $this->to_name);
    $this->mail->Subject = $this->subject;
    $this->mail->msgHTML($this->mailbody);
    $this->mail->AltBody=strip_tags($this->mailbody);
  
    if (!$this->mail->send()) {
      return false;
    } else {
      return true;
    }
  }
}