<?php

class Af2AjaxTestmail {

    function __construct() {}

    public function af2_test_mail() {
        $smtp = $_GET['use_smtp'];
        $wp_mail = $_GET['use_wp_mail'];
    
    
        if ($smtp === true || $smtp === 'true') {
            $cc = $_GET['cc'];
            $bcc = $_GET['bcc'];
            $cc_list = explode(',', $cc);
            $bcc_list = explode(',', $bcc);
    
            $resp = $this->af2_smtp_mail($_GET['host'], $_GET['username'], $_GET['password'], $_GET['port'], $_GET['type'], $_GET['to'], $_GET['from'], $_GET['from_name'], __('Testmail'), __('Dies ist eine Testmail vom Funnelforms Pro Plugin per SMTP'), $cc_list, $bcc_list,$_GET['reply_to']);
    
            $arr = [];
    
            if (!$resp)
                $arr = array('status' => 'Error', 'message' => __("Bitte konfiguriere deine SMTP Einstellungen", 'af2_multilanguage'), 'resp' => $resp);
    
            $arr = array('status' => $resp->status, 'message' => ( $resp->status == 'Success' ? __('E-mail sent successfully!', 'af2_multilanguage') : __("E-Mail konnte nicht gesendet werden!", 'af2_multilanguage') ), 'resp' => $resp);
    
            echo $arr['status'] . ' : ' . $arr['message'];
        }
        else {
            $headers = 'From: ' . $_GET['from_name'] . ' <' . $_GET['from'] . '>' . "\r\n";
            $headers .= 'CC: ' . $_GET['cc'] . "\r\n";
            $headers .= 'BCC: ' . $_GET['bcc'] . "\r\n";
    
            if ($wp_mail === true || $wp_mail === 'true') {
                wp_mail($_GET['to'], __('Testmail', 'af2_multilanguage'), __('This is a test mail from the Funnelforms Pro plugin', 'af2_multilanguage'), $headers);
    
                echo __('Testmail sent with WP Mail. Please check your mailbox!', 'af2_multilanguage');
            } else {
                mail($_GET['to'], __('Testmail', 'af2_multilanguage'), __('This is a test mail from the Funnelforms Pro plugin', 'af2_multilanguage'), $headers);
    
                echo __('Testmail sent without SMTP. Please check your mailbox!', 'af2_multilanguage');
            }
        }
    
    
        //echo $resp;
    
        wp_die();
    }
    
    private function af2_smtp_mail($host, $user, $password, $port, $type, $to, $from, $from_nam, $subject, $body, $cc, $bcc,$reply_to) {
        //$errors = '';
    
        //$swpsmtp_options = get_option('twm_smtp_options');
    
        require_once( ABSPATH . WPINC . '/class-smtp.php' );
        require_once( ABSPATH . WPINC . '/class-phpmailer.php' );
        
        $mail = new \PHPMailer();
    
        $charset = get_bloginfo('charset');
        $mail->CharSet = $charset;
        $mail->Timeout = 10;
    
        $from_name = $from_nam;
        $from_email = $from;
    
        $mail->IsSMTP();
    
        $mail->SMTPAuth = true;
        $mail->Username = $user;
        $mail->Password = $password;
    
        $mail->SMTPSecure = $type;
    
        /* PHPMailer 5.2.10 introduced this option. However, this might cause issues if the server is advertising TLS with an invalid certificate. */
        $mail->SMTPAutoTLS = false;
    
        /* Set the other options */
        $mail->Host = $host;
        $mail->Port = $port;
    
        $mail->SetFrom($from_email, $from_name);
        $mail->isHTML(true);
        $mail->Subject = $subject;
        $mail->MsgHTML($body);
        $mail->AddAddress($to);
    
        foreach ($cc as $c) {
            $c = trim($c);
    
            $mail->addCC($c);
        }
    
        foreach ($bcc as $bc) {
            $bc = trim($bc);
    
            $mail->addBCC($bc);
        }
    
        if(!empty($reply_to)){
            $mail->addReplyTo($reply_to);
        }
        
        $mail->SMTPDebug = 4;
    
        /* Send mail and return result */
    
        $error = $mail->Send();
    
        $mail->ClearAddresses();
        $mail->ClearAllRecipients();
        $mail->clearCCs();
        $mail->clearBCCs();
    
        return $error;
    }
}