Avoid PHPMailer boiler plate repetition

This commit is contained in:
2021-08-30 21:27:07 +02:00
parent 0da78509a7
commit 1b02d26fe0

View File

@@ -44,23 +44,23 @@ class Email extends PhpObject {
}
public function send() {
$oPHPMailer = new PHPMailer(true);
//Server settings
if(Settings::DEBUG) $oPHPMailer->SMTPDebug = SMTP::DEBUG_SERVER;//Enable verbose debug output
$oPHPMailer->isSMTP(); //Send using SMTP
$oPHPMailer->CharSet = Settings::TEXT_ENC; //Mail Character Set
$oPHPMailer->Encoding = 'base64'; //Base 64 Character Encoding
$oPHPMailer->Host = Settings::MAIL_SERVER; //Set the SMTP server to send through
$oPHPMailer->SMTPAuth = true; //Enable SMTP authentication
$oPHPMailer->Username = Settings::MAIL_USER; //SMTP username
$oPHPMailer->Password = Settings::MAIL_PASS; //SMTP password
$oPHPMailer->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; //Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$oPHPMailer->Port = 587; //TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
$oPHPMailer->setFrom(Settings::MAIL_FROM, 'Spotty');
$oPHPMailer->addReplyTo(Settings::MAIL_FROM, 'Spotty');
foreach($this->asDests as $asDest) {
$oPHPMailer = new PHPMailer(true);
//Server settings
if(Settings::DEBUG) $oPHPMailer->SMTPDebug = SMTP::DEBUG_SERVER;//Enable verbose debug output
$oPHPMailer->isSMTP(); //Send using SMTP
$oPHPMailer->CharSet = Settings::TEXT_ENC; //Mail Character Set
$oPHPMailer->Encoding = 'base64'; //Base 64 Character Encoding
$oPHPMailer->Host = Settings::MAIL_SERVER; //Set the SMTP server to send through
$oPHPMailer->SMTPAuth = true; //Enable SMTP authentication
$oPHPMailer->Username = Settings::MAIL_USER; //SMTP username
$oPHPMailer->Password = Settings::MAIL_PASS; //SMTP password
$oPHPMailer->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; //Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$oPHPMailer->Port = 587; //TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
$oPHPMailer->setFrom(Settings::MAIL_FROM, 'Spotty');
$oPHPMailer->addReplyTo(Settings::MAIL_FROM, 'Spotty');
//Message
$this->oTemplate->setLanguage($asDest['language'], Spot::DEFAULT_LANG);
$this->oTemplate->setTimezone($asDest['timezone']);
@@ -77,7 +77,12 @@ class Email extends PhpObject {
$sPlainMessage = strip_tags(str_replace('<br />', "\n", $sHtmlMessage));
//Recipients
$oPHPMailer->addAddress($asDest['email'], $asDest['name']);
try {
$oPHPMailer->addAddress($asDest['email'], $asDest['name']);
} catch (Exception $oError) {
$this->addError('Invalid address skipped: '.$asDest['email'].' ('.$asDest['name'].')');
continue;
}
//Content
$oPHPMailer->isHTML(true);
@@ -85,13 +90,18 @@ class Email extends PhpObject {
$oPHPMailer->Body = $sHtmlMessage;
$oPHPMailer->AltBody = $sPlainMessage;
$bSuccess = true;
try {
$oPHPMailer->send();
$bSuccess = $bSuccess && $oPHPMailer->send();
}
catch (Exception $e) {
catch (Exception $oError) {
$this->addError('Message could not be sent to "'.$asDest['email'].'". Mailer Error: '.$oPHPMailer->ErrorInfo);
$oPHPMailer->getSMTPInstance()->reset();
}
$oPHPMailer->clearAddresses();
$oPHPMailer->clearCustomHeaders();
}
return $bSuccess;
}
private static function getTimeZoneCity($sTimeZone) {