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

View File

@@ -1353,120 +1353,120 @@ function number_pad($number,$n) {
*/
include_once("resources/phpmailer/class.phpmailer.php");
include_once("resources/phpmailer/class.smtp.php");
try {
include_once("resources/phpmailer/class.phpmailer.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 -> IsSMTP();
if ($_SESSION['email']['smtp_hostname']['text'] != '') {
$mail -> Hostname = $_SESSION['email']['smtp_hostname']['text'];
}
$mail -> Host = $_SESSION['email']['smtp_host']['text'];
if (is_numeric($_SESSION['email']['smtp_port']['numeric'])) {
$mail -> Port = $_SESSION['email']['smtp_port']['numeric'];
}
if ($_SESSION['email']['smtp_auth']['text'] == "true") {
$mail -> SMTPAuth = $_SESSION['email']['smtp_auth']['text'];
$mail -> Username = $_SESSION['email']['smtp_username']['text'];
$mail -> Password = $_SESSION['email']['smtp_password']['text'];
}
else {
$mail -> SMTPAuth = 'false';
}
if ($_SESSION['email']['smtp_secure']['text'] == "none") {
$_SESSION['email']['smtp_secure']['text'] = '';
}
if ($_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") {
// bypass TLS certificate check e.g. for self-signed certificates
$mail -> SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
}
$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'];
$mail -> SetFrom($eml_from_address, $eml_from_name);
$mail -> AddReplyTo($eml_from_address, $eml_from_name);
$mail -> Subject = $eml_subject;
$mail -> MsgHTML($eml_body);
$mail -> Priority = $eml_priority;
if ($eml_read_confirmation) {
$mail -> AddCustomHeader('X-Confirm-Reading-To: '.$eml_from_address);
$mail -> AddCustomHeader('Return-Receipt-To: '.$eml_from_address);
$mail -> AddCustomHeader('Disposition-Notification-To: '.$eml_from_address);
}
if (is_numeric($eml_debug_level) && $eml_debug_level > 0) {
$mail -> SMTPDebug = $eml_debug_level;
}
$mail = new PHPMailer();
$mail->IsSMTP();
if ($_SESSION['email']['smtp_hostname']['text'] != '') {
$mail->Hostname = $_SESSION['email']['smtp_hostname']['text'];
}
$mail->Host = $_SESSION['email']['smtp_host']['text'];
if (is_numeric($_SESSION['email']['smtp_port']['numeric'])) {
$mail->Port = $_SESSION['email']['smtp_port']['numeric'];
}
if ($_SESSION['email']['smtp_auth']['text'] == "true") {
$mail->SMTPAuth = $_SESSION['email']['smtp_auth']['text'];
$mail->Username = $_SESSION['email']['smtp_username']['text'];
$mail->Password = $_SESSION['email']['smtp_password']['text'];
}
else {
$mail->SMTPAuth = 'false';
}
if ($_SESSION['email']['smtp_secure']['text'] == "none") {
$_SESSION['email']['smtp_secure']['text'] = '';
}
if ($_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") {
// bypass TLS certificate check e.g. for self-signed certificates
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
}
$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'];
$mail->SetFrom($eml_from_address, $eml_from_name);
$mail->AddReplyTo($eml_from_address, $eml_from_name);
$mail->Subject = $eml_subject;
$mail->MsgHTML($eml_body);
$mail->Priority = $eml_priority;
if ($eml_read_confirmation) {
$mail->AddCustomHeader('X-Confirm-Reading-To: '.$eml_from_address);
$mail->AddCustomHeader('Return-Receipt-To: '.$eml_from_address);
$mail->AddCustomHeader('Disposition-Notification-To: '.$eml_from_address);
}
if (is_numeric($eml_debug_level) && $eml_debug_level > 0) {
$mail->SMTPDebug = $eml_debug_level;
}
$address_found = false;
$address_found = false;
if (!is_array($eml_recipients)) { // must be a single or delimited recipient address(s)
$eml_recipients = str_replace(' ', '', $eml_recipients);
$eml_recipients = str_replace(array(';',','), ' ', $eml_recipients);
$eml_recipients = explode(' ', $eml_recipients); // convert to array of addresses
}
foreach ($eml_recipients as $eml_recipient) {
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
switch ($eml_recipient["delivery"]) {
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;
default : $mail -> AddAddress($eml_recipient["address"], ($eml_recipient["name"]) ? $eml_recipient["name"] : $eml_recipient["address"]);
if (!is_array($eml_recipients)) { // must be a single or delimited recipient address(s)
$eml_recipients = str_replace(' ', '', $eml_recipients);
$eml_recipients = str_replace(array(';',','), ' ', $eml_recipients);
$eml_recipients = explode(' ', $eml_recipients); // convert to array of addresses
}
foreach ($eml_recipients as $eml_recipient) {
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
switch ($eml_recipient["delivery"]) {
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;
default : $mail->AddAddress($eml_recipient["address"], ($eml_recipient["name"]) ? $eml_recipient["name"] : $eml_recipient["address"]);
}
$address_found = true;
}
}
else if ($eml_recipient != '' && preg_match($regexp, $eml_recipient) == 1) { // check if recipient value is simply (only) an address
$mail->AddAddress($eml_recipient);
$address_found = true;
}
}
else if ($eml_recipient != '' && preg_match($regexp, $eml_recipient) == 1) { // check if recipient value is simply (only) an address
$mail -> AddAddress($eml_recipient);
$address_found = true;
if (!$address_found) {
$eml_error = "No valid e-mail address provided.";
return false;
}
}
if (!$address_found) {
$eml_error = "No valid e-mail address provided.";
return false;
}
if (is_array($eml_attachments) && sizeof($eml_attachments) > 0) {
foreach ($eml_attachments as $attachment) {
$attachment['name'] = $attachment['name'] != '' ? $attachment['name'] : basename($attachment['value']);
if ($attachment['type'] == 'file' || $attachment['type'] == 'path') {
$mail -> AddAttachment($attachment['value'], $attachment['name']);
}
else if ($attachment['type'] == 'string') {
if (base64_encode(base64_decode($attachment['value'], true)) === $attachment['value']) {
$mail -> AddStringAttachment(base64_decode($attachment['value']), $attachment['name']);
if (is_array($eml_attachments) && sizeof($eml_attachments) > 0) {
foreach ($eml_attachments as $attachment) {
$attachment['name'] = $attachment['name'] != '' ? $attachment['name'] : basename($attachment['value']);
if ($attachment['type'] == 'file' || $attachment['type'] == 'path') {
$mail->AddAttachment($attachment['value'], $attachment['name']);
}
else {
$mail -> AddStringAttachment($attachment['value'], $attachment['name']);
else if ($attachment['type'] == 'string') {
if (base64_encode(base64_decode($attachment['value'], true)) === $attachment['value']) {
$mail->AddStringAttachment(base64_decode($attachment['value']), $attachment['name']);
}
else {
$mail->AddStringAttachment($attachment['value'], $attachment['name']);
}
}
}
}
}
if (!$mail -> Send()) {
$eml_error = $mail -> ErrorInfo;
return false;
}
else {
//send the email
$mail->Send();
$mail->ClearAddresses();
$mail->SmtpClose();
unset($mail);
return true;
}
catch (Exception $e) {
$eml_error = $mail->ErrorInfo;
return false;
}
$mail -> ClearAddresses();
$mail -> SmtpClose();
unset($mail);
}
}