Skip to content

i am sending mail from Post.php and mail goes to system decided email every time

i am sending mail from Post.php and mail goes to system decided email every time

private function sendEmail($post)
    {
        try {
            // Validate primary email format
            if (!filter_var($post['email'], FILTER_VALIDATE_EMAIL)) {
                throw new Exception('Invalid primary email format.');
            }

            // Send email to the primary recipient
            $this->mail->send(
                $post['email'],
                ['data' => new DataObject($post)]
            );

            // Secondary email address (make it configurable if needed)
            $secondaryEmail = '[email protected]'; // Replace with desired email address or fetch from config

            // Validate secondary email format
            if (!filter_var($secondaryEmail, FILTER_VALIDATE_EMAIL)) {
                throw new Exception('Invalid secondary email format.');
            }

            // Modify email data for secondary recipient if required
            $secondaryData = new DataObject(array_merge($post, [
                'email' => $secondaryEmail,
                'is_secondary' => true // Example to differentiate
            ]));

            // Send email to the secondary recipient
            $this->mail->send(
                $secondaryEmail,
                ['data' => $secondaryData]
            );

        } catch (Exception $e) {
            // Log or handle email sending failures
            error_log('Email sending failed: ' . $e->getMessage());
        }
    }