MVC PHP - sending mail from a model

I am having a problem with a digit when I have to send mail from a model or controller. The fact is that in the controller I use both

This applies to PHP.

In the controller:

if (Post::get()){ $this->model->registerUser( ... ); $this->model->mailSendUserActivation(); // assign something to view. } 

In the model:

 public function mailSendUserActivation(){ $mail = new \com\Mail(); // assign stuff to mail from API classes and other functions in model. $mail->send(); } 

It is right? Or should mail be sent from the controller?

+6
php model-view-controller controller model
source share
2 answers

The model should describe your domain model.
The controller must interact with the user.
Sending mail is an action, so you must handle it in the controller.
If sending emails requires complex code (say, a few lines), consider getting it into some kind of helper class so that your controller is thin and cohesive. So I would put the code for sending email in some helper class method and just call it in the controller action.

Good explanation of MVC on Wikipedia

+10
source share

You should send mail from the controller, reading the / etc data from the model when / if required.

+3
source share

All Articles