Advertisement
gustavotorregrosa

Untitled

Jul 5th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.22 KB | None | 0 0
  1. //NO CONTROLLER
  2. public function ciclomensagem(){
  3.  
  4. $nomeRemetente = addslashes($_POST['name']);
  5. $emailRemetente = addslashes($_POST['email']);
  6. $mensagem = addslashes($_POST['message']);
  7.  
  8.  
  9.  
  10. //(new Mensagem)->gravaMensagem($emailRemetente, $nomeRemetente, $mensagem);
  11.  
  12. //manda email
  13.  
  14. $resultado = (new mensagemModel)->enviaEmail($nomeRemetente, $emailRemetente, $mensagem);
  15.  
  16. if($resultado){
  17. Http::redirect_to('/?sucesso');
  18. }else{
  19. Http::redirect_to('/?erro');
  20. }
  21.  
  22.  
  23.  
  24.  
  25. }
  26.  
  27.  
  28. //MODEL
  29.  
  30. <?php
  31.  
  32. require 'vendor/phpmailer/class.phpmailer.php';
  33. require 'vendor/phpmailer/class.phpmaileroauth.php';
  34. require 'vendor/phpmailer/class.phpmaileroauthgoogle.php';
  35. require 'vendor/phpmailer/class.smtp.php';
  36. require 'vendor/phpmailer/class.pop3.php';
  37.  
  38. Class mensagemModel extends appModel {
  39.  
  40. public function __construct() {
  41. $this->initApp();
  42. }
  43.  
  44. public function gravarMensagem($nome, $emailremetente, $mensagem){
  45.  
  46. $sql = "insert into mensagem (nome, email, mensagem) values ('$nome', '$emailremetente', '$mensagem');";
  47.  
  48. $this->db->query($sql);
  49.  
  50. }
  51.  
  52. public function enviaEmail($nome, $emailremetente, $mensagem){
  53.  
  54. $infoMensagem = array(
  55. 'nome' => $nome,
  56. 'email' => $emailremetente,
  57. 'mensagem' => $mensagem
  58.  
  59. );
  60.  
  61.  
  62. $configuracoesEmailObj = (new emailModel)->getConfig();
  63.  
  64. //Cria params envio de email
  65. $meuMailer = new PHPMailer();
  66.  
  67.  
  68.  
  69.  
  70. $meuMailer->isSMTP();
  71. $meuMailer->Charset = 'UTF-8';
  72. $meuMailer->isHTML(true);
  73.  
  74.  
  75. if($configuracoesEmailObj->autenticacao === "sim"){
  76. $meuMailer->SMTPAuth = true;
  77. $meuMailer->SMTPSecure = "tls";
  78.  
  79.  
  80.  
  81. }
  82.  
  83.  
  84.  
  85. $nome_servidor = $configuracoesEmailObj->nome_servidor;
  86. $porta = $configuracoesEmailObj->porta;
  87.  
  88. $meuMailer->Host = $nome_servidor;
  89. $meuMailer->port = $porta;
  90.  
  91. $meuMailer->Username = $configuracoesEmailObj->username;
  92. $meuMailer->Password = $configuracoesEmailObj->senha;
  93. $meuMailer->AddReplyTo($infoMensagem['email'], $infoMensagem['nome']);
  94. $meuMailer->From = $configuracoesEmailObj->username;
  95. $meuMailer->FromName = $configuracoesEmailObj->nomeusuarioemissor;
  96. $meuMailer->Subject = $configuracoesEmailObj->assunto;
  97. $meuMailer->Body = $infoMensagem['mensagem'];
  98. $meuMailer->addAddress($configuracoesEmailObj->emailparaenvio);
  99. $meuMailer->addCC($configuracoesEmailObj->emailparacopia);
  100.  
  101. $meuMailer->Timeout = 120;
  102.  
  103. $resultado = $meuMailer->Send();
  104.  
  105. return $resultado;
  106.  
  107.  
  108. // if(!$resultado){
  109. // var_dump("houve erro");
  110. // }else{
  111. // var_dump("email enviado com sucesso");
  112. // }
  113.  
  114.  
  115. }
  116.  
  117.  
  118. static public function mail($data) {
  119. //$data['destinatario' => 'fulado@gmail.com', 'assunto' => 'teste', 'mensagem' => 'mensagem teste', 'copia' => '', 'responder' => ''];
  120. $host = new smtpModel();
  121. $mail = new PHPMailer();
  122. $mail->isSMTP();
  123. // $mail->SMTPSecure = 'tls';
  124. $mail->SMTPDebug = 0;
  125. $mail->SMTPAuth = true;
  126. $mail->Host = $host->__get('host');
  127. $mail->Port = $host->__get('port');
  128. $mail->Username = $host->__get('email');
  129. $mail->Password = $host->__get('pass');
  130. $mail->setFrom($host->__get('email'), utf8_decode($host->__get('nome')));
  131.  
  132.  
  133. if (isset($data['responder']) && !empty($data['responder'])) {
  134. $mail->addReplyTo($data['responder']);
  135. }
  136. if (isset($data['copia']) && is_array($data['copia'])) {
  137. foreach ($data['copia'] as $copia) {
  138. $mail->addBCC("$copia");
  139. }
  140. }
  141. $mail->addAddress("gustavo.torregrosa@gmail.com");
  142. $mail->Subject = utf8_decode("meu assunto");
  143. //$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
  144. $mail->msgHTML(utf8_decode($data['mensagem']));
  145.  
  146. //$mail->addAttachment('images/phpmailer_mini.png');
  147. if (!$mail->send()) {
  148. echo "Mailer Error: " . $mail->ErrorInfo;
  149. } else {
  150. return true;
  151. }
  152. }
  153.  
  154.  
  155.  
  156. static public function __runtest() {
  157.  
  158. self::enviaEmail("teste 123", "gustavo.torregrosa@gmail.com", "bla bla bla");
  159. }
  160.  
  161.  
  162. //
  163. //public function deletarImagem($id){
  164. // $this->db->query("delete from fundo where id = '$id';");
  165. //}
  166. //
  167. //public function get_all() {
  168. // $this->db->query = "SELECT * FROM fundo ORDER BY ordem ASC";
  169. // return $this->db->fetch();
  170. //}
  171. //
  172. //public function atualizaOrdem($ordem){
  173. //
  174. // for ($i = 1; $i < count($ordem); $i++) {
  175. //
  176. // $this->db->query("update fundo set ordem = $i where id = $ordem[$i];");
  177. // }
  178. //
  179. //
  180. //}
  181. //
  182. // public function get_by_id($id) {
  183. // $this->db->query = "SELECT * FROM fundo WHERE id = $id;";
  184. // return $this->db->fetch()[0];
  185. // }
  186. //
  187. //
  188. //
  189. //
  190. ////
  191. //// public function get_all($order = 'ASC') {
  192. //// $this->db->query = "SELECT * FROM usuario ORDER BY usuario_nome $order";
  193. //// return $this->db->fetch();
  194. //// }
  195. ////
  196. //// public function get_by_id($id) {
  197. //// $this->db->query = "SELECT * FROM usuario WHERE usuario_id = $id;";
  198. //// return $this->db->fetch()[0];
  199. //// }
  200. ////
  201. //// public function gravar($id = null) {
  202. //// $post = Post::query_build();
  203. //// if ($id > 0) {//atualiza
  204. //// $this->db->query("UPDATE usuario SET $post->sql_update WHERE usuario_id = $id;");
  205. //// } else {//cadastra
  206. //// $this->db->query("INSERT INTO usuario $post->sql_insert;");
  207. //// }
  208. //// }
  209. ////
  210. //// public function remove($id) {
  211. //// $this->db->query("DELETE FROM usuario WHERE usuario_id = $id;");
  212. //// }
  213.  
  214. public function __destruct() {
  215. //
  216. }
  217.  
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement