<?php
require 'vendor/autoload.php';
require 'mailhandling.php';

class Mail extends Mailhandling {
    // protected $mbox = null;
    protected $uid = 0;
    
    public function __construct(?string $templateName = null) {
        parent::__construct();
        if (!$this->connectToImap()) {
            $this->templateName = 'imaperror';
            return;
        }
        $this->uid = $this->getUriParams()['uid'];
        $this->content['uid'] = $this->uid;
    }
    
    protected function generateContent(): void {
        $messageStructure = imap_fetchstructure($this->mbox, $this->uid);
        $this->fetchEmailHeader($this->content);
        $this->fetchEmailBody($messageStructure, $this->content, 'INBOX', FT_PEEK);
        $this->renderAttachments($messageStructure);
    }
    
    protected function renderAttachments($messageStructure): void {
        $attachments = $this->getAttachments($messageStructure);
        $contentArray = [];
        foreach ($attachments as $attachment) {
            $contentArray[] = $attachment['filename'];
        }
        $this->content['attachments'] = implode('<br>', $contentArray);
    }
}
