Advertisement
Guest User

Untitled

a guest
Feb 5th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 KB | None | 0 0
  1. <?php
  2.  
  3. class email {
  4.  
  5. private $status;
  6. private $erros;
  7.  
  8. public function __construct() {
  9.  
  10. $this->__setStatus(false);
  11. $this->__setErros(array());
  12. }
  13.  
  14. private function __setStatus($s = false) {
  15.  
  16. $this->status = (is_bool($s)) ? $s : false;
  17. }
  18.  
  19. private function __setErros($e = array()) {
  20.  
  21. $this->erros = (is_array($e)) ? $e : array();
  22. }
  23.  
  24. public function getStatus() {
  25.  
  26. return $this->status;
  27. }
  28.  
  29. public function getErros() {
  30.  
  31. return $this->erros;
  32. }
  33.  
  34. public function validarForm($dados = array()) {
  35.  
  36. $erros = $campos = array();
  37.  
  38. if (!(strlen(trim($_POST['name'])) > 0)) {
  39.  
  40. $campos[] = 'nome';
  41. }
  42.  
  43. if (!(strlen(trim($_POST['email'])) > 0)) {
  44.  
  45. $campos[] = 'email';
  46. }
  47.  
  48. if (!empty($campos)) {
  49.  
  50. $erros[] = 'Por favor, verifique o(s) campo(s) - ( ' . implode(',', $campos) . ' )';
  51. }
  52.  
  53. if ($this->__existUploads($files)) {
  54.  
  55. $count = count($files['tmp_name']);
  56.  
  57. $invalidos = array();
  58.  
  59. for ($i = 0; $i < $count; $i++) {
  60.  
  61. $size = $files['size'][$i];
  62.  
  63. if ($size > 2000000) {
  64.  
  65. $invalidos[] = $files['name'][$i];
  66. }
  67. }
  68.  
  69. if (!empty($invalidos)) {
  70.  
  71. $erros[] = 'Por favor, verifique o(s) arquivo(s) - ( ' . implode(',', $invalidos) . ' )';
  72. }
  73. }
  74.  
  75. $status = (empty($erros));
  76.  
  77. $this->__setStatus($status);
  78. $this->__setErros($erros);
  79.  
  80. return $erros;
  81. }
  82.  
  83. private function __criarPastaArquivos() {
  84.  
  85. if ($this->__existUploads($files)) {
  86.  
  87. $total = count($files['tmp_name']);
  88.  
  89. for ($i = 0; $i < $total; $i++) {
  90.  
  91. $dir = $this->__getFolderContatoPorEmail();
  92.  
  93. if (!is_dir($dir)) {
  94.  
  95. mkdir($dir);
  96. }
  97.  
  98. if (!file_exists($dir . '/' . $files["name"][$i])) {
  99.  
  100. move_uploaded_file($files["tmp_name"][$i], $dir . '/' . $files["name"][$i]);
  101. }
  102. }
  103. }
  104. }
  105.  
  106. private function __getFolderContatoPorEmail() {
  107.  
  108. return "../storage/contatos/" . $_POST['email'];
  109. }
  110.  
  111. private function __existUploads(&$files) {
  112.  
  113. $r = false;
  114.  
  115. $files = (isset($_FILES['files'])) ? $_FILES['files'] : array();
  116.  
  117. if (is_array($files) && !empty($files)) {
  118.  
  119. if (isset($files['tmp_name'])) {
  120.  
  121. $r = true;
  122. }
  123. }
  124.  
  125. return $r;
  126. }
  127.  
  128. public function enviar() {
  129.  
  130. require 'libs/PHPMailer/PHPMailerAutoload.php';
  131.  
  132. $this->__criarPastaArquivos();
  133.  
  134. $mail = new PHPMailer;
  135.  
  136. //$mail->SMTPDebug = 3; // Enable verbose debug output
  137. $mail->isSMTP(); // Set mailer to use SMTP
  138. $mail->Host = 'smtp.live.com'; // Specify main and backup SMTP servers
  139. $mail->SMTPAuth = true; // Enable SMTP authentication
  140. $mail->Username = 'xxx'; // SMTP username
  141. $mail->Password = 'xxx'; // SMTP password
  142. $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
  143. $mail->Port = 587; // TCP port to connect to
  144.  
  145. $mail->setFrom('xxx', 'Mailer');
  146. $mail->addAddress('xxx', 'Joe User'); // Add a recipient
  147.  
  148. //$mail->addReplyTo('info@example.com', 'Information');
  149. //$mail->addCC('cc@example.com');
  150. //$mail->addBCC('bcc@example.com');
  151.  
  152. if ($this->__existUploads($files)) {
  153.  
  154. $total = count($files['tmp_name']);
  155.  
  156. for ($i = 0; $i < $total; $i++) {
  157.  
  158. $dir = $this->__getFolderContatoPorEmail();
  159.  
  160. $dir .= "/" . $files["name"][$i];
  161.  
  162. $mail->addAttachment($dir);
  163. }
  164. }
  165.  
  166. $mail->isHTML(true);
  167.  
  168. $mail->Subject = $_POST['assunto'];
  169.  
  170. $html = " <div>"
  171. . " <strong>Contato: &nbsp; </strong>" . $_POST['email'] . "<br> "
  172. . " <strong>Data: &nbsp; </strong>" . date('d/m/Y H:i:s') . "<br> "
  173. . " <br><hr><br> "
  174. . " <fieldset>"
  175. . " <legend>Mensagem</legend> "
  176. . " <p>" . ((strlen(trim($_POST['mensagem'])) > 0) ? $_POST['mensagem'] : 'Nenhuma mensagem foi escrita') . "</p> "
  177. . " </fieldset> "
  178. . "</div>";
  179.  
  180. $mail->Body = $html;
  181. //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
  182.  
  183. $status = $mail->send(); //$mail->ErrorInfo;
  184.  
  185. $this->__setStatus($status);
  186. }
  187.  
  188. }
  189.  
  190. $email = new email();
  191.  
  192. $email->validarForm();
  193.  
  194. $status = $email->getStatus();
  195.  
  196. if ($status == true) {
  197.  
  198. $email->enviar();
  199.  
  200. $status = $email->getStatus();
  201.  
  202. echo json_encode(array(
  203. 'status' => $status,
  204. 'msg' => ($status == true) ? 'Eamil enviado com sucesso, aguarde o nosso retorno, Obrigado !' : 'Ocorreu um problema no momento do envio, tente novamente !',
  205. 'validacao' => false
  206. ));
  207. } else {
  208.  
  209. echo json_encode(array(
  210. 'status' => false,
  211. 'msgs' => $email->getErros(),
  212. 'validacao' => true
  213. ));
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement