Files
kupshop/bundles/KupShop/DevelopmentBundle/MailArchive.php
2025-08-02 16:30:27 +02:00

141 lines
3.9 KiB
PHP

<?php
namespace KupShop\DevelopmentBundle;
use KupShop\KupShopBundle\Util\Mail\MailUtil;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Part\DataPart;
class MailArchive
{
/** @var MailUtil */
private $mailUtil;
/**
* @required
*/
public function setMailUtil(MailUtil $mailUtil): void
{
$this->mailUtil = $mailUtil;
}
public function archive($mail)
{
if ($mail instanceof Email) {
$this->archiveEmailMessage($mail);
return;
}
$mails = getCache('email_sms_archive');
$newMail['from'] = $mail->From;
$newMail['body'] = $mail->Body;
$newMail['subject'] = $mail->Subject;
$newMail['to'] = join(', ', array_column($mail->to, 0));
$newMail['cc'] = join(', ', array_column($mail->cc, 0));
$newMail['bcc'] = join(', ', array_column($mail->bcc, 0));
$newMail['attachment'] = $mail->attachment;
$newMail['time'] = date('Y-m-d H:i:s');
$mails[] = $newMail;
setCache('email_sms_archive', $mails);
}
public function archiveEmailMessage(Email $message)
{
$mails = getCache('email_sms_archive');
$newMail['from'] = $this->mailUtil->getStringAddress($message->getFrom());
$newMail['body'] = $message->getHtmlBody();
$newMail['subject'] = $message->getSubject();
$newMail['to'] = $this->mailUtil->getStringAddress($message->getTo());
$newMail['cc'] = $this->mailUtil->getStringAddress($message->getCc());
$newMail['bcc'] = $this->mailUtil->getStringAddress($message->getBcc());
$newMail['replyTo'] = $this->mailUtil->getStringAddress($message->getReplyTo());
$newMail['attachment'] = $this->getAttachments($message);
$newMail['time'] = date('Y-m-d H:i:s');
$newMail['id'] = $message->getHeaders()->get('X-WPJ-Id')?->getBody();
$mails[] = $newMail;
setCache('email_sms_archive', $mails);
}
public function getAttachments(Email $message)
{
$attachments = [];
foreach ($message->getAttachments() as $attachment) {
if ($attachment instanceof DataPart) {
// musim pres reflection, protoze tam nemaji zadny getter na filename :(
$dataPart = new \ReflectionObject($attachment);
$property = $dataPart->getProperty('filename');
$property->setAccessible(true);
$attachments[] = [
0 => $attachment->getBody(),
1 => $property->getValue($attachment),
2 => $property->getValue($attachment),
3 => '',
4 => $attachment->getMediaType().'/'.$attachment->getMediaSubtype(),
];
}
}
return $attachments;
}
public function archiveSms($sms)
{
$mails = getCache('email_sms_archive');
$newSms['number_id'] = $sms['number_id'];
$newSms['to'] = $sms['recipient'];
$newSms['body'] = $sms['message'];
$newSms['time'] = date('Y-m-d H:i:s');
$mails[] = $newSms;
setCache('email_sms_archive', $mails);
}
public function getArchive($id = null)
{
$mails = getCache('email_sms_archive');
if ($id !== null) {
if (isset($mails[$id])) {
return $mails[$id];
} else {
return false;
}
} else {
return $mails ?: [];
}
}
public function clearArchive()
{
clearCache('email_sms_archive');
}
public function getArchiveBySubject($subject)
{
$mails = getCache('email_sms_archive');
$filter = [];
foreach ($mails as $m) {
if (strpos($m, $subject)) {
$filter[] = $m;
}
}
return $filter;
}
public function getLast()
{
$archive = $this->getArchive();
return end($archive);
}
}