Php – Does an ‘Email’ class belong in model or view

design-patternsemailmodel-view-controllerPHP

I have a User class in the model and need to send a password reminder email.

  • Does the controller instantiate a User and pass it (or values from it, rather) to a Email class in the view? In which case, would the controller then do the actual sending of the email?
  • Or would the controller call User::sendEmail() and there not even be a view for the email (and the User do the sending)?
  • Or is there another model class for Email which handles the sending, and it is given to the view Email?

Or am I completely lost? 😉

Thanks for any help you may be able to give me!

Best Answer

Unless you are building a messaging system, emails would rather be a resource than part of the core domain. You can treat them as any other kind of resource such as database access, logging and so on.

Personally, I'd abstract it as a MessagingService to avoid too tight a coupling to emails specifically - that would allow you to send off messages along other channels in the future, if relevant. The controller would require an injected MessagingService and send off the message.

The only reason to involve a user object would be if you need values from the user to populate the contents of the email, but that is basically just a transformation of data.