Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App;
- class Mail
- {
- protected $mysqli;
- protected $mailer;
- protected $config = [];
- protected $admin = false;
- protected $casamadre = 0;
- protected $licenziatario = 0;
- protected $success = [];
- protected $to = [];
- public function __construct($mysqli)
- {
- $this->mysqli = $mysqli;
- $this->config = config('mail');
- $this->mailer = $this->mailConnection();
- $this->setFrom($this->config["from-mail"],$this->config["from-name"]);
- return $this;
- }
- public function admin($param = true)
- {
- $this->admin = $param;
- return $this;
- }
- public function casamadre($param = 0)
- {
- $this->casamadre = $param;
- return $this;
- }
- public function licenziatario($param = 0)
- {
- $this->licenziatario = $param;
- return $this;
- }
- public function send()
- {
- if(! $this->admin && $this->bloccoAdmin())
- {
- return true;
- }
- while(!empty($this->to))
- {
- $recipient = array_pop($this->to);
- $this->mailer->clearAllRecipients();
- $this->mailer->addAddress($recipient['mail'], $recipient['name']);
- if(config('app')['debug'])
- {
- $this->mailer->preSend();
- file_put_contents("log/mail/" . date("Y-m-d") . ".log", $this->mailer->getSentMIMEMessage(), FILE_APPEND);
- array_push($this->success, array("mail" => $recipient['mail'], "name" => $recipient['name']));
- }
- elseif($this->mailer->send())
- {
- array_push($this->success, array("mail" => $recipient['mail'], "name" => $recipient['name']));
- }
- }
- $this->log();
- return true;
- }
- public function setFrom($mail, $name = null)
- {
- $this->mailer->From = $mail;
- if($name)
- {
- $this->mailer->FromName = $name;
- }
- }
- public function setTo($mail, $name = '')
- {
- if(func_num_args() == 1 && is_array($mail))
- {
- foreach ($mail as $index => $row)
- {
- $this->setTo($row['mail'], $row['name'] ?? '');
- }
- return $this;
- }
- array_push($this->to, ["mail" => $mail, "name" => $name]);
- return $this;
- }
- public function setAttachment($path, $name = '')
- {
- if(func_num_args() == 1 && is_array($path))
- {
- foreach($path as $index => $row)
- {
- $this->setAttachment($row['path'], $row['name'] ?? '');
- }
- return $this;
- }
- $this->mailer->addAttachment($path, $name);
- return $this;
- }
- public function setSubject($string)
- {
- $this->mailer->Subject = $string;
- return $this;
- }
- public function setBody($string)
- {
- $this->mailer->msgHTML($string);
- return $this;
- }
- protected function mailConnection()
- {
- $mailer = new \PHPMailer;
- $mailer->isSMTP();
- $mailer->Host = $this->config['host'];
- $mailer->SMTPAuth = true;
- $mailer->Username = $this->config['username'];
- $mailer->Password = $this->config['password'];
- $mailer->SMTPSecure = $this->config['method'];
- $mailer->Port = $this->config['port'];
- $mailer->CharSet = 'UTF-8';
- $mailer->isHTML(true);
- return $mailer;
- }
- protected function bloccoAdmin()
- {
- $q = "SELECT flag FROM impostazioni WHERE id_impostazione = 18";
- $e = $this->mysqli->query($q);
- $r = $e->fetch_assoc();
- return $r['flag'] == 1;
- }
- protected function log()
- {
- if(count($this->success) > 0)
- {
- $oggetto = $this->mysqli->real_escape_string($this->mailer->Subject);
- $testo = $this->mysqli->real_escape_string($this->mailer->Body);
- # CREO LA TESTATA
- $sql = "INSERT INTO storico_mail (oggetto,testo,id_casa_madre,id_licenziatario)
- VALUES ('$oggetto','$testo','$this->casamadre','$this->licenziatario') ";
- $this->mysqli->query($sql);
- $id_testata = $this->mysqli->insert_id;
- $stmt = $this->mysqli->prepare("INSERT INTO storico_mail_indirizzi (id_storico_mail,email,nome) VALUES (?,?,?)");
- $stmt->bind_param("iss", $id_testata, $address, $alias);
- foreach ($this->success as $index => $row)
- {
- $alias = (isset($row['name']))? $this->mysqli->real_escape_string($row['name']) : '';
- $address = $row['mail'];
- $stmt->execute();
- }
- $allegati = $this->mailer->getAttachments();
- if(!empty($allegati))
- {
- $stmt = $this->mysqli->prepare("INSERT INTO storico_mail_allegati (id_storico_mail,nome_file,nome_utente) VALUES (?,?,?)");
- $stmt->bind_param("iss", $id_testata, $filename, $alias);
- foreach ($allegati as $index => $row)
- {
- $alias = $this->mysqli->real_escape_string($row[2]);
- $filename = $row[1];
- $stmt->execute();
- $id_allegato = $stmt->insert_id;
- $estensione = trovaEstensione($row[0]);
- $nuovonome = creaHash();
- $nuovonome = $id_allegato."_".$nuovonome.".".$estensione;
- copy($row[0], __DIR__ . "/../media/storico_mail_allegati/".$nuovonome);
- $sql = "UPDATE storico_mail_allegati SET nome_file = '$nuovonome' WHERE id = '$id_allegato' ";
- $this->mysqli->query($sql);
- }
- }
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement