Last active
December 17, 2015 16:09
-
-
Save Bolinha1/5636568 to your computer and use it in GitHub Desktop.
Realizando o envio de emails usando PHPMailer, baseando-se em um tipo de mensagem a ser enviada.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class DefineTipoDeMensagem | |
| { | |
| private $tipo; | |
| public function __construct($tipo) | |
| { | |
| $this->tipo = $tipo; | |
| } | |
| public function getTipo() | |
| { | |
| return $this->tipo; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class EnviarEmail | |
| { | |
| private $email, $nome, $assunto, $mensagem, $tipo, $mail; | |
| public function __construct($email, $nome, DefineTipoDeMensagem $tipo, PHPMailer $mail) | |
| { | |
| $this->email = filter_var($email, FILTER_VALIDATE_EMAIL)? $email : 'Email nao valido'; | |
| $this->nome = $nome; | |
| $this->tipo = $tipo; | |
| $this->mail = $mail; | |
| } | |
| public function getEmail() | |
| { | |
| return $this->email; | |
| } | |
| public function getNome() | |
| { | |
| return $this->nome; | |
| } | |
| public function montaMensagem($param) | |
| { | |
| switch($this->tipo->getTipo()) | |
| { | |
| case 'emailAtualizacaoSenha' : | |
| $mensagem = '<div style="font-family: Arial,sans-serif; font-size: 12px; line-height: 150%" >'; | |
| $mensagem .= '<h3 style="background:rgb(241, 241, 241); padding:10px">Mensagem enviada pelo site</h3>'; | |
| $mensagem .= '<b>Mensagem enviada automática não precisa responder</b><br/>'; | |
| $mensagem .= '<b>Sua nova senha é:</b> '.$param.'<br/>'; | |
| break; | |
| case 'emailConfirmacaoArquivoTurmas' : | |
| $mensagem = '<div style="font-family: Arial,sans-serif; font-size: 12px; line-height: 150%" >'; | |
| $mensagem .= '<h3 style="background:rgb(241, 241, 241); padding:10px">Mensagem enviada pelo site</h3>'; | |
| $mensagem .= '<b>Mensagem enviada automática não precisa responder</b><br/>'; | |
| $mensagem .= '<b>A turma de'.$param.' recebeu novos arquivos</b> <br/>'; | |
| break; | |
| case 'emailConfirmacaoArquivoClientes' : | |
| $mensagem = '<div style="font-family: Arial,sans-serif; font-size: 12px; line-height: 150%" >'; | |
| $mensagem .= '<h3 style="background:rgb(241, 241, 241); padding:10px">Mensagem enviada pelo site</h3>'; | |
| $mensagem .= '<b>Mensagem enviada automática não precisa responder</b><br/>'; | |
| $mensagem .= '<b>Olá'.$param.'vocêle; recebeu novos arquivos.</b> <br/>'; | |
| break; | |
| } | |
| $this->mensagem = $mensagem; | |
| } | |
| public function getMensagemMontada() | |
| { | |
| return $this->mensagem; | |
| } | |
| public function defineAssunto() | |
| { | |
| switch($this->tipo->getTipo()) | |
| { | |
| case 'emailAtualizacaoSenha' : | |
| $this->assunto = 'Senha nova Sistema Professor Kenji'; | |
| break; | |
| case 'emailConfirmacaoArquivoTurmas' : | |
| $this->assunto = 'Há novos arquivos Sistema Professor Kenji'; | |
| break; | |
| case 'emailConfirmacaoArquivoClientes' : | |
| $this->assunto = 'Há novos arquivos Sistema EkoSeno'; | |
| break; | |
| } | |
| } | |
| public function getAssuntoDefinido() | |
| { | |
| return $this->assunto; | |
| } | |
| public function enviar() | |
| { | |
| $this->mail->IsSMTP(); // Define que a mensagem será SMTP | |
| $this->mail->Host = "smtp.Dominio"; // Endereço do servidor SMTP | |
| #$mail->SMTPSecure = 'tls'; | |
| $this->mail->SMTPAuth = true; // Usa autenticação SMTP? (opcional) | |
| $this->mail->Username = 'seuEmail@seuDominio'; // Usuário do servidor SMTP | |
| $this->mail->Password = 'suaSenha'; // Senha do servidor SMTP | |
| $this->mail->From = "seuEmail@seuDominio"; // Seu e-mail | |
| $this->mail->Sender = "seuEmail@seuDominio"; // Seu e-mail | |
| $this->mail->FromName = "Teste"; // Seu nome | |
| $this->mail->AddAddress($this->getEmail(), $this->getNome()); | |
| $this->mail->IsHTML(true); // Define que o e-mail será enviado como HTML | |
| $this->mail->Subject = $this->getAssuntoDefinido(); // Assunto da mensagem | |
| $this->mail->Body = utf8_decode($this->getMensagemMontada()); | |
| $enviado = $this->mail->Send(); | |
| $this->mail->ClearAllRecipients(); | |
| $this->mail->ClearAttachments(); | |
| if ($enviado) | |
| { | |
| return 'OK'; | |
| } | |
| else | |
| { | |
| echo "Não foi possível enviar o e-mail.<br /><br />"; | |
| echo "<b>Informações do erro:</b> <br />" . $this->mail->ErrorInfo; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'DefineTipo.php'; | |
| require 'EnviarEmail.php'; | |
| require 'phpMailer/class.phpmailer.php'; | |
| $mail = new PHPMailer(); | |
| $tipo = new DefineTipoDeMensagem('emailAtualizacaoSenha'); | |
| $envio = new EnviarEmail('[email protected]', 'Eduardo', $tipo, $mail); | |
| $envio->montaMensagem('Teste'); | |
| $envio->defineAssunto(); | |
| var_dump($envio->enviar()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment