|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Classe para implementacao envio de Emails em Massa ou <EFBFBD>nico!
|
|
|
|
|
*
|
|
|
|
|
* @author Lucas Awade
|
|
|
|
|
* @Desenvolvedor
|
|
|
|
|
* @version 1.1.0
|
|
|
|
|
*/
|
|
|
|
|
require("Logger.php");
|
|
|
|
|
require("phpmailer/class.phpmailer.php");
|
|
|
|
|
|
|
|
|
|
class SimpleMail {
|
|
|
|
|
########################################################################
|
|
|
|
|
## CONFIGURACOES CLASSE ##
|
|
|
|
|
########################################################################
|
|
|
|
|
|
|
|
|
|
private $log;
|
|
|
|
|
private $limit;
|
|
|
|
|
private $sended = 0;
|
|
|
|
|
private $mailer;
|
|
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
|
## CONFIGURACOES MAIL ##
|
|
|
|
|
########################################################################
|
|
|
|
|
private $host;
|
|
|
|
|
private $port;
|
|
|
|
|
private $smtpAuth;
|
|
|
|
|
private $smtpSecure;
|
|
|
|
|
private $charset;
|
|
|
|
|
private $mail;
|
|
|
|
|
private $passwd;
|
|
|
|
|
private $title;
|
|
|
|
|
private $subject;
|
|
|
|
|
private $from;
|
|
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
|
## CONFIGURACOES DEFAULT ##
|
|
|
|
|
########################################################################
|
|
|
|
|
|
|
|
|
|
public function __construct($mail, $passwd, $subject, $logger = false) {
|
|
|
|
|
$this->log = new Logger('simplesmailer', $logger, "/var/log/");
|
|
|
|
|
if ($mail && $passwd && $subject) {
|
|
|
|
|
$this->mail = $mail;
|
|
|
|
|
$this->passwd = $passwd;
|
|
|
|
|
$this->subject = $subject;
|
|
|
|
|
$this->phpMailer();
|
|
|
|
|
} else {
|
|
|
|
|
$this->log->error('Nao foi possivel instanciar a Classe SimplesMail,informacoes incompletas!', debug_backtrace());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Metodo de configura<EFBFBD><EFBFBD>o do Host de Envio;
|
|
|
|
|
* @param string $host
|
|
|
|
|
* @param string|int $port
|
|
|
|
|
* @param string $title
|
|
|
|
|
* @param boolean $smtpAuth
|
|
|
|
|
* @param string $smtpSecure
|
|
|
|
|
* @param string $charset
|
|
|
|
|
*/
|
|
|
|
|
public function config($host, $port, $title, $smtpAuth = true, $smtpSecure = 'TLS', $charset = 'UTF8') {
|
|
|
|
|
$this->host = $host;
|
|
|
|
|
$this->port = $port;
|
|
|
|
|
$this->title = $title;
|
|
|
|
|
$this->smtpAuth = $smtpAuth;
|
|
|
|
|
$this->smtpSecure = $smtpSecure;
|
|
|
|
|
$this->charset = $charset;
|
|
|
|
|
$this->phpMailer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Informa um limite de email em massa a ser enviado.
|
|
|
|
|
*
|
|
|
|
|
* @param int $limit
|
|
|
|
|
*/
|
|
|
|
|
public function limitSend($limit) {
|
|
|
|
|
$this->log->debug('Limite de Email: ' . $limit, debug_backtrace());
|
|
|
|
|
$this->limit = $limit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Mensagem escrito em HTML para envio de um email elaborado.
|
|
|
|
|
*
|
|
|
|
|
* Os dados em $data devem ser em formato array como o exemplo:
|
|
|
|
|
* array("MSG_HEADER" => 'Seja Bem vindo', "MSG_FOOTER" => 'Volte Sempre');
|
|
|
|
|
*
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @param array $data
|
|
|
|
|
*/
|
|
|
|
|
public function pathBodyMail($path, $data) {
|
|
|
|
|
$this->log->info('Path HTML Body: ' . $path, debug_backtrace());
|
|
|
|
|
$contents = file_get_contents($path);
|
|
|
|
|
foreach ($data as $key => $value) {
|
|
|
|
|
$contents = str_replace($key, $value, $contents);
|
|
|
|
|
}
|
|
|
|
|
$this->mailer->Body = $contents;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Mensagem simples para o envio rapido de um email.
|
|
|
|
|
*
|
|
|
|
|
* @param string $message
|
|
|
|
|
*/
|
|
|
|
|
public function bodyMessage($message) {
|
|
|
|
|
$this->mailer->Body = $message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adiciona no email anexos para serem enviados
|
|
|
|
|
* @param array|string $files
|
|
|
|
|
*/
|
|
|
|
|
public function setAddAttachment($files) {
|
|
|
|
|
if ($files) {
|
|
|
|
|
if (is_array($files)) {
|
|
|
|
|
foreach ($files as $name => $file) {
|
|
|
|
|
$this->mailer->AddAttachment($file, (is_numeric($name) ? '' : $name));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$this->mailer->AddAttachment($files);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adiciona no email imagens para visualizadas.
|
|
|
|
|
*
|
|
|
|
|
* #=> Caso o paramtro for um array seu corpo deve ser enviado da seguinte forma:
|
|
|
|
|
* $cid = array('minha_img_jpg' => 'img/path/mail/header.jpg');
|
|
|
|
|
*
|
|
|
|
|
* #=> Caso for uma string deve se passar o cid caso contrario gera um cid randomico;
|
|
|
|
|
*
|
|
|
|
|
* @param array|string $images
|
|
|
|
|
*/
|
|
|
|
|
public function setAddEmbeddedImage($images, $cid = null) {
|
|
|
|
|
if ($images) {
|
|
|
|
|
if (is_array($images)) {
|
|
|
|
|
foreach ($images as $name => $image) {
|
|
|
|
|
$this->mailer->AddEmbeddedImage($image, $name);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$cid = $cid ? $cid : rand(100, 999999);
|
|
|
|
|
$this->mailer->AddEmbeddedImage($image, $cid);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Esta funcao envia os email em massa ou unicos.
|
|
|
|
|
*
|
|
|
|
|
* Email em massa <EFBFBD> necessario passar um array com os emails seu indice que representa o email.
|
|
|
|
|
*
|
|
|
|
|
* Email unico so <EFBFBD> preciso passar o email na variavel $data;
|
|
|
|
|
*
|
|
|
|
|
* @param array|string $data
|
|
|
|
|
* @param string $indice
|
|
|
|
|
* @return array|boolean
|
|
|
|
|
*/
|
|
|
|
|
public function mailing($data, $indice = '') {
|
|
|
|
|
if (is_array($data)) {
|
|
|
|
|
$invalid = array();
|
|
|
|
|
foreach ($data as $mail) {
|
|
|
|
|
if ($this->sended <= $this->limit) {
|
|
|
|
|
$mail = $indice ? $mail[$indice] : $mail;
|
|
|
|
|
$this->setSended();
|
|
|
|
|
if (!$this->send($mail)) {
|
|
|
|
|
$invalid[] = $mail;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $invalid;
|
|
|
|
|
} else {
|
|
|
|
|
if ($this->send($data)) {
|
|
|
|
|
$this->setSended();
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setFrom($from) {
|
|
|
|
|
$this->from = $from;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getErrorSend(){
|
|
|
|
|
return $this->mailer->ErrorInfo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
|
## ENVIO MAIL ##
|
|
|
|
|
########################################################################
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gerencia a quantidade de email que esta sendo enviado.
|
|
|
|
|
*/
|
|
|
|
|
private function setSended() {
|
|
|
|
|
$this->sended++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gerencia o envio do email para estar sempre no limite de envio.
|
|
|
|
|
*
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
|
|
|
|
public function getSended() {
|
|
|
|
|
$this->log->debug("Sended: " . $this->sended, debug_backtrace());
|
|
|
|
|
if ($this->sended < $this->limit) {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Metodo responsavel para realizar o envio do email.
|
|
|
|
|
*
|
|
|
|
|
* @param string $mailFrom
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
|
|
|
|
private function send($mailFrom) {
|
|
|
|
|
if (!$this->mailer) {
|
|
|
|
|
$this->log->error("Objeto nao criado! E-mail From: " . $this->from, debug_backtrace());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$this->mailer->setFrom(($this->from ? $this->from : $this->mail), $this->title);
|
|
|
|
|
$this->mailer->addAddress($mailFrom, $this->title);
|
|
|
|
|
$this->mailer->send();
|
|
|
|
|
$this->mailer->clearAllRecipients();
|
|
|
|
|
$this->log->success("Mail: {$mailFrom} | Send Queue: {$this->sended}", debug_backtrace());
|
|
|
|
|
return true;
|
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
|
if ($this->mailer->ErrorInfo) {
|
|
|
|
|
$this->log->error("ErrorInfo Mail: " . $this->mailer->ErrorInfo . " | Exception Mail: {$mailFrom} >> " . $ex->getMessage(), debug_backtrace());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Cria as configura<EFBFBD><EFBFBD>es e parametros para ser implementado e enviado os emails.
|
|
|
|
|
*
|
|
|
|
|
* @void null
|
|
|
|
|
*/
|
|
|
|
|
private function phpMailer() {
|
|
|
|
|
$this->log->debug("Instanciando objeto PHPMailer", debug_backtrace());
|
|
|
|
|
$this->mailer = new PHPMailer(true);
|
|
|
|
|
$this->mailer->isSMTP();
|
|
|
|
|
$this->mailer->Host = $this->host;
|
|
|
|
|
$this->mailer->SMTPAuth = $this->smtpAuth;
|
|
|
|
|
$this->mailer->isHTML(true);
|
|
|
|
|
|
|
|
|
|
if ($this->smtpAuth) {
|
|
|
|
|
$this->mailer->SMTPSecure = $this->smtpSecure;
|
|
|
|
|
$this->mailer->Username = $this->mail;
|
|
|
|
|
$this->mailer->Password = $this->passwd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->mailer->CharSet = $this->charset;
|
|
|
|
|
$this->mailer->Port = $this->port;
|
|
|
|
|
$this->mailer->Subject = $this->subject;
|
|
|
|
|
}
|
|
|
|
|
}
|