176 Zeilen
		
	
	
		
			Kein EOL
		
	
	
		
			5,7 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			176 Zeilen
		
	
	
		
			Kein EOL
		
	
	
		
			5,7 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 | |
| 
 | |
| use Greew\OAuth2\Client\Provider\Azure;
 | |
| use PHPMailer\PHPMailer\PHPMailer;
 | |
| use PHPMailer\PHPMailer\SMTP;
 | |
| use PHPMailer\PHPMailer\OAuth;
 | |
| use PHPMailer\PHPMailer\Exception;
 | |
| 
 | |
| /**
 | |
|  * @param bool|null $exception
 | |
|  * @return PHPMailer
 | |
|  * @throws Exception
 | |
|  */
 | |
| function initPHPMailer(bool $exception = null): PHPMailer
 | |
| {
 | |
|     if(cFileHandler::exists(cRegistry::getClientConfig(1)['config']['path'] . 'config.phpmailer.php')) {
 | |
|         $config = parse_ini_file(cRegistry::getClientConfig(1)['config']['path'] . 'config.phpmailer.php', true);
 | |
|     } else if (cFileHandler::exists(cRegistry::getConfigValue('path', 'config') . 'config.phpmailer.php')) {
 | |
|         $config = parse_ini_file(cRegistry::getConfigValue('path', 'config') . 'config.phpmailer.php', true);
 | |
|     } else {
 | |
|         throw new Exception("SMTP config not found!");
 | |
|     }
 | |
| 
 | |
|     $mailer = new PHPMailer($exception);
 | |
| 
 | |
|     //Enable SMTP debugging
 | |
|     //SMTP::DEBUG_OFF = off (for production use)
 | |
|     //SMTP::DEBUG_CLIENT = client messages
 | |
|     //SMTP::DEBUG_SERVER = client and server messages
 | |
|     $mailer->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 '<pre>No message specified</pre>';
 | |
|         $bOK = false;
 | |
|     }
 | |
|     if (strlen($subject) == 0) {
 | |
|         echo '<pre>No subject specified</pre>';
 | |
|         $bOK = false;
 | |
|     }
 | |
|     if (count($recipients) == 0 || (isset($recipients['email']) && strlen($recipients['email']) == 0) || (isset($recipients[0]['email']) && strlen($recipients[0]['email']) == 0)) {
 | |
|         echo '<pre>No recipient(s) specified</pre>';
 | |
|         $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, '<body'));
 | |
|     $message = str_replace(array("\n", '</p>', '<br />', '<br>', '</li>'), array('', "</p>\n\n", "\n", "\n", "\n"), $message);
 | |
|     $message = trim(strip_tags($message));
 | |
|     $message = explode("\n", $message);
 | |
|     for ($i = 0, $n = count($message); $i < $n; $i ++) {
 | |
|         $message[$i] = trim($message[$i]);
 | |
|     }
 | |
|     $message = implode("\n", $message);
 | |
|     $message = html_entity_decode($message, ENT_QUOTES, $encoding);
 | |
|     try {
 | |
|         $message = cString::replaceDiacritics($message);
 | |
|     } catch (cInvalidArgumentException $e) {
 | |
|         return false;
 | |
|     }
 | |
| 
 | |
|     # <-- Nur-Text-Bereich
 | |
|     $mail->AltBody = $message;
 | |
| 
 | |
|     if(count($attachments) > 0) {
 | |
|         foreach ($attachments as $attachment) {
 | |
|             if(is_file($attachment)) {
 | |
|                 $mail->addAttachment($attachment);
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     $mail->WordWrap = 76;
 | |
|     
 | |
|     if ($mail->Send()) {
 | |
|         return true;
 | |
|     } else {
 | |
|         echo '<pre>' . $mail->ErrorInfo . '</pre>';
 | |
|         return false;
 | |
|     }
 | |
| } |