MVC PHP - Sending mail from Model
Asked Answered
S

3

6

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 ?

Snug answered 16/2, 2011 at 14:2 Comment(0)
L
10

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

Lailaibach answered 16/2, 2011 at 14:12 Comment(1)
Also, consider rendering your mail body in the View layer, if possible. Some MVCs make this very easy, and it puts the presentation where it belongs. This often brings advantages, e.g. if the view layer in your system has lots of handy facilities for rendering HTML for web pages, it makes it very easy to render HTML email, too.Chink
A
3

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

Airedale answered 16/2, 2011 at 14:9 Comment(0)
F
-1

I m pretty confused with putting application logic and workflows in the controller itself.. Sending business emails should be counted in the application logic and workflow and according to MVC architecture, A model should hold the application logic and workflow of business as It is the only object which is aware of all the business workflows and logics as far as controller concern, I am confused, if the controller should be told about the business flows implementation and or should it contact directly to a helper class which implements the mailing services in ur case.. However, I see, if Controller gets the composition of messages ,subject, and other properties of message from the model and then it passes them to the mailer helping class, that makes more sense here.. as Model still knows about the details of the message and controller role could be just made to get the message detail from the model and pass it to the helper class. i am confused in this case,if an error occurs while sending email via helper class, the model should be informed or not , or controller either logs it to a file or presents it to View.. however, Thinking of contacting a Service provider From the Models directly does make sense. in that case, Controller only sends requests to the model, Model on its side could contact service provider on its behalf to get its work done and pass the output to the controller back which further could pass it to view or process further...

Fillet answered 3/8, 2018 at 7:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.