<?php

class mySESMail {
  private $msg;

  public function __construct() {
    require_once(sfConfig::get('sf_symfony_lib_dir').'/vendor/ses/SimpleEmailService.class.php');
    $this->msg = new SimpleEmailServiceMessage();
    $this->setFrom('hotro@violet.vn', 'Thu vien Violet');
  }

  public function setFrom($address, $name = null) {
    $this->msg->setFrom($name==null? $address: myUtility::vietDecode($name)." <$address>");
  }

  public function addAddress($address, $name = null) {
    $this->msg->addTo($name==null? $address: myUtility::vietDecode($name)." <$address>");
  }

  public function addReplyTo($address, $name = null) {
    $this->msg->addReplyTo($name==null? $address: myUtility::vietDecode($name)." <$address>");
  }

  public function setSubject($subject) {
    $this->msg->setSubject($subject);
  }

  public function setBody($body) {
    $this->msg->setMessageFromString(null, $body);
  }

  public function send() {
    try {
      myUtility::log($this->msg->to[0].' "'.$this->msg->subject.'"', 'mail.log');
      $ses = new SimpleEmailService();
      $result = $ses->sendEmail($this->msg);
      return (isset($result['RequestId'])? true: false);
    } catch (Exception $e) {
      return false;
    }
  }
}
