It's a MVC question. Here is the situation:
- I am writing an application where I have "groups".
- You can invite other persons to your groups by typing their email and clicking "invite".
- There are two ways this functionality can be called: a) web interface and b) API
- After the mail sending is over I want to report to the user which mails were sent successfully (i.e., if the SMTP send succeeded. Currently, I am not interested in reporting mail bounces).
So, I am thinking how should I design so that there is no code duplication. That is, API and web-interface should share the bulk of the code.
To do this, I can create the method "invite" inside the model "group". So, the API and and the Web-interface can just call: group->invite($emailList); This method can send the emails. But the, problem is, then I have to access the mail templates, create the views for the mails, and then send the mails. Which should actually be in the "View" part or at least in the "Controller" part.
What is the most elegant design in this situation?
Note: I am really thinking to write this in the Model. My only doubt is: previously I thought sending mails also as "presentation". Since it is may be considered as a different form of generating output.
Added after edit
I understand that View does not necessarily have to be output to the browser. And that is where my doubt is. Now the problem is, say I have a "task list" in my app. We can assign a task to some folks. Now the "assignTo" method can be called in two situations: 1) While creating the task 2) reassign a task to someone else.
In both cases the new assignee should get the email notification. So if the method "assignTo" is not sending the mail, we have to duplicate the mailing part in two places: "task create controller" and "task reassign controller".
I wanted to avoid this duplication.