<?php
class htmlMimeMail {
	var $SMTP_HOST;
	var $SMTP_PORT;
	var $SMTP_HELLO;
	var $SMTP_AUTH;
	var $SMTP_AUTH_TYPE;
	var $SMTP_USER;
	var $SMTP_PASS;
	var $HEADERS;
	var $BODY_HTML;
	var $BODY_TEXT;
	var $EMAIL_SUBJECT;
	var $EMAIL_RETURN_PATH;
	var $EMAIL_FROM;
	var $CHARSET='ISO-8859-1'; //por padrão o charset é ISO
	var $errors;
	function STANDARDMAIL()	{
	}

	/*************************
	Método que seta o UTF-8 para conseguir enviar o e-mail
	pelo AMAZON SES
	Por Default o valor da variável charset é 0 que é ISO-8859-1 COISA
	Se for passado "utf-8" o charset será UTF-8
	**************************/
	function setCharSet($charset="ISO-8859-1"){
		$this->CHARSET = $charset;
	}
	/*************************
	Método que seta o UTF-8 para conseguir enviar o e-mail
	pelo AMAZON SES
	Por Default o valor da variável charset é 0 que é ISO-ALGUMA COISA
	Se for passado 1 o charset será UTF-8
	**************************/
	

	function setSMTPParams( $host, $port, $hello, $auth, $auth_type, $user, $pass ){
		$this->SMTP_HOST = $host;
		$this->SMTP_PORT = $port;
		$this->SMTP_HELLO = $hello;
		$this->SMTP_AUTH = $auth;
		$this->SMTP_AUTH_TYPE = $auth_type;
		$this->SMTP_USER = $user;
		$this->SMTP_PASS = $pass;
	}

	function setHtml($html, $text){
		$this->BODY_HTML = $html;
		$this->BODY_TEXT = $text;
	}

	function setSubject( $subject ){
		$this->EMAIL_SUBJECT = $subject;
	}

	function setReturnPath( $return ){
		$this->EMAIL_RETURN_PATH = $return;
	}

	function setFrom( $from ){
		$this->EMAIL_FROM = $from;
	}

	function setHeader( $name, $value ){
		$this->HEADERS[$name] = $value;
	}

	function send($recipients, $method){
		$result = false;
		for( $i = 0; $i < count($recipients); $i++){
			$result_individual = $this->sendOnlyOne( $recipients[$i] , $method);
			if ( $result_individual ) {
				if( !$result ){
					$result = true;
				}
			}
		}
		return $result;
	}

	function sendOnlyOne( $recipient, $method ){
		//// Verifica Se é Servidor Google
		$google_server = ( (strpos($this->SMTP_HOST, "gmail.com") > 0) || (strpos($this->SMTP_HOST, "google.com") > 0) ) || ( $this->SMTP_AUTH_TYPE == "tls" );
		$ssl_auth = ( $this->SMTP_AUTH_TYPE == "ssl" );
		$SSL_OR_TLS = ( $google_server || $ssl_auth );
		if ( $SSL_OR_TLS ){
			if ( $ssl_auth ){
				$auth_type = 1; //ssl
			} else {
				$auth_type = 2; //tls ( google )
			}
			//ENVIAR EMAILS UTILIZANDO GMAIL
			//-----------------------------------------------
			$m = new dSendMail2;
			$m->setCharset($this->CHARSET);
			$m->setTo( $recipient );
			$m->setReturnPath( $this->EMAIL_RETURN_PATH );
			
			if (strtoupper($this->CHARSET) == "UTF-8"){
			    //transforma para codificação UTF-8
				$m->setFrom( utf8_encode($this->EMAIL_FROM) );
				$m->setSubject( utf8_encode($this->EMAIL_SUBJECT) );
				$m->setHtml( utf8_encode($this->BODY_HTML), utf8_encode($this->BODY_TEXT) );
			} else {
				$m->setFrom( $this->EMAIL_FROM );
				$m->setSubject( $this->EMAIL_SUBJECT );
				$m->setHtml( $this->BODY_HTML, $this->BODY_TEXT );
			}
			
			foreach ($this->HEADERS as $name => $value)
				$m->headers[$name] = $value;

			$m->sendThroughSMTP($this->SMTP_HOST, $this->SMTP_PORT, $this->SMTP_USER, $this->SMTP_PASS, $auth_type);
			$result = $m->send();
			$this->errors = $m->error;
			return $result;
		} else if( ($method == "mail")||($method == "smtp") ){
			//ENVIAR EMAILS UTILIZANDO MAIL ou SMTP
			//-----------------------------------------------
			$mail = new htmlMimeMail1();
			if( $method == "smtp" ) {
				$mail->setSMTPParams( $this->SMTP_HOST, $this->SMTP_PORT, $this->SMTP_HELLO, true, $this->SMTP_USER, $this->SMTP_PASS );
			}
    	    $mail->setHtml( $this->BODY_HTML,$this->BODY_TEXT );
			$mail->setSubject( $this->EMAIL_SUBJECT );
			$mail->setReturnPath($this->EMAIL_RETURN_PATH);
			$mail->setFrom( $this->EMAIL_FROM );
			foreach ($this->HEADERS as $name => $value)
				$mail->setHeader($name, $value);

			$result = $mail->send( array($recipient), $method );
			$this->errors = $mail->errors;
			return $result;
		} 
	}
}//end class
?>