fromaddress = $address; if (empty($name)) { $this->fromname = $address; } else { $this->fromname = $name; } } public function setSubject(string $subj) { $this->subject = $subj; } public function setBody(string $body) { $this->body = $body; } public function addAttachment(string $filename, string $name = "") { $this->attachments[$filename] = $name; } public function addTo(string $email) { $this->to[] = $email; } public function rmTo() { $this->to = []; } /** * Set the SMTP configuration. * * @param string $host smtp.example.com * @param int $port 587 * @param bool $auth true * @param string $user * @param string $pass * @param string $security tls */ public function setSMTP(string $host = "smtp.example.com", int $port = 587, bool $auth = true, string $user = "", string $pass = "", string $security = "tls") { $this->smtphost = $host; $this->smtpport = $port; $this->smtpauth = $auth; $this->smtpusername = $user; $this->smtppassword = $pass; $this->smtpsecurity = $security; } public function send() { $mail = new PHPMailer(true); $mail->isSMTP(); $mail->Host = $this->smtphost; $mail->SMTPAuth = $this->smtpauth; if ($this->smtpauth) { $mail->Username = $this->smtpusername; $mail->Password = $this->smtppassword; } if ($this->smtpsecurity != "none") { $mail->SMTPSecure = $this->smtpsecurity; } $mail->Port = $this->smtpport; $mail->isHTML(false); $mail->setFrom($this->fromaddress, $this->fromname); foreach ($this->to as $to) { $mail->addAddress($to); } $mail->Subject = $this->subject; $mail->Body = $this->body; foreach ($this->attachments as $file => $name) { if (empty($name)) { $mail->addAttachment($file); } else { $mail->addAttachment($file, $name); } } $mail->send(); } }