1

In my extensions updated for Joomla! 4.0 i use the method ClearAddresses(). In Joomla! 3.x the Joomla\CMS\Mail\Mail extended phpmailer class now in Joomla! 4.x the Joomla\CMS\Component\ComponentHelper\MailTemplate extends nothing and i don't have anymore access to ClearAddresses(), ClearCCs(), ClearBCCs() This is my code :

use Joomla\CMS\Mail\MailTemplate;

public static function send(MailTemplate $mailTemplate, array $receivers): bool
{
  $chunks = array_chunk($receivers, $emailRecipientCount);

    $success = true;

    foreach ($chunks as $emails)
    {
        if ($emailRecipientCount == 1 || $emailRecipientPrivacy == 'to')
        {
            $mailTemplate->ClearAddresses();
            $mailTemplate->addRecipient($emails);
        }
        elseif ($emailRecipientPrivacy == 'cc')
        {
            $mailTemplate->ClearCCs();
            $mailTemplate->addCC($emails);
        }
        else
        {
            $mailTemplate->ClearBCCs();
            $mailTemplate->addBCC($emails);
        }

        try
        {
            $result = $mailTemplate->Send();

            if ($result === false)
            {
                // Mail is turned off, or broken
                return false;
            }

            if (is_subclass_of($result, 'Exception'))
            {
                // Mail send is failed
                $success = false;
            }
        }
        catch (Exception $e)
        {
            $success = false;
            Log::add($e->getMessage(), Log::ERROR, 'kunena');
        }

        if (isset(static::$mailer_error_status))
        {
            $success = false;
            break;
        }
    }

    return $success;
}

So by what replacing ClearAddresses(), ClearCCs(), ClearBCCs() method here ?

1 Answer 1

0

The function is still in Joomla 4 as part of the PHPMailer code

../libraries/vendor/phpmailer/phpmailer/src/PHPMailer.php

In Joomla 3 it comes from

../libraries/vendor/phpmailer/phpmailer/class.phpmailer.php

You need to look at your extension and make any changes to find or USE the updated library.

Update...

Try adding this line to the top of the code you have included in your original post above or below the existing Use statement.

use PHPMailer\PHPMailer\PHPMailer
3
  • In Joomla! 3.x, the method ClearAddresses(), ClearCCs(), ClearBCCs() was inherited from phpmailer class in Joomla\CMS\Mail\Mail but now in Joomla! 4.x Joomla\CMS\Component\ComponentHelper\MailTemplate doesn't extends anymore phpmailer and extends nothing so i have no ideas how to access these methods Mar 13, 2022 at 17:14
  • I have updated the answer with a statement that you can use that might fix the problem.
    – Irata
    Mar 14, 2022 at 10:24
  • The method ClearAddresses() isn't declared as static in PHPMailer\PHPMailer\PHPMailer so it's not possible to use it directly the only way it's to load the joomla! mail class, but in my case i don't know how to do it Mar 15, 2022 at 21:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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