CodeIgniter Forums
[SOLVED]disable mail - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: [SOLVED]disable mail (/showthread.php?tid=18630)

Pages: 1 2 3


[SOLVED]disable mail - El Forum - 05-13-2009

[eluser]rogierb[/eluser]
Hi,

I need to setup a demo system with full functionality except for mail.

Is there an easy way to disable sending email from codeigniter without hacking the email library? Like setting the library to 'demo' or 'test' mode?

Thanx,

Rogier


[SOLVED]disable mail - El Forum - 05-13-2009

[eluser]TheFuzzy0ne[/eluser]
Code:
class MY_CI_Mail extends CI_Email
{
    function send($return_val=TRUE)
    {    
        if ($this->config->item('dev_mode') === TRUE)
            return $return_val;

        if ($this->_replyto_flag == FALSE)
        {
            $this->reply_to($this->_headers['From']);
        }
    
        if (( ! isset($this->_recipients) AND ! isset($this->_headers['To']))  AND
            ( ! isset($this->_bcc_array) AND ! isset($this->_headers['Bcc'])) AND
            ( ! isset($this->_headers['Cc'])))
        {
            $this->_set_error_message('email_no_recipients');
            return FALSE;
        }

        $this->_build_headers();

        if ($this->bcc_batch_mode  AND  count($this->_bcc_array) > 0)
        {
            if (count($this->_bcc_array) > $this->bcc_batch_size)
                return $this->batch_bcc_send();
        }

        $this->_build_message();

        if ( ! $this->_spool_email())
        {
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }
}

Hopefully this is what you're after. I haven't tested it but basically, if dev_mode is set to TRUE in the config.php file, it won't send. Obviously the rest of the Email class is fully functional, so it may end up doing a bit of work for nothing.


[SOLVED]disable mail - El Forum - 05-13-2009

[eluser]Dam1an[/eluser]
Although its not built in (which I think it should), there's a very easy and clean way to do it

Create an email library extending the default one
Replace the send function by creating your own
Have an optional parapemter for success (default to true)
The reason for the optional paramter is so you can test what happens if it would fail
Check if a config item for test mode exists
If in test mode, return true or false for success
If not in test mode, call the real send method

I'll mock some code up later and post it back (unless my description was so clear and easy you don't need it)

EDIT: Fuzzy beat me


[SOLVED]disable mail - El Forum - 05-13-2009

[eluser]TheFuzzy0ne[/eluser]
Great idea. I've updated my code to encompass your suggestion.


[SOLVED]disable mail - El Forum - 05-13-2009

[eluser]xwero[/eluser]
I wonder why there is a need to extend the email library to disable a feature in a custom developed system? Wouldn't it be easier to set a flag to show or hide the mail functionality in the system.


[SOLVED]disable mail - El Forum - 05-13-2009

[eluser]Dam1an[/eluser]
@xwero: Not sure if I understand what you're saying, but the aim of this isn't to stop the system from sending emails in the sense of removing that functionality... Its just to stop you receiving hundreds of emails during development (Once you've obviously tested that it works)


[SOLVED]disable mail - El Forum - 05-13-2009

[eluser]rogierb[/eluser]
Hi guys,

Thanks a bundle, totally forgot I could extend the library.


Regards,
Rogier


[SOLVED]disable mail - El Forum - 05-13-2009

[eluser]xwero[/eluser]
I got that after your remark but i still think overwriting the send method of the email class for a system specific functionality is a bad idea. It should be handled inside the system. This makes it possible to prevent mail content generating actions in the process.


[SOLVED]disable mail - El Forum - 05-13-2009

[eluser]TheFuzzy0ne[/eluser]
xwero, I still don't follow. Please could you give an example of what you're suggesting?


[SOLVED]disable mail - El Forum - 05-13-2009

[eluser]Dam1an[/eluser]
But because its a system specific thing, it goes in the application libraries, not the system ones
Also, if as you earlier suggest you set a flag to hide email functionality, that will either break any code which calls the email lib (during dev), or you would have to add this check in every method)