<?php

class Email
{
    private static $name = 'Cappen';
    private static $host = 'smtp.googlemail.com';
    private static $user = 'email@cappen.com';
    private static $pass = '';
    private static $protocol = 'ssl';
    private static $port = 465;

    // ------------------------------------------------------------

    /**
     * Nova senha.
     *
     * @param  string $nome
     * @param  string $email
     * @param  string $token
     */
    public static function novaSenha($nome = '', $email = '', $token = '')
    {
        $subject = Util::tituloSistema() . ' - Redefinir nova senha';

        $body = 'Prezado(a) ' . $nome . ',';
        $body .= '<br />';
        $body .= 'recebemos uma solicitação para redefinir a sua senha no site: ' . Util::tituloSistema() . ' (' . Util::$siteUrl . ')';
        $body .= '<br /><br>';
        $body .= 'Para continuar, clique no link: ' . Util::baseUrl() . 'recuperar.php?token=' . $token;
        $body .= '<br /><br>';
        $body .= 'Caso não tenha sido você que tenha realizado essa solicitação, por favor desconsiderar este e-mail.';

        return self::envia($email, $subject, $body);
    }

    /**
     * Envia o email.
     *
     * @param  string $email
     * @param  string $subject
     * @param  string $body
     * @return boolean
     */
    public static function envia($email = '', $subject = '', $body = '')
    {
        $phpMailer = new PHPMailer();
        $phpMailer->SetLanguage('br');
        $phpMailer->CharSet = 'utf-8';
        $phpMailer->IsHTML(true);
        $phpMailer->WordWrap = 50;

        // Envia sem autenticar
        $phpMailer->IsMail();

        // Validando a autenticação
        $phpMailer->IsSMTP();
        $phpMailer->SMTPDebug = 2;
        $phpMailer->SMTPAuth = true;
        $phpMailer->SMTPSecure = self::$protocol;
        $phpMailer->Host = self::$host;
        $phpMailer->Username = self::$user;
        $phpMailer->Password = self::$pass;
        $phpMailer->Port = self::$port;

        $phpMailer->From = self::$user;
        $phpMailer->FromName = self::$name;

        $phpMailer->ClearAllRecipients();

        $arrEmail = explode('|', $email);
        if (count($arrEmail) > 0) {
            foreach ($arrEmail as $arr) {
                $phpMailer->AddAddress($arr);
            }
        }

        $phpMailer->Subject = $subject;
        $phpMailer->Body = $body;

        if (!$phpMailer->Send()) {
            return false;
        }

        return true;
    }
}
