Last active
December 17, 2015 16:59
-
-
Save Bolinha1/5642795 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, mas agora com a criação da mensagem sendo criada em uma classe especifica.
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
| <?php | |
| class Mensagem | |
| { | |
| private $tipo, $assunto, $mensagem; | |
| public function __construct(DefineTipoDeMensagem $tipo) | |
| { | |
| $this->tipo = $tipo; | |
| } | |
| 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; | |
| } | |
| } |
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, $mensagem, $mail; | |
| public function __construct($email, $nome, Mensagem $mensagem, PHPMailer $mail) | |
| { | |
| $this->email = filter_var($email, FILTER_VALIDATE_EMAIL)? $email : 'Email nao valido'; | |
| $this->nome = $nome; | |
| $this->mensagem = $mensagem; | |
| $this->mail = $mail; | |
| } | |
| public function getEmail() | |
| { | |
| return $this->email; | |
| } | |
| public function getNome() | |
| { | |
| return $this->nome; | |
| } | |
| public function enviar() | |
| { | |
| $this->mail->IsSMTP(); | |
| $this->mail->Host = "smtp.dominio"; | |
| $this->mail->SMTPAuth = true; | |
| $this->mail->Username = 'seuEmail@seuDominio'; | |
| $this->mail->Password = 'suaSenha'; | |
| $this->mail->From = "email@adominio"; | |
| $this->mail->Sender = "email@adominio"; | |
| $this->mail->FromName = "Sistema "; | |
| $this->mail->AddAddress($this->getEmail(), $this->getNome()); | |
| $this->mail->IsHTML(true); // Define que o e-mail será enviado como HTML | |
| $this->mail->Subject = $this->mensagem->getAssuntoDefinido(); | |
| $this->mail->Body = utf8_decode($this->mensagem->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
| <?php | |
| require 'DefineTipo.php'; | |
| require 'Mensagem.php'; | |
| require 'phpMailer/class.phpmailer.php'; | |
| $tipo = new DefineTipoDeMensagem('emailAtualizacaoSenha'); | |
| $mensagem = new Mensagem($tipo); | |
| $mensagem->montaMensagem('Teste'); | |
| $mensagem->defineAssunto(); | |
| $mail = new PHPMailer(); | |
| $envio = new EnviarEmail('[email protected]', 'Eduardo', $mensagem, $mail); | |
| var_dump($envio->enviar()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment