Advertisement
Guest User

Mail class

a guest
Jan 11th, 2017
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.81 KB | None | 0 0
  1. <?php
  2. namespace App;
  3.  
  4. class Mail
  5. {
  6.     protected $mysqli;
  7.     protected $mailer;
  8.  
  9.     protected $config = [];
  10.     protected $admin = false;
  11.     protected $casamadre = 0;
  12.     protected $licenziatario = 0;
  13.  
  14.     protected $success = [];
  15.     protected $to = [];
  16.  
  17.     public function __construct($mysqli)
  18.     {
  19.         $this->mysqli = $mysqli;
  20.         $this->config = config('mail');
  21.  
  22.         $this->mailer = $this->mailConnection();
  23.         $this->setFrom($this->config["from-mail"],$this->config["from-name"]);
  24.  
  25.         return $this;
  26.     }
  27.     public function admin($param = true)
  28.     {
  29.         $this->admin = $param;
  30.  
  31.         return $this;
  32.     }
  33.     public function casamadre($param = 0)
  34.     {
  35.         $this->casamadre = $param;
  36.  
  37.         return $this;
  38.     }
  39.     public function licenziatario($param = 0)
  40.     {
  41.         $this->licenziatario = $param;
  42.  
  43.         return $this;
  44.     }
  45.     public function send()
  46.     {
  47.         if(! $this->admin && $this->bloccoAdmin())
  48.         {
  49.             return true;
  50.         }
  51.  
  52.         while(!empty($this->to))
  53.         {
  54.             $recipient = array_pop($this->to);
  55.  
  56.             $this->mailer->clearAllRecipients();
  57.             $this->mailer->addAddress($recipient['mail'], $recipient['name']);
  58.  
  59.             if(config('app')['debug'])
  60.             {
  61.                 $this->mailer->preSend();
  62.                 file_put_contents("log/mail/" . date("Y-m-d") . ".log", $this->mailer->getSentMIMEMessage(), FILE_APPEND);
  63.  
  64.                 array_push($this->success, array("mail" => $recipient['mail'], "name" => $recipient['name']));
  65.             }
  66.             elseif($this->mailer->send())
  67.             {
  68.                 array_push($this->success, array("mail" => $recipient['mail'], "name" => $recipient['name']));
  69.             }
  70.         }
  71.  
  72.         $this->log();
  73.        
  74.         return true;
  75.     }
  76.     public function setFrom($mail, $name = null)
  77.     {
  78.         $this->mailer->From = $mail;
  79.         if($name)
  80.         {
  81.             $this->mailer->FromName = $name;
  82.         }
  83.     }
  84.     public function setTo($mail, $name = '')
  85.     {  
  86.         if(func_num_args() == 1 && is_array($mail))
  87.         {
  88.             foreach ($mail as $index => $row)
  89.             {
  90.                 $this->setTo($row['mail'], $row['name'] ?? '');
  91.             }
  92.  
  93.             return $this;
  94.         }
  95.        
  96.         array_push($this->to, ["mail" => $mail, "name" => $name]);
  97.  
  98.         return $this;
  99.     }
  100.     public function setAttachment($path, $name = '')
  101.     {
  102.         if(func_num_args() == 1 && is_array($path))
  103.         {
  104.             foreach($path as $index => $row)
  105.             {
  106.                 $this->setAttachment($row['path'], $row['name'] ?? '');
  107.             }
  108.  
  109.             return $this;
  110.         }
  111.        
  112.         $this->mailer->addAttachment($path, $name);
  113.  
  114.         return $this;
  115.     }
  116.     public function setSubject($string)
  117.     {
  118.         $this->mailer->Subject = $string;
  119.  
  120.         return $this;
  121.     }
  122.     public function setBody($string)
  123.     {
  124.         $this->mailer->msgHTML($string);
  125.  
  126.         return $this;
  127.     }
  128.     protected function mailConnection()
  129.     {
  130.         $mailer = new \PHPMailer;
  131.         $mailer->isSMTP();
  132.         $mailer->Host = $this->config['host'];
  133.         $mailer->SMTPAuth = true;
  134.         $mailer->Username = $this->config['username'];
  135.         $mailer->Password = $this->config['password'];
  136.         $mailer->SMTPSecure = $this->config['method'];
  137.         $mailer->Port = $this->config['port'];
  138.  
  139.         $mailer->CharSet = 'UTF-8';
  140.         $mailer->isHTML(true);
  141.  
  142.         return $mailer;
  143.     }
  144.     protected function bloccoAdmin()
  145.     {
  146.         $q = "SELECT flag FROM impostazioni WHERE id_impostazione = 18";
  147.         $e = $this->mysqli->query($q);
  148.         $r = $e->fetch_assoc();
  149.  
  150.         return $r['flag'] == 1;
  151.     }
  152.     protected function log()
  153.     {
  154.         if(count($this->success) > 0)
  155.         {
  156.             $oggetto = $this->mysqli->real_escape_string($this->mailer->Subject);
  157.             $testo = $this->mysqli->real_escape_string($this->mailer->Body);
  158.  
  159.                 # CREO LA TESTATA
  160.             $sql = "INSERT INTO storico_mail (oggetto,testo,id_casa_madre,id_licenziatario)
  161.                     VALUES ('$oggetto','$testo','$this->casamadre','$this->licenziatario') ";
  162.             $this->mysqli->query($sql);
  163.             $id_testata = $this->mysqli->insert_id;
  164.  
  165.             $stmt = $this->mysqli->prepare("INSERT INTO storico_mail_indirizzi (id_storico_mail,email,nome) VALUES (?,?,?)");
  166.             $stmt->bind_param("iss", $id_testata, $address, $alias);
  167.  
  168.             foreach ($this->success as $index => $row)
  169.             {          
  170.                 $alias = (isset($row['name']))? $this->mysqli->real_escape_string($row['name']) : '';
  171.                 $address = $row['mail'];
  172.  
  173.                 $stmt->execute();
  174.             }
  175.  
  176.             $allegati = $this->mailer->getAttachments();
  177.             if(!empty($allegati))
  178.             {
  179.                 $stmt = $this->mysqli->prepare("INSERT INTO storico_mail_allegati (id_storico_mail,nome_file,nome_utente) VALUES (?,?,?)");
  180.                 $stmt->bind_param("iss", $id_testata, $filename, $alias);
  181.  
  182.                 foreach ($allegati as $index => $row)
  183.                 {
  184.                     $alias = $this->mysqli->real_escape_string($row[2]);
  185.                     $filename = $row[1];
  186.  
  187.                     $stmt->execute();
  188.  
  189.                     $id_allegato = $stmt->insert_id;
  190.  
  191.                     $estensione = trovaEstensione($row[0]);
  192.                     $nuovonome = creaHash();
  193.                     $nuovonome = $id_allegato."_".$nuovonome.".".$estensione;
  194.  
  195.                     copy($row[0], __DIR__ . "/../media/storico_mail_allegati/".$nuovonome);
  196.  
  197.                     $sql = "UPDATE storico_mail_allegati SET nome_file = '$nuovonome' WHERE id = '$id_allegato' ";
  198.                     $this->mysqli->query($sql);
  199.                 }
  200.             }
  201.         }
  202.     }
  203. }
  204.  
  205. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement