I have been working on this class for days with no luck or results.

I'm trying to insert variable in the email body that contains a function that iterates through an array. The variable that has problems is the $yearFunction that calls cleaningArrays() method.

I have tested by inserting an exit() right after calling the cleaningArrays() and I could the data through the array is correct, but it won't just get passed down to the body section.

When an email is sent it just comes as blank no data from the $yearFunction variable.

// No direct access.
defined('_JEXEC') or die;

// Include dependancy of the main controller form class
JForm::addFormPath(JPATH_COMPONENT_ADMINISTRATOR . '/models/forms');

class VifBadgesControllerBadges extends JControllerForm
    protected $yearBadgeArray;
    protected $moduleBadgeArray;
    protected $otherBadgeArray;
    protected $yearBadges;
    protected $moduleBadges;
    protected $otherBadges;
    protected $recipientName;
    protected $recipientEmail;
    protected $message;
    protected $badgeEarner;
    protected $yearFunction;    

    public function cleaningArrays($list)
        for ($k=0; $k < count($list); $k++)
            echo $list[$k];

        return ;

    public function submit()
        // Check for request forgeries.
        JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

        // Initialize variables.
        $app = JFactory::getApplication();
        $mail = &JFactory::getMailer();
        $user = JFactory::getUser();
        $redirectUrl = JRoute::_('index.php/', false);

        $postData = $app->input->post;
        $this->recipientEmail = $postData->get('email', '', 'STRING');
        $this->recipientName = $postData->get('name', '', 'STRING');
        $this->message = $postData->get('message', '', 'STRING');
        $this->badgeEarner = $postData->get('badgeEarner', '', '');
        $yearBadgeArray = $postData->get('yearBadgeData', '', '');
        $moduleBadgeArray = $postData->get('moduleBadgeData', '', '');
        $otherBadgeArray = $postData->get('otherBadgeData', '', '');

        $this->yearBadgeArray= unserialize(base64_decode($yearBadgeArray));
        $this->moduleBadgeArray= unserialize(base64_decode($moduleBadgeArray));
        $this->otherBadgeArray= unserialize(base64_decode($otherBadgeArray));

        $yearFunction = function()
            $badge = [];

            foreach ($this->yearBadgeArray as $yearBadge)
                $badge[] = $yearBadge[2][1];



        $body = "<div style='font-family:arial,helvetica,sans-serif;font-size:12px;'>
        <p>Hi ".$this->recipientName.",</p>
        <p>".$this->badgeEarner." has earned digital badges for completing a PD modules in VIF's Learning Center and wants to share the details with you!</p>
        <p><strong>Message from ".$this->badgeEarner."</strong> " .$this->message."</p>
        <p><strong>Year Badges:</strong></p>


        <hr style='border:0px;color:rgb(237,237,235);min-height:1px;width:525px;background-color:rgb(237,237,235)' />
        <div style='font-family:arial,helvetica,sans-serif;text-align:center;font-size:10px;color:rgb(102,102,102)'>
        <p>This email was sent to Andrew from Community.</p><p>2015</p></div>";

        $senderEmail = $user->email;
        $senderName = $user->name;
        $sender = array($senderEmail, $senderName);

        $mail->Encoding = 'base64';
        $mail->setSubject(JText::_($this->badgeEarner.' Shared a Digital Badge'));

        $send = $mail->Send();

  • On a side note, don't use JRequest as it's deprecated
    – Lodder
    Dec 2, 2015 at 20:41
  • 1
    On the actual topic, have you set error reporting to development on your Global Configuration? Also, have you tried returning the result of your anonymous function like so: return $this->cleaningArrays($badge); ?
    – Lodder
    Dec 2, 2015 at 20:52

1 Answer 1


I finally fixed it thanks for my co-worker Simon. So what I did is I removed the cleaningArrays() method, and re-wrote my anonymous function like below by adding the implode() method:

$yearBadgesFunction = function()
        $badge = [];

        foreach ($this->yearBadgeArray as $yearBadge)
            $badge[] = $yearBadge[2][1];

        $badgeDataSet = implode(" ", $badge);

        return $badgeDataSet;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.