title: PHPMailer exception error
body: <p>I've written my own Code Igniter model for sending emails. All was fine until recently when I started to get this error:</p>
  
  <p>Fatal error: Cannot redeclare class phpmailerException in /home/mysite/public_html/subdir/application/libraries/phpmailer/class.phpmailer.php on line 2319</p>
  
  <p>I'm using:</p>
  
  <p>CodeIgniter 2
  PHPMailer 5.1</p>
  
  <p>I've tried the following to resolve it:</p>
  
  <ul>
  <li>Added "$mail->SMTPDebug  = 0" to turn off errors. </li>
  <li>Added:    "$mail->MailerDebug = false;" </li>
  <li>Modified the PHPMailer to only show    errors when SMTPDebug is turned on. </li>
  <li>Looked for and removed any echo    statements </li>
  <li>Added try / catch blocks Tried adding / removing: $mail =    new PHPMailer(true);</li>
  </ul>
  
  <p>Here is my controller method (company/contact) which calls my model (message_model):</p>
  
  <pre><code>    function contact()
      {
          //Do settings.
          $this-&gt;options-&gt;task='email';
          $this-&gt;options-&gt;change = 'sent';
          $this-&gt;options-&gt;form_validation='';
          $this-&gt;options-&gt;page_title='Contact Us';
  
          //Import library
          include_once('application/libraries/recaptcha/recaptchalib.php');//Include recaptcha library.
  
          //Keys for recaptcha, stored in mainconfig file.
          $this-&gt;options-&gt;publickey = $this-&gt;config-&gt;item('recaptcha_public');
          $this-&gt;options-&gt;privatekey = $this-&gt;config-&gt;item('recaptcha_private');      
  
          //Form validation
          $this-&gt;form_validation-&gt;set_error_delimiters('&lt;div class="error"&gt;', '&lt;/div&gt;');
          $this-&gt;form_validation-&gt;set_rules('name_field','Name of problem','trim|required|min_length[3]|max_length[100]');
          $this-&gt;form_validation-&gt;set_rules('desc_field','Description','trim|required|min_length[10]|max_length[2000]');
          $this-&gt;form_validation-&gt;set_rules('email_field','Your email address','trim|required|valid_email');
  $this-&gt;form_validation-&gt;set_rules('recaptcha_response_field','captcha field','trim|required|callback__check_recaptcha');
  
  //If valid.
          if( $this-&gt;form_validation-&gt;run() )
          {
    //Set email contents.
              $message="This is a message from the contact form on ".$this-&gt;config-&gt;item('site_name')."&lt;br /&gt;&lt;br /&gt;";
              $message.=convert_nl($this-&gt;input-&gt;post('desc_field'));
              $message.="&lt;br /&gt;&lt;br /&gt;Reply to this person by clicking this link: &lt;a href=\"mailto:".$this-&gt;input-&gt;post('email_field')."\"&gt;".$this-&gt;input-&gt;post('name_field')."&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;";
  
    $options = array('host'=&gt;$this-&gt;config-&gt;item('email_host'),//mail.fixilink.com
              'username'=&gt;$this-&gt;config-&gt;item('email_username'),
              'password'=&gt;$this-&gt;config-&gt;item('email_password'),
              'from_name'=&gt;$this-&gt;input-&gt;post('name_field'),
              'to'=&gt;array($this-&gt;config-&gt;item('email_to')=&gt;$this-&gt;config-&gt;item('email_to') ),
              'cc'=&gt;$this-&gt;config-&gt;item('email_cc'),
              'full_name'=&gt;$this-&gt;input-&gt;post('name_field'),
              'subject'=&gt;'Email from '.$this-&gt;config-&gt;item('site_name').' visitor: '.$this-&gt;input-&gt;post('name_field'),
              'message'=&gt;$message,
              'word_wrap'=&gt;50,
              'format'=&gt;$this-&gt;config-&gt;item('email_format'),
              'phpmailer_folder'=&gt;$this-&gt;config-&gt;item('phpmailer_folder')
              );
  
    //Send email using own email class and phpmailer.
              $result = $this-&gt;message_model-&gt;send_email($options);
  
              //Second email to sender
    //Set email contents.
              $message="Thank you for your enquiry, we aim to get a reply to you within 2 working days.  In the meantime, please do follow us on www.facebook.com/autismworksuk";
  
              $options = array('host'=&gt;$this-&gt;config-&gt;item('email_host'),//mail.fixilink.com
              'username'=&gt;$this-&gt;config-&gt;item('email_username'),
              'password'=&gt;$this-&gt;config-&gt;item('email_password'),
              'from_name'=&gt;$this-&gt;input-&gt;post('name_field'),
              'to'=&gt;$this-&gt;input-&gt;post('email_field'),
              'full_name'=&gt;$this-&gt;input-&gt;post('name_field'),
              'subject'=&gt;'Email from '.$this-&gt;config-&gt;item('site_name'),
              'message'=&gt;$message,
              'word_wrap'=&gt;50,
              'format'=&gt;$this-&gt;config-&gt;item('email_format'),
              'phpmailer_folder'=&gt;$this-&gt;config-&gt;item('phpmailer_folder')
              );
  
    //Send email using own email class and phpmailer.
              $result = $this-&gt;message_model-&gt;send_email($options);           
  
              //Set result.    
              if($result==-1)
                  $this-&gt;session-&gt;set_flashdata('result', ucfirst($this-&gt;options-&gt;task).' was not '.$this-&gt;options-&gt;change.' because of a database error.');
              elseif($result==0)
                  $this-&gt;session-&gt;set_flashdata('result', 'No changes were made.');
              else
                  $this-&gt;session-&gt;set_flashdata('result', ucfirst($this-&gt;options-&gt;task).' was '.$this-&gt;options-&gt;change.' successfully.');
  
              //Redirect to completed controller.
              redirect('completed');
          }
  
  //Validation failed or first time through loop.
          $this-&gt;load-&gt;view('company/contact_view.php',$this-&gt;options);
      }
  </code></pre>
  
  <p>Here is my model's method to send the emails. It used to work but without any changes I can think of now I get an exception error:</p>
  
  <pre><code>    function send_email($options=array())
      {
          if(!$this-&gt;_required(array('host','username','password','from_name','to','full_name','subject','message'),$options))//check the required options of email and pass aggainst provided $options.
            return false;
  
          $options = $this-&gt;_default(array('word_wrap'=&gt;50,'format'=&gt;'html','charset'=&gt;'utf-8'),$options);
  
  try
  {        
    if(isset($options['phpmailer_folder']))
      require($options['phpmailer_folder']."/class.phpmailer.php");
    else
      require("application/libraries/phpmailer/class.phpmailer.php");//Typical CI 2.1 folder.
  
    $mail = new PHPMailer();
    $mail-&gt;MailerDebug = false;
  
    //Set main fields.
    $mail-&gt;SetLanguage("en", 'phpmailer/language/');
    $mail-&gt;IsSMTP();// set mailer to use SMTP
    $mail-&gt;SMTPDebug  = 0;  
  
    $mail-&gt;Host =     $options['host'];
    $mail-&gt;SMTPAuth = TRUE;     // turn on SMTP authentication
    $mail-&gt;Username = $options['username'];
    $mail-&gt;Password = $options['password'];
    $mail-&gt;FromName = $options['from_name'];//WHo is the email from.
    $mail-&gt;WordWrap = $options['word_wrap'];// Set word wrap to 50 characters default.
    $mail-&gt;Subject =  $options['subject'];
    $mail-&gt;Body    =  $options['message'];        
    $mail-&gt;CharSet =  $options['charset'];
  
    //From is the username on the server, not sender email.
    if(isset($options['from']))
      $mail-&gt;From = $options['from'];
    else
      $mail-&gt;From = $mail-&gt;Username;  //Default From email same as smtp user
  
    //Add reply to.
    if(isset($options['reply_to']))
      $mail-&gt;AddReplyTo($options['reply_to'], $options['from']);
  
    if(isset($options['sender']))
      $mail-&gt;Sender = $options['sender'];
  
    //Add recipients / to field (required)
    if(is_array($options['to']))
    {
      foreach($options['to'] as $to =&gt;$fn)
        $mail-&gt;AddAddress($to, $fn);
    }
    else
    {
      $mail-&gt;AddAddress($options['to']); //Email address where you wish to receive/collect those emails.
    }
  
    //Add cc to list if exists. Must be an array
    if(isset($options['cc']))
    {
      if(is_array($options['cc']))
      {
        foreach($options['cc'] as $to =&gt;$fn)
          $mail-&gt;AddCC($to, $fn);
      }
      else
      {
        log_message('debug', '----&gt;CC field must be an array for use with Message_Model.');
      }
    }
  
    //Add bcc to list if exists. Must be an array
    if(isset($options['bcc']))
    {
      if(is_array($options['bcc']))
      {
        foreach($options['bcc'] as $to =&gt;$fn)
          $mail-&gt;AddBCC($to, $fn);
      }
      else
      {
        log_message('debug', '----&gt;BCC field must be an array for use with Message_Model.');
      }
    }
  
    //Alternative text-only body.
    if(isset($options['alt_body']))
      $mail-&gt;AltBody=$options['alt_body'];
    else
      $mail-&gt;AltBody = htmlspecialchars_decode( strip_tags( $options['message'] ),ENT_QUOTES );//Strip out all html and other chars and convert to plain text.
  
    //Plain/html format.
    if(isset($options['format']))
    {
      if($options['format']=='html')
        $mail-&gt;IsHTML(true);                                  // set email format to HTML
    }     
  
    //Send email and set result.
    $return['message']='';
  
    if(!$mail-&gt;Send())
    {
     $return['message'].= "Message could not be sent.&lt;br /&gt;\n";
     $return['message'].= "Mailer Error: " . $mail-&gt;ErrorInfo."\n";
     $return['result'] = 0;
    }
    else
    {
      $return['message'].= "Message has been sent successfully.\n";
      $return['result'] = 1;
    }
  }
  catch (phpmailerException $e)
  {
    log_message('error', '----&gt;PHPMailer error: '.$e-&gt;errorMessage() );
  }
  catch (Exception $e) 
  {
    log_message('error', '----&gt;PHPMailer error: '.$e-&gt;errorMessage() );
  }
          return $return;
      }
  </code></pre>