Your IP : 216.73.216.229


Current Path : /home/jeromecohp/www/tmp/yooessentials/modules/core-joomla/src/
Upload File :
Current File : /home/jeromecohp/www/tmp/yooessentials/modules/core-joomla/src/Mailer.php

<?php
/**
 * @package   Essentials YOOtheme Pro (Freemium) 1.6.8
 * @author    ZOOlanders https://www.zoolanders.com
 * @copyright Copyright (C) Joolanders, SL
 * @license   http://www.gnu.org/licenses/gpl.html GNU/GPL
 */

namespace ZOOlanders\YOOessentials\Joomla;

use \ZOOlanders\YOOessentials\Mailer as MailerInterface;
use Exception;
use Joomla\CMS\Factory;
use Joomla\CMS\Mail\Mail;
use YOOtheme\Config as Yooconfig;
use YOOtheme\File;
use YOOtheme\Path;
use YOOtheme\Str;

class Mailer implements MailerInterface
{
    /**
     * @var Mail
     */
    public $mailer;

    /**
     * @var Yooconfig
     */
    public $yooconfig;

    /**
     * Constructor.
     */
    public function __construct(Yooconfig $yooconfig)
    {
        $this->mailer = Factory::getMailer();
        $this->yooconfig = $yooconfig;
    }

    public function setFrom(string $from, string $name = ''): MailerInterface
    {
        if (empty($from)) {
            $from = $this->yooconfig->get('joomla.config.mailfrom');
        }

        if (empty($from_name)) {
            $from_name = $this->yooconfig->get('joomla.config.fromname');
        }

        $this->mailer->setFrom($from, $name);

        return $this;
    }

    public function addRecipient(string $recipient, string $name = ''): MailerInterface
    {
        $this->mailer->addRecipient($recipient, $name);

        return $this;
    }

    public function addCc(string $cc, string $name = ''): MailerInterface
    {
        $this->mailer->addCc($cc, $name);

        return $this;
    }

    public function addBcc(string $bcc, string $name = ''): MailerInterface
    {
        $this->mailer->addBcc($bcc, $name);

        return $this;
    }

    public function addReplyTo(string $replyTo, string $name = ''): MailerInterface
    {
        $this->mailer->addReplyTo($replyTo, $name);

        return $this;
    }

    public function setSubject(string $subject): MailerInterface
    {
        $this->mailer->setSubject($subject);

        return $this;
    }

    public function setBody(string $content): MailerInterface
    {
        $this->mailer->setBody($content);

        return $this;
    }

    public function setAltBody(string $content): MailerInterface
    {
        $this->mailer->AltBody = $content;

        return $this;
    }

    public function isHtml(bool $isHtml): MailerInterface
    {
        $this->mailer->isHtml($isHtml);

        return $this;
    }

    public function addAttachment(string $filePath): MailerInterface
    {
        if (!Str::startsWith($filePath, '~') && !Path::isAbsolute($filePath)) {
            $filePath = "~/$filePath";
        }

        if (File::exists($filePath)) {
            $this->mailer->addAttachment(Path::resolve($filePath));
        }

        return $this;
    }

    public function send(): bool
    {
        $result = $this->mailer->Send();

        if ($result instanceof Exception) {
            throw $result;
        }

        if (!$result) {
            throw new Exception($this->mailer->ErrorInfo ?: 'There was an error sending the email.');
        }

        return $result;
    }
}