Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC PHP - Sending mail from Model

I am having problem to figure whenever I should send mail from the Model or the Controller. The thing is In the controller i use like

This is regarding PHP.

In Controller:

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

In Model:

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

Is this correct ? Or should the mail really be sent from the controller ?

like image 406
Jonatan Avatar asked Feb 16 '11 14:02

Jonatan


2 Answers

Model should describe you domain model.
Controller should handle interaction with user.
Sending mail is an action so you should handle it in controller.
If sending email requires complicated code (say more than few lines) consider to extract it to some helper class to keep your controller slim and cohesive. So I would put code for sending email in some helper class method and just call it in controller action.

Good explanation of MVC on wikipedia

like image 68
Mr. L Avatar answered Oct 23 '22 02:10

Mr. L


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

like image 20
diagonalbatman Avatar answered Oct 23 '22 01:10

diagonalbatman