SMTPDebug = SMTP::DEBUG_OFF; //Tell PHPMailer to use SMTP $mailer->isSMTP(); //Set the hostname of the mail server $mailer->Host = $config['smtp']['host']; //Set the SMTP port number: // - 465 for SMTP with implicit TLS, a.k.a. RFC8314 SMTPS or // - 587 for SMTP+STARTTLS $mailer->Port = $config['smtp']['port']; //Set the encryption mechanism to use: // - SMTPS (implicit TLS on port 465) or // - STARTTLS (explicit TLS on port 587) $mailer->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; //Whether to use SMTP authentication $mailer->SMTPAuth = true; //Set AuthType to use XOAUTH2 $mailer->AuthType = 'XOAUTH2'; //Create a new OAuth2 provider instance $provider = new Azure( [ 'clientId' => $config['smtp']['clientId'], 'clientSecret' => $config['smtp']['clientSecret'], 'tenantId' => $config['smtp']['tenantId'], ] ); //Pass the OAuth provider instance to PHPMailer $mailer->setOAuth( new OAuth( [ 'provider' => $provider, 'clientId' => $config['smtp']['clientId'], 'clientSecret' => $config['smtp']['clientSecret'], 'refreshToken' => $config['smtp']['refreshToken'], 'userName' => $config['smtp']['email'], ] ) ); $mailer->From = $config['smtp']['email']; return $mailer; } /** * @param string $html * @param string $subject * @param array $recipients * @param array $attachments * @param string $fromEmail * @param string $fromName * @return bool * @throws Exception * @throws cDbException * @throws cException * @throws phpmailerException */ function enwiSendMailSmtp(string $html, string $subject, array $recipients, array $attachments, string $fromEmail = '', string $fromName = ''): bool { $bOK = true; if (strlen($html) == 0) { echo '
No message specified'; $bOK = false; } if (strlen($subject) == 0) { echo '
No subject specified'; $bOK = false; } if (count($recipients) == 0 || (isset($recipients['email']) && strlen($recipients['email']) == 0) || (isset($recipients[0]['email']) && strlen($recipients[0]['email']) == 0)) { echo '
No recipient(s) specified'; $bOK = false; } if (!$bOK) { return false; } $encoding = cRegistry::getEncoding(); $mail = initPHPMailer(); $mail->CharSet = $encoding; $mail->Subject = html_entity_decode($subject, ENT_QUOTES, $encoding); $mail->FromName = html_entity_decode(((strlen($fromName)) ? $fromName : getEffectiveSetting('email', 'sender-name')), ENT_QUOTES, $encoding); // recipients if (isset($recipients[0]) && is_array($recipients[0])) { for ($i = 0, $n = count($recipients); $i < $n; $i ++) { if (strlen($recipients[$i]['email'])) { $mail->AddAddress($recipients[$i]['email'], ((strlen($recipients[$i]['name']) > 0) ? html_entity_decode($recipients[$i]['name'], ENT_QUOTES, $encoding) : $recipients[$i]['email'])); } } } else { $mail->AddAddress($recipients['email'], ((strlen($recipients['name']) > 0) ? html_entity_decode($recipients['name'], ENT_QUOTES, $encoding) : $recipients['email'])); } // set replyto if needed if(filter_var($fromEmail, FILTER_VALIDATE_EMAIL)) { $fromName = (strlen($fromName) > 0)?$fromName:$fromEmail; $mail->addReplyTo($fromEmail, $fromName); } // html and only txt body $mail->Body = $html; $message = substr($html, strpos($html, '', '
' . $mail->ErrorInfo . ''; return false; } }