Klaus-Uwe Mitterer
2eed78b83e
Adding two config options to modify behavior Allow messages with empty body to be sent Automatically convert HTML to plain if none provided Version bump to 0.4
95 lines
No EOL
2.9 KiB
PHP
95 lines
No EOL
2.9 KiB
PHP
<?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->AllowEmpty = true;
|
|
$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.1 (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"]);
|
|
}
|
|
}
|
|
|
|
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();
|
|
$text = $req->prepare_text(!$req->has_config("noconversion"));
|
|
$mail->add_content($html, $text);
|
|
|
|
$mail->add_attachments($req->get_attachments(true, true, $req->has_config("ignoredlfails")));
|
|
|
|
return $mail;
|
|
}
|
|
|
|
function send() {
|
|
$this->mailer->send();
|
|
}
|
|
|
|
function set_from($email, $name) {
|
|
$this->mailer->setFrom($email, $name);
|
|
}
|
|
} |