<?php

namespace api\classes;

use api\classes\API;

require_once 'API.php';


class API_mailsettings extends API
{
    public function updateMailSettings($updatePassword)
    {
        if ($updatePassword) {
            $query = "UPDATE system_settings SET mail_from_name = ?, mail_from_address = ?, mail_smtp_host = ?, mail_smtp_secure = ?, mail_smtp_port = ?, mail_smtp_auth = ?, mail_smtp_user = ?, mail_smtp_pass = ? WHERE portal_uuid = ?";
            $stmt = $this->prepareStatement($query);
            $stmt->bind_param('ssssiisss', $this->data['mail_from_name'], $this->data['mail_from_address'], $this->data['mail_smtp_host'], $this->data['mail_smtp_secure'], $this->data['mail_smtp_port'], $this->data['mail_smtp_auth'], $this->data['mail_smtp_user'], $this->data['mail_smtp_pass'], $this->data['portal_uuid']);

        } else {
            $query = "UPDATE system_settings SET mail_from_name = ?, mail_from_address = ?, mail_smtp_host = ?, mail_smtp_secure = ?, mail_smtp_port = ?, mail_smtp_auth = ?, mail_smtp_user = ? WHERE portal_uuid = ?";
            $stmt = $this->prepareStatement($query);
            $stmt->bind_param('ssssiiss', $this->data['mail_from_name'], $this->data['mail_from_address'], $this->data['mail_smtp_host'], $this->data['mail_smtp_secure'], $this->data['mail_smtp_port'], $this->data['mail_smtp_auth'], $this->data['mail_smtp_user'], $this->data['portal_uuid']);

        }

        if ($this->executeStatement($stmt)) {
            $this->apiOutput(200, ['success' => 'mail settings updated successfully.']);
        }

    }

    public function testSendMail($mailBuilder)
    {

        $mailBuilder->subject = "Test mail";
        $mailBuilder->addAddress($this->data['mail_to'], $this->data['mail_to']);
        $mailBuilder->mailText = '
Hello,<br><br>

This is a test email sent through Sentri.<br><br>

If you received this email, your Sentri mail settings are configured correctly.
';
        $mailBuilder->sendMail();
    }
}
