Elegant/efficient means of sending email with Codeigniter and Sendgrid - codeigniter

I'm setting up some transactional email fun in our Codeigniter app via integrating with Sendgrid.
I've got things setup and ready to move forward with creating all of the specific transactions/emails, but I was wondering about the most efficient and/or elegant way of doing so.
It seems a bit convoluted to include the appropriate email code in each of the functions. To call a specific function from a clean and separated email controller would require me to use AJAX (so as to not cause a redirect).
Is there some way that I'm not considering currently that would help balance things, namely cleanliness and separation along with coherency and ease?
Thanks for any thoughts-

Not sure about the specifics of your application structure. But you could always create a model function and call that from the functions in your controller.
CodeIgniter also comes with some built in functionality to help you send emails, specifically setting some of your email settings in a config file so you don't have to rewrite that. http://ellislab.com/codeigniter/user-guide/libraries/email.html

I actually wrote a blog post about this not too terribly long ago. Have a look at this:
http://blog.sendgrid.com/using-sendgrid-with-php-codeigniter/
Essentially, codeigniter comes with an awesome email library that makes it easy to send stuff over SMTP, so I just show you how to hook into that.

Related

How do I make an ajax calls internally in Laravel without using the route system

In Laravel, I would like to know how to make ajax call internally without using the the route system. I'm not sure what other ways I can access internal methods, without routes.
The reason I'm looking for this is because the way the application is setup, routes always return html, even if a JSON response, because the framework is loaded inside the content area of a parent website (using different framework) which always had a header and footer(ugh, I know). I have another post about this, but lets please stick to the question.
I just need this framework to talk to itself, from the $.ajax() request, straight to the controller, bypassing the route, so maybe a JSONP, or some way to access methods directly without going overboard.
I know I did this sort of thing using FOSJsRouting from Symfony in a previous app to accomplish this sort of thing, but I had to make a wrapper to access the methods. I don't want that kind of elaborate setup if possible here.
Are there other ways?

phplist api - subscibe / edit / remove users by ajax

I have a very-simple php-based website.
I would like to add "subsribe for information" box and send it to some list-management tool.
I found that phplist is quite a good one. However, I don't find any api which allows me to subscribe users using ajax.
(I would like to avoid the pages of phplist in order to avoid confusion due to different design, and also I want the interface to be multi-language).
Of-course I can touch the code, but I doubt that it misses this feature, so I assunme I simply missed it.
Thanks!
Hopefully you have managed how to achieve this already. I have a different use case not requiring AJAX.
Anyway, I've found a script that tries to ease the addition of subscribers to your list. Maybe you could use it as the backend to support your AJAX request.
This is the thread in which the script is shared, as others have found it useful, also for unsuscribing users, even handling massive subscriptions/unsuscriptions:
http://forums.phplist.com/viewtopic.php?t=5009
Finally, be aware this code is using PEAR DB abstraction layer, probably I'm going to use this but without the PEAR package.
I look forward for what you could do through AJAX and this functions.

Why use MVC/router

I'm trying to comprehend the concept behind MVC and URL routing. I understand that it's good to seperate your code, hence MVC, but fail to understand the idea behind the URL router!
Instead of having a lot of rewrite rules in htaccess, I send all traffic to router.php, and in this page I have an array with page urls, and its corresponding PHP controller.
To keep it simple, I just include the controller, where the output finally is generated, however having seen lots of other practices, I'm afraid that im doing something wrong, or bad in some way..
Can someone please enlighten me, how to do a good, but simple URL router? Is it okay just to include the controller, which then generates the output? Perhaps someone has some information that describes the subject in details (something understandable for a beginner)
Thanks in advance
There are lots of ways to do URL routing. Some are client side like with backbone.js, others are server side. Doing it with .htaccess is one way, another is th way you are doing it by having a prerequisite path that is is either a hard path, or a regular expression that you parse and figure out where to send it. None of them are 100% right or 100% wrong, it's all preference, and it sounds like you are doing just fine with a route file.
For more information on how different frameworks do routing you should read over the docs on routing for CodeIgniter, and Symfony frameworks to see 2 different styles of server side routing, and then maybe look at the backbone.js framework for client side routing just to see the similarities and differences.
The router in the MVC concept decides which controller it has to load when a user requests a page. E.g. a user requests example.com/something/very/important, the router would now look for an action which is mapped to this route and execute it. There are different methods how you can accomplish that (simple include, instantiating a class and running a method etc.) but the most simple and still powerful solution I came up with is creating a separate class for every action. I've written a little article on that matter, since I've been asked this question several times, you can have a look at it here: Writing a simple and fast mvc router with PHP
The ASP.NET Routing module is responsible for mapping incoming browser requests to particular MVC controller actions.
Routing is a pattern matching system that monitor the incoming request and figure out what to do with that request. At runtime, Routing engine use the Route table for matching the incoming request's URL pattern against the URL patterns defined in the Route table. You can register one or more URL patterns to the Route table at Application_Start event.

