expmail/Mail.class.php

100 lines
3.1 KiB
PHP
Raw Normal View History

<?php
require_once 'vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
class Mail {
private $mailer;
function __construct($host=null, $user=null, $password=null, $port=null) {
global $MAIL_HOST, $MAIL_USER, $MAIL_PASS, $MAIL_STARTTLS, $MAIL_SMTPS, $MAIL_PORT;
$this->mailer = new PHPMailer(true);
$this->mailer->isSMTP();
$this->mailer->Host = $host ?: $MAIL_HOST;
$this->mailer->SMTPAuth = (bool) $MAIL_USER;
$this->mailer->Username = $user ?: $MAIL_USER;
$this->mailer->Password = $password ?: $MAIL_PASS;
$this->mailer->SMTPSecure = $MAIL_STARTTLS ? PHPMailer::ENCRYPTION_STARTTLS : ($MAIL_SMTPS ? PHPMailer::ENCRYPTION_SMTPS : false);
$this->mailer->Port = $port ?: ($MAIL_PORT ?: ($MAIL_SMTPS ? 465 : 587));
$this->mailer->XMailer = "Kumi Systems Mailer 0.5 (https://kumi.systems/)";
}
function add_attachment($content, $filename=null, $cid=null) {
$this->mailer->addStringEmbeddedImage($content, $cid, $filename);
}
function add_attachments($attachments) {
foreach ($attachments as $attachment) {
$this->add_attachment($attachment["content"], $attachment["filename"], $attachment["cid"]);
}
}
function add_bcc($email, $name) {
$this->mailer->addBCC($email, $name);
}
function add_bccs($bccs) {
foreach ($bccs as $bcc) {
$this->add_bcc($bcc["email"], $bcc["name"]);
}
}
function add_cc($email, $name) {
$this->mailer->addCC($email, $name);
}
function add_ccs($ccs) {
foreach ($ccs as $cc) {
$this->add_cc($cc["email"], $cc["name"]);
}
}
function add_content($html=null, $text=null) {
$this->mailer->isHTML((bool) $html);
$this->mailer->Body = $html ?: $text;
$this->mailer->AltBody = $html ? $text : null;
}
function add_recipient($email, $name) {
$this->mailer->addAddress($email, $name);
}
function add_recipients($recipients) {
foreach ($recipients as $recipient) {
$this->add_recipient($recipient["email"], $recipient["name"]);
}
}
function allow_empty($boolean) {
$this->mailer->AllowEmpty = $boolean;
}
static function FromRequest($req) {
$mail = new self();
$mail->set_from($req->get_sender()["email"], $req->get_sender()["name"]);
$mail->add_recipients($req->get_recipients());
$mail->add_ccs($req->get_ccs());
$mail->add_bccs($req->get_bccs());
$html = $req->prepare_html($req->has_config("urlbeforestring"));
$text = $req->prepare_text(!$req->has_config("noconversion"), $req->has_config("urlbeforestring"));
$mail->add_content($html, $text);
$mail->add_attachments($req->get_attachments(true, true, $req->has_config("ignoredlfails")));
$mail->allow_empty($req->has_config("allowempty"));
return $mail;
}
function send() {
$this->mailer->send();
}
function set_from($email, $name) {
$this->mailer->setFrom($email, $name);
}
}