CodeIgniter: A simple solution to the SAFE_MODE mail problem
Posted: March 7th, 2007 | Author: Panos Karageorgakis | Filed under: CodeIgniter | No Comments »The problem
Enhancing an existing web app built with CodeIgniter (or Code Igniter, or CI for short), I wanted to add an automatic e-mail message functionality. This way, when the contact form is submitted, except for the inquiry being presented in the administrative backend, an e-mail would be sent to the site’s owner address to notify them that someone wants to contact them.
Reading the CI manual, I was fascinated to see how easy this is to implement. I injected a few lines of code in my controller and tested it locally. Voila, it worked! And I didn’t have to configure anything. This was awesome, but let’s try it at the server too, shall we?
The production server, utilizing a shared hosting environment in a VPS, was expected to be more strict in such matters, and it turned out it was. The same set of code returned the following error:
Message: mail(): SAFE MODE Restriction in effect. The fifth parameter is disabled in SAFE MODE.
Obviously, PHP’s safe mode is on, and this seems to be troubling the mail code. But turning safe mode off, in order for this to work, is not a good idea. Even if it’s not turned off for the whole server but just for this site, still that would somehow weaken the security of the website. So an alternate solution had to be found: a way to make CI work with safe mode.
The solution
After googling about this problem with no results, I was lucky to find out the solution is really simple. It turns out that CI code has already predicted this, and all you have to do is to tell it that safe mode is on. That can be done quite easily, by changing the following line in the system/application/libraries/Email.php file:
bq. var $_safe_mode = FALSE;
should be changed to
bq. var $_safe_mode = TRUE;
That’s it! You can now send mail from within a controller with no problem. (Don’t you wish though, that this was documented?)
Leave a Reply