Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CakePhp 4 ($mailer) must be of type Cake\Mailer\Mailer, Cake\View\ViewBuilder given #951

Closed
DrMickArtisan opened this issue Aug 29, 2023 · 4 comments · Fixed by #953
Closed

Comments

@DrMickArtisan
Copy link

Hi,
I use mailer and mail preview since cake 3, and while trying to do same with cakephp 4 I just ran into something weird.
I got a UserMailer with many method (devis, contact, confirmation, etc..). In each of these methods I set something like :

public function devis($data) 
{
 return $this->setTo($data['email']) // add email recipient
                ->setFrom($this->_contactAddress,$this->_contactName)
                ->setEmailFormat('html') // email format
                ->setSubject($subject) //  subject of email
                ->setAttachments($Attachements)
                ->setViewVars($data)
                ->viewBuilder()
                // the template file you will use in this email
                ->setTemplate($template)
                ->setLayout('users')
                ->addHelpers(['Html','Number'])
                ;
}

(Switching to cake 4, I was forced to add the call to "->viewBuidler()" before "setTemplates", "setLayouts" and "addHelpers".)
And then, in my UserMailPreview I use :

public function devis()
{
//... some treatment to gather data
return $this->getMailer("Users")
                ->devis($dataUser);
}

And unfortunately, I ran in :
'DebugKit\Mailer\PreviewResult::__construct(): Argument #1 ($mailer) must be of type Cake\Mailer\Mailer, Cake\View\ViewBuilder given'
I do understand that yes, my "devis" method return a viewBuilder object, but how can I assign templates, layout and helpers without it ??

@markstory
Copy link
Member

I do understand that yes, my "devis" method return a viewBuilder object, but how can I assign templates, layout and helpers without it ??

You have to change your code to look more like

 $mailer = $this->setTo($data['email']) // add email recipient
                ->setFrom($this->_contactAddress,$this->_contactName)
                ->setEmailFormat('html') // email format
                ->setSubject($subject) //  subject of email
                ->setAttachments($Attachements)
                ->setViewVars($data);
$mailer->viewBuilder()
                // the template file you will use in this email
                ->setTemplate($template)
                ->setLayout('users')
                ->addHelpers(['Html','Number']);
return $mailer;

@DrMickArtisan
Copy link
Author

Ha yes, it works. It seems that the issue is just on documentation, then :)
Thanks a lot !

@markstory
Copy link
Member

It seems that the issue is just on documentation, then :)

Which documentation is incorrect?

@DrMickArtisan
Copy link
Author

The examples in the mail-preview debug-kit page, which are the same than in cakephp 3 :

class UserMailer extends Mailer
        {
            public function welcome($user)
            {
                return $this // Returning the chain is a good idea :)
                    ->to($user->email)
                    ->subject(sprintf("Welcome %s", $user->name))
                    ->template("welcome_mail") // By default template with same name as method name is used.
                    ->layout("custom")
                    ->set(["user" => $user]);
            }
        }

and

class UserMailPreview extends MailPreview
        {
            public function welcome()
            {
                $this->loadModel("Users");
                $user = $this->Users->find()->first();
                return $this->getMailer("User")
                    ->welcome($user)
                    ->set(["activationToken" => "dummy-token"]);
            }
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants