Bonfire Secrets: Debugging emails via text files

If you’re like me, you do most of your development work on a local desktop environment. For me, that means running the latest version of VertrigoServ on a Windows 7 desktop using a copy of PHPStorm and the Git command line interface.

While this setup provides an almost instant way to code and test my work, it provides one big limitation when working with PHP, there is no sendmail. I’ve seen tools like The Windows sendmail program but the time to actually install and troubleshoot desktop email is just to worth it.

So while working on developing workflows for my OOTP Fantasy leagues and OOTP Open Web Platform tools that required email messages to be sent, I wrote a small function to decide to automatically write the email output to local HTML files when under the development environment. That way I could develop the functionality and assure the email content was correct without having to constantly push my work to a remote server with sendmail.

I implemented this for Codeigniter as a standalone helper function:

function sendEmail($to,$fromEmail, $fromName,$subject,$message,$to_name = '',$filePrefix = 'email_') {

    $ci =& get_instance();

    if ((!defined('ENVIRONMENT') || (defined('ENVIRONMENT') && ENVIRONMENT != 'development'))) {
        if ($ci->email->send()) {
            return true;
        } else {
            return false;
        } // END if
    } else {
        if (!function_exists('write_file')) {
        } // END if
        return true;
    } // END if

Nothing crazy being done here really. I load a instance of the Codeigniter app and build the email via the email class methods. The logic whether to send to sendmail or write to a file occurs on line 9. I test for the environment variable (which I custom added to my fantasy mod as it used CodeIgniter 1.7.3 but is now a part of version 2.x). If the environment is not development (therefore it’s QA or production) the email is sent via the email classes send() method. Otherwise I load the file helper and write the contents to a local file. Pretty simple.

In practical application, I was able to do this:

$msg = $this->lang->line('team_trade_offer');
$msg .= $this->lang->line('email_footer');
$data['title'] = $this->lang->line('team_email_title_trade_offer');
$data['messageBody']= $msg;
$emailMess = $this->load->view($this->config->item('email_templates').'general_template', $data, true);

$subject = str_replace('[LEAGUE_NAME]',$this->league_model->getLeagueName($this->dataModel->league_id),$this->lang->line('team_trade_email_subject_offer_to'));

$error = !sendEmail($email,$email2, $username, $subject, $emailMess, '','email_trd_offer_');

You can do this in Bonfire too

I recently submitted this same type of feature to Bonfire 0.6 developers and they accepted it as part of their 0.6 project.  So in Bonfire 0.6, you can write email content to files if you’re project:

  • Is set to use the development environment
  • You set the appropriate application config setting to true.

To turn on email debugging in Bonfire 0.6+:

  • Open the file bonfire/application/config/application.php
  • Scroll down to the !Emailer section
  • Change $config[’emailer.write_to_file’] to true
  • Save

Bonfire automatically writes and emails generated by the application to the bonfire log directory (bonfire/application/logs).

It’s a small little addition, but it sometimes can save a lot of time rather than saving, pushing code, running the email code and waiting for the email to arrive only to have to repeat the process more than once.  Hope it helps you as well.


Jeff Fox is an over twenty-year web developer and digital user experience technology leader. Jeff cut his teeth in the Web's early days and is mainly self-taught in his professional skills. Having worked for a broad number of companies has helped build skills in development, organization and public speaking. In addition to being a passionate developer and technical speaker, Jeff is a dedicated tech and sci-fi geek gladly indulging in Doctor Who and Star Wars marathons. He is also a talented musician, writer and proud father of three little Foxies. And don't get him started about his San Francisco Giants.

Leave a Reply

Your email address will not be published.


This site uses Akismet to reduce spam. Learn how your comment data is processed.