tags:

views:

86

answers:

3

I have built a model in some of my MVC websites to assist with sending emails, generally I do something like this

$mail = new Mail_Model;
$mail->to('[email protected]');
$mail->from('[email protected]');
$mail->subject('hello');
$mail->body('hello how are you');
$mail->send();

I know the model is meant to model data - so is what I'm doing a violation of that? Should it be a helper class instead? Something like...

Mail::send('[email protected]', '[email protected]', 'hello', 'hello how are you');

That second one doesn't read as well to me.. of course I could pass in an array with named keys to make it more readable.

So is my Mail model violating what a model should be in the MVC paradigm?

+1  A: 

The mail isn't really a model, I suspect, unless you're saving it to the database or doing some other complicated processing with it.

IMO, instantiating an object in this case is probably not very 'MVC', and your second example looks (at least to me) more like a line of code that indicates a one-off action with no real business logic repercussions.

But - don't feel too constrained by the paradigm! Which of those two options do you think is easier to read, and easier for another coder to follow along with later?

arcwhite
A: 

Take '_Model' off your first one and that would make perfect sense to me. Implementing mail-sending as a single static function gives you no benefit over just calling mail(), really. you'll end up with the same problems trying to set additional headers, or send HTML email, or add attachments that extra methods on a class can abstract from your calling code.

Greg
+1  A: 

since this question is tagged with 'kohana' maybe you already know that kohana has an email helper that does exactly what you're doing here.

inherently, my answer is that the code to send an email would fit better as a helper.

gpilotino