<?php
namespace Bdp\Modules\Mail\Controllers;

class Sendmail
{
    function __construct()
    {
        global $_REQUEST;

/*
        $smtp_host = 'bdp.mein-verein.online'; // SMTP-Host
        $smtp_port = 25; // SMTP-Port
        $smtp_username = 'noreply@mareike.sachsen.pfadfinden.de'; // SMTP-Benutzername
        $smtp_password = 'fwJ_wxbW9G45'; // SMTP-Passwort
        $smtp_secure = 'tls'; // Verschlüsselung (tls oder ssl)

        // Einstellungen für wp_mail ändern
        add_action( 'phpmailer_init', function( $phpmailer ) use ( $smtp_host, $smtp_port, $smtp_username, $smtp_password, $smtp_secure ) {
            #$phpmailer->isSMTP();
            #$phpmailer->Host = $smtp_host;
            #$phpmailer->Port = $smtp_port;
            #$phpmailer->SMTPAuth = true;
            #$phpmailer->Username = $smtp_username;
            #$phpmailer->Password = $smtp_password;
            $phpmailer->setFrom('info@pfingstlager-24.de', "KoPfiLa '24 - Die Lagerleitung");
            #$phpmailer->SMTPSecure = $smtp_secure;
        } );
*/

        $unfiltered_mails = explode(',', str_replace(';', ',', $_REQUEST['mail-to']));
        $mail_to = array();
        foreach ($unfiltered_mails as $recipient) {
            $recipient = trim($recipient);
            if (!in_array($recipient, $mail_to)) {
                $mail_to[] = $recipient;
            }
        }

        foreach ($mail_to as $mail_to_line) {
            if (str_contains($mail_to_line, '<')) {
                $pattern = '/^(.*)<(.*)>$/';
                if (preg_match($pattern, $mail_to_line, $matches)) {
                    $name = trim($matches[1]); // Name
                    $email = trim($matches[2]); // E-Mail-Adresse

                    $content = str_replace('[anrede]', 'Hallo ' . $name . ',', $_REQUEST['mail-text']);
                    $content = str_replace('[name]', $name, $content);
                    $content = nl2br($content);
                    $sent = wp_mail($email, $_REQUEST['mail-subject'], $content,
                        ['Reply-To: ' . $_REQUEST['mail-from'], 'Content-Type: text/html; charset=UTF-8']);

                    // Überprüfen, ob die E-Mail erfolgreich gesendet wurde
                    if ($sent) {
                        kompass_print_message_box(__('Mail sent to', BDP_LV_PLUGIN_SLUG) . ': ' . $email);
                    } else {
                        kompass_print_message_box(__('Mail failed to sent to', BDP_LV_PLUGIN_SLUG) . ': ' . $email, 'error');
                    }
                }
            } else {
                $sent = wp_mail($_REQUEST['mail-to'],$_REQUEST['mail-subject'], $_REQUEST['mail-text'],
                    ['Reply-To: ' . $_REQUEST['mail-from'], 'Content-Type: text/html; charset=UTF-8']);

                // Überprüfen, ob die E-Mail erfolgreich gesendet wurde
                if ( $sent ) {
                    kompass_print_message_box(__('Mail successfully sent to', BDP_LV_PLUGIN_SLUG) .': ' . $_REQUEST['mail-to']) ;
                } else {
                    kompass_print_message_box(__('Mail failed to sent to', BDP_LV_PLUGIN_SLUG) .': ' . $_REQUEST['mail-to'], 'error') ;
                }
            }
        }
    }
}