Use a try catch block in the send_email function.

This commit is contained in:
FusionPBX
2021-04-12 12:45:59 -06:00
committed by GitHub
parent def4972da0
commit e19c1d2b01
+37 -37
View File
@@ -1353,37 +1353,38 @@ function number_pad($number,$n) {
*/ */
try {
include_once("resources/phpmailer/class.phpmailer.php"); include_once("resources/phpmailer/class.phpmailer.php");
include_once("resources/phpmailer/class.smtp.php"); include_once("resources/phpmailer/class.smtp.php");
$regexp = '/^[A-z0-9][\w.-]*@[A-z0-9][\w\-\.]+\.[A-z0-9]{2,7}$/'; $regexp = '/^[A-z0-9][\w.-]*@[A-z0-9][\w\-\.]+\.[A-z0-9]{2,7}$/';
$mail = new PHPMailer(); $mail = new PHPMailer();
$mail -> IsSMTP(); $mail->IsSMTP();
if ($_SESSION['email']['smtp_hostname']['text'] != '') { if ($_SESSION['email']['smtp_hostname']['text'] != '') {
$mail -> Hostname = $_SESSION['email']['smtp_hostname']['text']; $mail->Hostname = $_SESSION['email']['smtp_hostname']['text'];
} }
$mail -> Host = $_SESSION['email']['smtp_host']['text']; $mail->Host = $_SESSION['email']['smtp_host']['text'];
if (is_numeric($_SESSION['email']['smtp_port']['numeric'])) { if (is_numeric($_SESSION['email']['smtp_port']['numeric'])) {
$mail -> Port = $_SESSION['email']['smtp_port']['numeric']; $mail->Port = $_SESSION['email']['smtp_port']['numeric'];
} }
if ($_SESSION['email']['smtp_auth']['text'] == "true") { if ($_SESSION['email']['smtp_auth']['text'] == "true") {
$mail -> SMTPAuth = $_SESSION['email']['smtp_auth']['text']; $mail->SMTPAuth = $_SESSION['email']['smtp_auth']['text'];
$mail -> Username = $_SESSION['email']['smtp_username']['text']; $mail->Username = $_SESSION['email']['smtp_username']['text'];
$mail -> Password = $_SESSION['email']['smtp_password']['text']; $mail->Password = $_SESSION['email']['smtp_password']['text'];
} }
else { else {
$mail -> SMTPAuth = 'false'; $mail->SMTPAuth = 'false';
} }
if ($_SESSION['email']['smtp_secure']['text'] == "none") { if ($_SESSION['email']['smtp_secure']['text'] == "none") {
$_SESSION['email']['smtp_secure']['text'] = ''; $_SESSION['email']['smtp_secure']['text'] = '';
} }
if ($_SESSION['email']['smtp_secure']['text'] != '') { if ($_SESSION['email']['smtp_secure']['text'] != '') {
$mail -> SMTPSecure = $_SESSION['email']['smtp_secure']['text']; $mail->SMTPSecure = $_SESSION['email']['smtp_secure']['text'];
} }
if (isset($_SESSION['email']['smtp_validate_certificate']) && $_SESSION['email']['smtp_validate_certificate']['boolean'] == "false") { if (isset($_SESSION['email']['smtp_validate_certificate']) && $_SESSION['email']['smtp_validate_certificate']['boolean'] == "false") {
// bypass TLS certificate check e.g. for self-signed certificates // bypass TLS certificate check e.g. for self-signed certificates
$mail -> SMTPOptions = array( $mail->SMTPOptions = array(
'ssl' => array( 'ssl' => array(
'verify_peer' => false, 'verify_peer' => false,
'verify_peer_name' => false, 'verify_peer_name' => false,
@@ -1393,18 +1394,18 @@ function number_pad($number,$n) {
} }
$eml_from_address = ($eml_from_address != '') ? $eml_from_address : $_SESSION['email']['smtp_from']['text']; $eml_from_address = ($eml_from_address != '') ? $eml_from_address : $_SESSION['email']['smtp_from']['text'];
$eml_from_name = ($eml_from_name != '') ? $eml_from_name : $_SESSION['email']['smtp_from_name']['text']; $eml_from_name = ($eml_from_name != '') ? $eml_from_name : $_SESSION['email']['smtp_from_name']['text'];
$mail -> SetFrom($eml_from_address, $eml_from_name); $mail->SetFrom($eml_from_address, $eml_from_name);
$mail -> AddReplyTo($eml_from_address, $eml_from_name); $mail->AddReplyTo($eml_from_address, $eml_from_name);
$mail -> Subject = $eml_subject; $mail->Subject = $eml_subject;
$mail -> MsgHTML($eml_body); $mail->MsgHTML($eml_body);
$mail -> Priority = $eml_priority; $mail->Priority = $eml_priority;
if ($eml_read_confirmation) { if ($eml_read_confirmation) {
$mail -> AddCustomHeader('X-Confirm-Reading-To: '.$eml_from_address); $mail->AddCustomHeader('X-Confirm-Reading-To: '.$eml_from_address);
$mail -> AddCustomHeader('Return-Receipt-To: '.$eml_from_address); $mail->AddCustomHeader('Return-Receipt-To: '.$eml_from_address);
$mail -> AddCustomHeader('Disposition-Notification-To: '.$eml_from_address); $mail->AddCustomHeader('Disposition-Notification-To: '.$eml_from_address);
} }
if (is_numeric($eml_debug_level) && $eml_debug_level > 0) { if (is_numeric($eml_debug_level) && $eml_debug_level > 0) {
$mail -> SMTPDebug = $eml_debug_level; $mail->SMTPDebug = $eml_debug_level;
} }
$address_found = false; $address_found = false;
@@ -1418,15 +1419,15 @@ function number_pad($number,$n) {
if (is_array($eml_recipient)) { // check if each recipient has multiple fields if (is_array($eml_recipient)) { // check if each recipient has multiple fields
if ($eml_recipient["address"] != '' && preg_match($regexp, $eml_recipient["address"]) == 1) { // check if valid address if ($eml_recipient["address"] != '' && preg_match($regexp, $eml_recipient["address"]) == 1) { // check if valid address
switch ($eml_recipient["delivery"]) { switch ($eml_recipient["delivery"]) {
case "cc" : $mail -> AddCC($eml_recipient["address"], ($eml_recipient["name"]) ? $eml_recipient["name"] : $eml_recipient["address"]); break; case "cc" : $mail->AddCC($eml_recipient["address"], ($eml_recipient["name"]) ? $eml_recipient["name"] : $eml_recipient["address"]); break;
case "bcc" : $mail -> AddBCC($eml_recipient["address"], ($eml_recipient["name"]) ? $eml_recipient["name"] : $eml_recipient["address"]); break; case "bcc" : $mail->AddBCC($eml_recipient["address"], ($eml_recipient["name"]) ? $eml_recipient["name"] : $eml_recipient["address"]); break;
default : $mail -> AddAddress($eml_recipient["address"], ($eml_recipient["name"]) ? $eml_recipient["name"] : $eml_recipient["address"]); default : $mail->AddAddress($eml_recipient["address"], ($eml_recipient["name"]) ? $eml_recipient["name"] : $eml_recipient["address"]);
} }
$address_found = true; $address_found = true;
} }
} }
else if ($eml_recipient != '' && preg_match($regexp, $eml_recipient) == 1) { // check if recipient value is simply (only) an address else if ($eml_recipient != '' && preg_match($regexp, $eml_recipient) == 1) { // check if recipient value is simply (only) an address
$mail -> AddAddress($eml_recipient); $mail->AddAddress($eml_recipient);
$address_found = true; $address_found = true;
} }
} }
@@ -1436,37 +1437,36 @@ function number_pad($number,$n) {
return false; return false;
} }
if (is_array($eml_attachments) && sizeof($eml_attachments) > 0) { if (is_array($eml_attachments) && sizeof($eml_attachments) > 0) {
foreach ($eml_attachments as $attachment) { foreach ($eml_attachments as $attachment) {
$attachment['name'] = $attachment['name'] != '' ? $attachment['name'] : basename($attachment['value']); $attachment['name'] = $attachment['name'] != '' ? $attachment['name'] : basename($attachment['value']);
if ($attachment['type'] == 'file' || $attachment['type'] == 'path') { if ($attachment['type'] == 'file' || $attachment['type'] == 'path') {
$mail -> AddAttachment($attachment['value'], $attachment['name']); $mail->AddAttachment($attachment['value'], $attachment['name']);
} }
else if ($attachment['type'] == 'string') { else if ($attachment['type'] == 'string') {
if (base64_encode(base64_decode($attachment['value'], true)) === $attachment['value']) { if (base64_encode(base64_decode($attachment['value'], true)) === $attachment['value']) {
$mail -> AddStringAttachment(base64_decode($attachment['value']), $attachment['name']); $mail->AddStringAttachment(base64_decode($attachment['value']), $attachment['name']);
} }
else { else {
$mail -> AddStringAttachment($attachment['value'], $attachment['name']); $mail->AddStringAttachment($attachment['value'], $attachment['name']);
} }
} }
} }
} }
if (!$mail -> Send()) { //send the email
$eml_error = $mail -> ErrorInfo; $mail->Send();
$mail->ClearAddresses();
$mail->SmtpClose();
unset($mail);
return true;
}
catch (Exception $e) {
$eml_error = $mail->ErrorInfo;
return false; return false;
} }
else {
return true;
}
$mail -> ClearAddresses();
$mail -> SmtpClose();
unset($mail);
} }
} }