Advertisement
Guest User

Untitled

a guest
May 4th, 2019
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. <?php
  2. require_once '../../vendor/autoload.php';
  3. use PHPMailer\PHPMailer\PHPMailer;
  4. use PHPMailer\PHPMailer\Exception;
  5.  
  6. Class Email{
  7.  
  8.  
  9. private $template = null;
  10. private $body = null;
  11. private $mail = null;
  12. private $assunto = null;
  13. private $emails = null;
  14.  
  15. public function __construct($template){
  16. $this->mail = new PHPMailer(true);
  17.  
  18. if(empty($template)){
  19. throw new Exception("O Template não pode ser vazio");
  20. }
  21. $this->template = file_get_contents($template);
  22.  
  23. }
  24.  
  25. private function array_keys($array){
  26.  
  27. return array_map(function($arr){
  28. return "%%{$arr}%%";
  29. }, array_keys($array));
  30.  
  31. }
  32.  
  33. private function array_values($array){
  34. return array_values($array);
  35. }
  36.  
  37. public function preencherTemplate($dados){
  38. $this->body = str_replace($this->array_keys($dados), $this->array_values($dados), $this->template);
  39. }
  40.  
  41. public function setDestinatarios($emails){
  42. $this->emails = $emails;
  43. }
  44.  
  45. public function setAssunto($assunto){
  46. $this->assunto = $assunto;
  47. }
  48.  
  49.  
  50. public function enviar(){
  51. if(empty($this->assunto)){
  52. throw new Exception("O Assunto não pode ser vazio");
  53. }
  54.  
  55. if(empty($this->emails)){
  56. throw new Exception("O E-mail não pode ser vazio");
  57. }
  58. $this->mail->Body = $this->body;
  59. $this->mail->SMTPDebug = 0;
  60. $this->mail->isSMTP();
  61. $this->mail->Host = 'host';
  62. $this->mail->SMTPAuth = true;
  63. $this->mail->Username = 'mail@mail.com.br';
  64. $this->mail->Password = 'passoword';
  65. $this->mail->SMTPSecure = 'tls';
  66. $this->mail->Port = 587;
  67.  
  68. $this->mail->SMTPOptions = array(
  69. 'ssl' => array(
  70. 'verify_peer' => false,
  71. 'verify_peer_name' => false,
  72. 'allow_self_signed' => true
  73. )
  74. );
  75. $this->mail->setFrom('email@mail.com.br', 'Nome');
  76. $this->mail->ClearAllRecipients();
  77. if(is_array($this->emails)){
  78. foreach($this->emails as $key => $value) {
  79. $this->mail->AddCC($value->email);
  80. }
  81. }else{
  82. $this->mail->AddCC($this->emails);
  83. }
  84. $this->mail->addReplyTo('email@mail.com.br');
  85. $this->mail->isHTML(true);
  86. $this->mail->CharSet = 'utf-8';
  87. $this->mail->Subject = $this->assunto;
  88. $enviado = $this->mail->Send();
  89. $this->mail->ClearAttachments();
  90.  
  91. if (!$enviado) {
  92. return $mail->ErrorInfo;
  93. }
  94. else {
  95. return true;
  96. }
  97. }
  98.  
  99.  
  100. }
  101. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement