<?php

function isConnectedInternet() {
    $connected = @fsockopen("www.google.com", 80);
    //website, port  (try 80 or 443)
    if ($connected) {
        $is_conn = true; //action when connected
        fclose($connected);
    } else {
        $is_conn = false; //action in connection failure
    }
    return $is_conn;
}

function isOnLineDomain() {
    if ($_SERVER['SERVER_NAME'] != "localhost") {
        if ($_SERVER['SERVER_NAME'] != "gme_gm") {
            return true;
        }
    }
    return false;
}

function getEmailSender() {
    $host = $_SERVER['HTTP_HOST'];
    if (($host == "gme.ao") || ($host == "www.gme.ao")) {
        return "info@gme.ao";
    } else {
        return "noreply@silicaweb.ao";
    }
}

function mailStyle() {
    return 'html{
                border: 1px solid #ccc;
                border-radius: 10px;
                margin: 10px;
                font-family: sans-serif;
            }
            .divMailHeader{
                padding: 20px;
                min-height: 30px;
                background-color: #7e00cc;
                border-radius: 10px;
                color: white;
            }
            .divMailContent{
                padding: 20px;
                border-radius: 10px;
                margin: 10px;
                min-height: 100px;
            }
            .divMailContent a{
                display: block;
                border: none;
                border-radius: 5px;
                background-color: #7e00cc;
                color: white;
                padding: 10px;
                width: max-content;
                text-align: center;
            }
            
            .divMailRemetent{
                padding-bottom: 5px;
                margin-bottom: 20px;
                border-bottom: 1px solid black;
                max-width: max-content;
            }

            .divMailFooter{
                padding: 20px;
                min-height: 30px;
                border-top: 1px solid #ccc;
                font-size: 10px;
            }
            .divMailFooter h3{
                font-size: 14px;
            }';
}

function mailHeader() {
    return '<div class="divMailHeader">
            <h2>Sílica RH</h2>
            <p>Software de facturação e gestão, certificado pela AGT</p>
        </div>';
}

function mailFooter() {
    return '<div class="divMailFooter">
            <h3>Sílica RH - Software de facturação e gestão</h3>
            <span>Endereço: <a href="https://goo.gl/maps/TnzY4TBaSYVfw6AbA" target="_blank">Av. Dr. António Agostinho Neto, Junto ao Memorial Agostinho Neto, Bairro da Coreia, Comuna da Kinanga, Ingombota, Luanda, Angola</a></span><br>
            <span>Contactos: (+244) <a href="tel:993700757">993 700 757</a> / <a href="tel:948109778">948 109 778</a></span><br>
            <span>E-mail: <a href="mailto:cc@silicaweb.ao">cc@silicaweb.com</a> / <a href="mailto:at@silicaweb.ao">at@silicaweb.ao</a></span><br>
            <span>Website: <a href="https://www.silicaweb.ao?sn=email" target="_blank">www.silicaweb.ao</a></span><br>
            <span>Facebook: <a href="https://www.facebook.com/silicaweb" target="_blank">/silicaweb</a></span><br>
            <span>WhatsApp: <a href="https://wa.me/244993700757" target="_blank">993 700 757</a></span>
            <p>Desenvolvido por GM Empreendimentos, Lda <br>
                Todos direitos reservados para GM Empreendimentos, Lda</p>
        </div>';
}

function mailContent($content) {
    return '<div class="divMailContent">'
            . $content
            . '</div>';
}

function sendMail($to, $subject, $contentHtmlStyle, $entityRemetent = "", $mailSender = "") {
    if ($entityRemetent != "") {
        $entityRemetent = '<div class="divMailRemetent">'
                . '<label><i>Entidade remetente: ' . $entityRemetent . '</i></label><br>'
                . '</div>';
    }
    $message = "<html>"
            . "<head>"
            . "<title>Sílica RH</title>"
            . "<style>" . mailStyle() . '</style>'
            . "</head>"
            . "<body>"
            . mailHeader()
            . mailContent($entityRemetent . $contentHtmlStyle)
            . mailFooter()
            . "</body>"
            . "</html>";

// Always set content-type when sending HTML email
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
   // if ($mailSender == "") {
  //      $mailSender = getEmailSender();
 //   }
    $headers .= 'From: <' . getEmailSender() . '>' . "\r\n";
    if (isOnLineDomain()) {
        if (isConnectedInternet()) {
            mail($to, $subject, $message, $headers);
            return true;
        }
    }
    return false;
}
