HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: //home/xedaptot/hi.naniguide.com/app/Services/ReplyTrackingAddressService.php
<?php

namespace App\Services;

use App\Models\Campaign;
use App\Models\CampaignRecipient;

class ReplyTrackingAddressService
{
    public function isEnabled(): bool
    {
        return (bool) config('mailpurse.reply_tracking.enabled', false);
    }

    protected function trackingDomainForCampaign(Campaign $campaign): ?string
    {
        $campaign->loadMissing('replyServer');

        $domain = trim((string) ($campaign->replyServer?->reply_domain ?? ''));
        if ($domain !== '') {
            return $domain;
        }

        $domain = trim((string) (config('mailpurse.reply_tracking.reply_domain') ?? ''));

        return $domain !== '' ? $domain : null;
    }

    public function trackingAddressForRecipient(CampaignRecipient $recipient): ?string
    {
        if (!$this->isEnabled()) {
            return null;
        }

        $recipient->loadMissing('campaign.replyServer');

        $campaign = $recipient->campaign;
        if (!$campaign) {
            return null;
        }

        $domain = $this->trackingDomainForCampaign($campaign);
        if (!is_string($domain) || trim($domain) === '') {
            return null;
        }

        $uuid = (string) ($recipient->uuid ?? '');
        $uuid = trim($uuid);

        if ($uuid === '') {
            return null;
        }

        return 'reply+' . $uuid . '@' . $domain;
    }

    public function effectiveReplyTo(Campaign $campaign, ?CampaignRecipient $recipient = null, ?string $fallbackFromEmail = null): string
    {
        $fallbackFromEmail = (string) ($fallbackFromEmail ?? '');
        $campaignReplyTo = (string) ($campaign->reply_to ?? '');

        if ($recipient) {
            $tracking = $this->trackingAddressForRecipient($recipient);
            if (is_string($tracking) && $tracking !== '') {
                return $tracking;
            }
        }

        if ($campaignReplyTo !== '') {
            return $campaignReplyTo;
        }

        // Check if reply server is configured
        $campaign->loadMissing('replyServer');
        $replyServer = $campaign->replyServer;
        
        // If reply server is not configured, return empty string
        if (!$replyServer || !$replyServer->isActive()) {
            return '';
        }

        // Use reply server username as reply-to email if available
        $replyServerEmail = trim((string) ($replyServer->username ?? ''));
        if ($replyServerEmail !== '') {
            return $replyServerEmail;
        }

        // If reply server is configured but no username, use from email
        return $fallbackFromEmail;
    }
}