cakePHP wrapper for EmailComponent::send()

As I already wrote in a previous post, the cakePHP EmailComponentseems to have some quirks, forcing me to add more code to send an email.

I stumbled upon one more issue today : a View poisoning coming from the EmailComponent. Some vars I was passing to my view through the use of a custom Helperwere wipe away if I send a mail in the same request.

It took me some time to track it down, but I finally decided that I'll now wrap calls to $this->Email->send() into a __sendEmail() custom method where I'll add my tweaks.

This will help migrating to a newer cake version easier when those bugs will be fixed.

Easy debug

Debugging emails is not an easy task, but is greatly eased by cake. One can set the delivery type to debugand the mail will be generated like a normal mail, but won't be sent. Instead, its content will be saved in Session.

My wrapper will allow switching from normal to debug mail thanks to an argument.

View cleaning

I'll also clear the ClassRegistryfrom the Viewcreated by the EmailComponent. The Emailview is shared with the display view and this can result in vars being lost or not correctly set. I think all those troubles are gone in cake 2.0 but I haven't tested it yet.

The __sendMail method

Here is the method code. You should add it to your AppController :

function __sendMail($sendAsDebug = false) {
  if (empty($this->Email)) {
    return false;
  }
  // Debug mode
  if (!empty($sendAsDebug)) {
    $this->Email->delivery = 'debug';
  }
  // We force adding the boundaries and header otherwise some webmail (like SquirelMail) won't correctly display them
  $this->Email->_createboundary();
  $this->Email->__header[] = 'MIME-Version: 1.0';
  $this->Email->send();
  // We display debug info
  if (!empty($sendAsDebug)) {
    debug($this->Session->read('Message.email.message'), true);
  }
  // We also need to clear the generated view so our display does not get poisoned by the Email display
  ClassRegistry::removeObject('view');
}

Getting raw JSON POST in PHP

Sometimes you need to post raw POSTdata without following the key/value convention, like when you need to post to some webservices.

Accessing it from PHP can be a little obsure at first, but all that is needed is to read the input buffer by doing : file_get_contents("php://input")

This can be useful when posting raw JSON without adding the overhead of all the keys.

Malformed POST request made to an absolute url with Flash

Sometimes you encounter bugs that smacks you in the face with their strangeness. And even when you defeated them you still have this sour taste of puzzlement.

I encountered one of them today.

While testing in a local environment and doing POST requests from Flash to a distant cake app, I could get the request just fine.

But when uploading the SWF on the server and embedding it in a Facebook App Iframe, the POST data seemed corrupted and I couldn't get it.

Replacing the distant POST call to a local one (removing the whole http:// part) fixed this strange bug.

So we ended by adding a check to see if we were online or local and make an absolute or relative call depending on the environment.

So far, no more issues.

Going to parent folder in Windows 7 Explorer

At first I thought I missed an option somewhere, but now I have to admit : Microsoft removes the "Go to parent folder" button in the Windows 7 version of Explorer.

I like their new implementation of folder path, but a fast click to go up one level, positionned in a consistent way in the UI is a plus.

After some research I found the Alt + Up keyboard shortcut to achieve the same result, but while navigating through the explorer, I always am using my mouse. Switching from mouse to keyboard is tedious, so I hunt for another solution.

I finally installed ClassicShellthat adds the missing button as well as some other minor cosmetics changes.

Protecting a directory using HTTP Auth on Dreamhost with cakePHP

One can protect the browsing of a special directory with a simple set of login/password by using appropriate .htaccess/.htpasswd files.

The classic way

Just create an .htaccess in the directory you want to protect with the following lines :

AuthName "Restricted Access"
AuthType Basic
AuthUserFile /full/path/to/your/.htpasswd
<Limit GET POST PUT>
Require valid-user
</Limit>

And to create the .htpasswd file, run the following command :

htpasswd -c /full/path/to/your/.htpasswd username

The -c modifier will create the file, omit it if you only want to add a new user. Also change the path to your .htpasswd file (moving it out of the webdir could be a good idea) and change usernameto any login you want.

You'll then be prompted to enter the password (twice) and your file will be generated.

cakePHP and Dreamhost fixed

I had an issue when protecting a folder in my app/webroot/folder on Dreamhost. I'm not sure it is completly cake related nor Dreamhost related but the two together made it quite hard to debug.

Anyway, it appears that when issuing an HTTP Auth, Dreamhost redirect to a file named /failed_auth.html (this is the file you're supposed to see when your Auth fails, obviously).

But as I didn't have such a file in my app, everytime I tried to access my protected dir, I got my custom 404 error page.

To finally fix that, all I had to do was to create a real failed_auth.html page, or in my case, create a Route that redirect failed_auth.html to a custom failed auth page.

I guess just dropping a failed_auth.html file in app/webroot/could have done the trick too.