MVC Emails
Moderator: General Moderators
- inghamn
- Forum Contributor
- Posts: 174
- Joined: Mon Apr 16, 2007 10:33 am
- Location: Bloomington, IN, USA
MVC Emails
How do you all assemble emails in your MVC applications?
In our content manager, we have the need to send out HTML newsletters via email. These need to allow for someone to add custom content to the email that goes out as well.
Putting together the contents of the email seems incredibly similar to a normal Composite View, except it's not rendering to the browser. It's still creating a full web page, only the delivery method is different. Would it be best for a Controller to create two views, and call render() on each one?
In our content manager, we have the need to send out HTML newsletters via email. These need to allow for someone to add custom content to the email that goes out as well.
Putting together the contents of the email seems incredibly similar to a normal Composite View, except it's not rendering to the browser. It's still creating a full web page, only the delivery method is different. Would it be best for a Controller to create two views, and call render() on each one?
- inghamn
- Forum Contributor
- Posts: 174
- Joined: Mon Apr 16, 2007 10:33 am
- Location: Bloomington, IN, USA
Re: MVC Emails
You know, would it make sense to have the View::render() optionally accept some Email object.
So a Controller's action would look something like:
I realize this is all psuedo-code , but the idea is allowing one controller action to assemble and render multiple views. Only one of the views would actually be sent to the browser.
You could have Views that get sent via SMS, maybe.
So a Controller's action would look something like:
Code: Select all
$email = new Email('someone@somewhere.com');
$email->subject = 'Subject';
$view = new View();
$view->blocks[] = new Block('banner.inc');
$view->blocks[] = new Block('email_message_body.inc',array('message'=>$_POST['message']));
$view->blocks[] = new Block('footer.inc');
$view->render($email);
# Then do display the success page
$view = new View();
$view->blocks[] = new Block('banner.inc');
$view->blocks[] = new Block('success.inc');
$view->blocks[] = new Block('footer.inc');
$view->render();
You could have Views that get sent via SMS, maybe.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: MVC Emails
Generally speaking your view shouldn't output directly to the browser. The content generated from your view should then be passed to the response object which then decides how to output the contents. Therefore, it would be as simple as passing your view to the content, which at some point should render the view.
//or
Code: Select all
$view = new A_View_Email();
$view->foobar = 'foo';
$emailer = new A_Emailer($view); //internally will call $view->render()Code: Select all
$emailer = new A_Emailer();
$emailer->setContent($view->render());- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: MVC Emails
The code you show is not really a Controller because there is not request handling or program flow. What you really have is a View. It uses the same Model to fill in a HTML template, which is the main response to the request (which came from the browser). But you also respond with an email as a second response -- but that's still a View thing.
(#10850)
- inghamn
- Forum Contributor
- Posts: 174
- Joined: Mon Apr 16, 2007 10:33 am
- Location: Bloomington, IN, USA
Re: MVC Emails
arborint: You are correct, sir, the pseudo-code I posted would be a snippet of an action inside of a controller. I was just trying to focus on the Controller's responsibility of creating Views and having them rendered.
Jcart makes a good point about creating a response object and passing the View to it. That was one of two routes I was seeing.
1) pass the View to the Response and tell the Response to send it somewhere.
2) pass the Email to the View and let the View send it somewhere.
Although I'm not using the ZF right now, I know the ZF uses the Response object way of handling it. Right now, my render() methods use include() to lazy load the appropriate formatted files...but I could work that out and turn it into returning output instead of outputting output.
What I'm really curious about, though is about having multiple responses in a single controller action. Working with the ZF, it seems to only allow One Response, set up by the Front controller. Which doesn't let you send the same View to multiple Response objects. At least, not without all the crazy routing (which is what's turned me away from ZF so far).
In this case, to send an email and show a success page in the browser. Why not have the controller action generate multiple responses (or multiple views, depending on your style)?
I'm happy to post actual code examples from my current stuff. Although, since I'm not using ZF it might just lead to more confusion.
Jcart makes a good point about creating a response object and passing the View to it. That was one of two routes I was seeing.
1) pass the View to the Response and tell the Response to send it somewhere.
2) pass the Email to the View and let the View send it somewhere.
Although I'm not using the ZF right now, I know the ZF uses the Response object way of handling it. Right now, my render() methods use include() to lazy load the appropriate formatted files...but I could work that out and turn it into returning output instead of outputting output.
What I'm really curious about, though is about having multiple responses in a single controller action. Working with the ZF, it seems to only allow One Response, set up by the Front controller. Which doesn't let you send the same View to multiple Response objects. At least, not without all the crazy routing (which is what's turned me away from ZF so far).
In this case, to send an email and show a success page in the browser. Why not have the controller action generate multiple responses (or multiple views, depending on your style)?
I'm happy to post actual code examples from my current stuff. Although, since I'm not using ZF it might just lead to more confusion.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Re: MVC Emails
Sounds like you may need to decouple your view from your response a little... what if you need to send multiple emails in the same action?
Code: Select all
$emailView = new EmailView();
$emailView->setVars(array(
'var1' => $something,
'var2' => $somethingElse
));
$emailView->load('some-template.tpl.php');
mail(....., $emailView->render());- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: MVC Emails
I think you are getting "HTTP Response" mixed up with "response to the user". The View can generate both, but what is send back to the browser is the "HTTP Response". If you also send an email, which is generated by a separate View, that is a "response to the user". Remember, we are talking about a web application so Request/Response has a specific HTTP meaning.inghamn wrote:What I'm really curious about, though is about having multiple responses in a single controller action. Working with the ZF, it seems to only allow One Response, set up by the Front controller. Which doesn't let you send the same View to multiple Response objects. At least, not without all the crazy routing (which is what's turned me away from ZF so far).
(#10850)
- inghamn
- Forum Contributor
- Posts: 174
- Joined: Mon Apr 16, 2007 10:33 am
- Location: Bloomington, IN, USA
Re: MVC Emails
Ahh, I see, now, my confusion about the ZF Response object. I was thinking it was a generic 'send stuff to the user' response and not necessarily an HTTP Response. In any event, I am only wanting to send out a single HTTP Response in response to a single HTTP Request. In addition, I am wanting to send out stuff in other ways ( in this case, email).
In the case of my current project, a content manager, I'm wanting to send out the new versions of documents to all the 'watchers' and to send out new documents to all the section subscribers. In both these cases, I've got the HTML contents of the document in a View object.
JCart pointed out very well, that the View shouldn't send anything, it should just return stuff. And that I should pass the View to whatever's going to handle the sending. I'm working on converting my Views to do just that (up to now I've been letting the View object do the sending). I should have been doing this all along.
It's at this point that I'm looking for the best place to make calls to whatever's going to do the sending. And if it's worth creating this sending code to be generic enough to handle multiple types of sending (email, SMS, who knows...)
So far, it has seemed right to put these email sending calls inside the Contoller actions. The first place I've been working on is the Document::save() action. Although right after that I know I'm going to want a Section::notify() action to allow ad-hoc sending out of notices.
How have ya'll handled email sending in your apps. Do you route to a special controller to handle the emails, or just put the email-sending calls directly in the controllers that need it?
In the case of my current project, a content manager, I'm wanting to send out the new versions of documents to all the 'watchers' and to send out new documents to all the section subscribers. In both these cases, I've got the HTML contents of the document in a View object.
JCart pointed out very well, that the View shouldn't send anything, it should just return stuff. And that I should pass the View to whatever's going to handle the sending. I'm working on converting my Views to do just that (up to now I've been letting the View object do the sending). I should have been doing this all along.
It's at this point that I'm looking for the best place to make calls to whatever's going to do the sending. And if it's worth creating this sending code to be generic enough to handle multiple types of sending (email, SMS, who knows...)
So far, it has seemed right to put these email sending calls inside the Contoller actions. The first place I've been working on is the Document::save() action. Although right after that I know I'm going to want a Section::notify() action to allow ad-hoc sending out of notices.
How have ya'll handled email sending in your apps. Do you route to a special controller to handle the emails, or just put the email-sending calls directly in the controllers that need it?