tags:

views:

185

answers:

2

I have frontend and admin apps in Symfony 1.2/Doctrine and both generate email messages. I see a lot of shared code doing the email between the two apps. If I wanted to consolidate the email into one component, where would it reside? Doesn't seem like it should go in the model. Suggestions?

+1  A: 

Edit: Sorry I thought you also want to share the email text between applications, i.e. sending the same email from different applications. Anyway, an event handler could be interesting for you.


Best way (i.e. cleanest approach): Create a custom event handler that listens to a custom event and raise your event where appropriate. More about events in symfony.


Another approach: Put your email text into a template. You can do the same and use a shared template folder this way. But then you have to duplicate the logic to send the email.

Or you put the text and the code to send the email into a separate class and put it into the global lib folder.

If sending the email is not business logic but some kind of notification, don't put it into the model.

Felix Kling
+1  A: 

In my Symfony projects I find that makes sense to have an Email.class.php class in lib/ with

<?php

class Email
{
  public static function sendEmail($body, $bodyTxt, $subject, $to, $replyto = null){
   ...
  }
}

And then you can call it from everywhere with Email::sendEmail(...) after clearing cache.

nacmartin