How do I send an email from my webpage?

I am not a web developer but I do have a lot of programming experience in C# and Windows forms programming. On our company webpage my boss wants me to put in a textbox where visitors can submit a comment and press a submit button and that comment will be sent to an email address. Right now, our website uses just plain old html, no php or javascript or anything like that. I am wondering what is the simplest way to accomplish what I need? Can someone point me in the right direction? The website is hosted on an Apache server so I won't be able to use aspx.
The simplest method depends heavily on what is available. If PHP is supported, use it.
Here's a simple example (I wouldn't focus too much on their HTML -- which is a bit shoddy) but the PHP at the bottom to give you an idea on how to pull the <form> in and send the email.
If you don't have PHP and don't want to install it, you can do this without any server-side code and outsource the problem. Bravenet (a name that will be familiar with any old-school webdeveloper) have a free hosted form solution that lets you post your forms to their server and they email you the result.
Not amazingly professional, but takes about 10 seconds to implement.
The simplest solution would be have the form action as "mailto:email#address.com"
However, this has the downside of the email address being sent to being exposed to spam bots, along with the clients mail application having to load to send the email which can be confusing and slow.
Sending emails in PHP is common, and there are thousands of articles out there on how to do it, here's one
In this case the most simple way is to install PHP to your apache to use the mail()-function.
Of couse you could use tomcat additional to apache, but the configuaration is much more time-eating.
If you don't want to use any sort of scripting technology, then the form mailto might be your only option. You can just make the action of your HTML form mailto:youraddress and the form post will be mailed directly.
I would highly recommend looking into some sort of scripting technology though to do this in a more reliable way....PHP looks like a good fit in your environment.

Ajax architecture in Django application

I am trying to find the optimal architecture for an ajax-heavy Django application I'm currently building. I'd like to keep a consistent way of doing forms, validation, fetching data, JSON message format but find it exceedingly hard to find a solution that can be used consistently.
Can someone point me in the right direction or share their view on best practice?
I make everything as normal views which display normally in the browser. That includes all the replies to AJAX requests (sub pages).
When I want to make bits of the site more dynamic I then use jQuery to do the AJAX, or in this case AJAH and just load the contents of one of the divs in the sub page into the requesting page.
This technique works really well - it is very easy to debug the sub pages as they are just normal pages, and jQuery makes your life very easy using these as part of an AJA[XH]ed page.
For all the answers to this, I can't believe no one's mentioned django-piston yet. It's mainly promoted for use in building REST APIs, but it can output JSON (which jQuery, among others, can consume) and works just like views in that you can do anything with a request, making it a great option for implementing AJAX interactions (or AJAJ [JSON], AJAH, etc whatever). It also supports form validation.
I can't think of any standard way to insert ajax into a Django application, but you can have a look to this tutorial.
You will also find more details on django's page about Ajax
Two weeks ago I made a write up how I implement sub-templates to use them in "normal" and "ajax" request (for Django it is the same). Maybe it is helpful for you.
+1 to Nick for pages displaying normally in the browser. That seems to be the best starting point.
The problem with the simplest AJAX approaches, such as Nick and vikingosegundo propose, is that you'll have to rely on the innerHTML property in your Javascript. This is the only way to dump the new HTML sent in the JSON. Some would consider this a Bad Thing.
Unfortunately I'm not aware of a standard way to replicate the display of forms using Javascript that matches the Django rendering. My approach (that I'm still working on) is to subclass the Django Form class so it outputs bits of Javascript along with the HTML from as_p() etc. These then replicate the form my manipulating the DOM.
From experience I know that managing an application where you generate the HTML on the server side and just "insert" it into your pages, becomes a nightmare. It is also impossible to test using the Django test framework. If you're using Selenium or a similar tool, it's ok, but you need to wait for the ajax request to go return so you need tons of sleeps in your test script, which may slow down your test suite.
If the purpose of using the Ajax technique is to create a good user interface, I would recommend going all in, like the GMail interface, and doing everything in the browser with JavaScript. I have written several apps like this using nothing but jQuery, state machines for managing UI state and JSON with ReST on the backend. Django, IMHO, is a perfect match for the backend in this case. There are even third party software for generating a ReST-interface to your models, which I've never used myself, but as far as I know they are great at the simple things, but you of course still need to do your own business logic.
With this approach, you do run into the problem of duplicating code in the JS and in your backend, such as form handling, validation, etc. I have been thinking about solving this with generating structured information about the forms and validation logic which I can use in JS. This could be compiled at deploy-time and be loaded as any other JS file.
Also, avoid XML. The browsers are slow at parsing it, it is a pain to generate and a pain to work with in the browser. Use JSON.
Im currently testing:
jQuery & backbone.js client-side
django-piston (intermediate layer)
Will write later my findings on my blog http://blog.sserrano.com

Resources