Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7976673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:58:18+00:00 2026-06-04T08:58:18+00:00

I’ve written my own Code Igniter model for sending emails. All was fine until

  • 0

I’ve written my own Code Igniter model for sending emails. All was fine until recently when I started to get this error:

Fatal error: Cannot redeclare class phpmailerException in /home/mysite/public_html/subdir/application/libraries/phpmailer/class.phpmailer.php on line 2319

I’m using:

CodeIgniter 2
PHPMailer 5.1

I’ve tried the following to resolve it:

  • Added “$mail->SMTPDebug = 0” to turn off errors.
  • Added: “$mail->MailerDebug = false;”
  • Modified the PHPMailer to only show errors when SMTPDebug is turned on.
  • Looked for and removed any echo statements
  • Added try / catch blocks Tried adding / removing: $mail = new PHPMailer(true);

Here is my controller method (company/contact) which calls my model (message_model):

    function contact()
    {
        //Do settings.
        $this->options->task='email';
        $this->options->change = 'sent';
        $this->options->form_validation='';
        $this->options->page_title='Contact Us';

        //Import library
        include_once('application/libraries/recaptcha/recaptchalib.php');//Include recaptcha library.

        //Keys for recaptcha, stored in mainconfig file.
        $this->options->publickey = $this->config->item('recaptcha_public');
        $this->options->privatekey = $this->config->item('recaptcha_private');      

        //Form validation
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        $this->form_validation->set_rules('name_field','Name of problem','trim|required|min_length[3]|max_length[100]');
        $this->form_validation->set_rules('desc_field','Description','trim|required|min_length[10]|max_length[2000]');
        $this->form_validation->set_rules('email_field','Your email address','trim|required|valid_email');
$this->form_validation->set_rules('recaptcha_response_field','captcha field','trim|required|callback__check_recaptcha');

//If valid.
        if( $this->form_validation->run() )
        {
  //Set email contents.
            $message="This is a message from the contact form on ".$this->config->item('site_name')."<br /><br />";
            $message.=convert_nl($this->input->post('desc_field'));
            $message.="<br /><br />Reply to this person by clicking this link: <a href=\"mailto:".$this->input->post('email_field')."\">".$this->input->post('name_field')."</a><br /><br />";

  $options = array('host'=>$this->config->item('email_host'),//mail.fixilink.com
            'username'=>$this->config->item('email_username'),
            'password'=>$this->config->item('email_password'),
            'from_name'=>$this->input->post('name_field'),
            'to'=>array($this->config->item('email_to')=>$this->config->item('email_to') ),
            'cc'=>$this->config->item('email_cc'),
            'full_name'=>$this->input->post('name_field'),
            'subject'=>'Email from '.$this->config->item('site_name').' visitor: '.$this->input->post('name_field'),
            'message'=>$message,
            'word_wrap'=>50,
            'format'=>$this->config->item('email_format'),
            'phpmailer_folder'=>$this->config->item('phpmailer_folder')
            );

  //Send email using own email class and phpmailer.
            $result = $this->message_model->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'=>$this->config->item('email_host'),//mail.fixilink.com
            'username'=>$this->config->item('email_username'),
            'password'=>$this->config->item('email_password'),
            'from_name'=>$this->input->post('name_field'),
            'to'=>$this->input->post('email_field'),
            'full_name'=>$this->input->post('name_field'),
            'subject'=>'Email from '.$this->config->item('site_name'),
            'message'=>$message,
            'word_wrap'=>50,
            'format'=>$this->config->item('email_format'),
            'phpmailer_folder'=>$this->config->item('phpmailer_folder')
            );

  //Send email using own email class and phpmailer.
            $result = $this->message_model->send_email($options);           

            //Set result.    
            if($result==-1)
                $this->session->set_flashdata('result', ucfirst($this->options->task).' was not '.$this->options->change.' because of a database error.');
            elseif($result==0)
                $this->session->set_flashdata('result', 'No changes were made.');
            else
                $this->session->set_flashdata('result', ucfirst($this->options->task).' was '.$this->options->change.' successfully.');

            //Redirect to completed controller.
            redirect('completed');
        }

//Validation failed or first time through loop.
        $this->load->view('company/contact_view.php',$this->options);
    }

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:

    function send_email($options=array())
    {
        if(!$this->_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->_default(array('word_wrap'=>50,'format'=>'html','charset'=>'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->MailerDebug = false;

  //Set main fields.
  $mail->SetLanguage("en", 'phpmailer/language/');
  $mail->IsSMTP();// set mailer to use SMTP
  $mail->SMTPDebug  = 0;  

  $mail->Host =     $options['host'];
  $mail->SMTPAuth = TRUE;     // turn on SMTP authentication
  $mail->Username = $options['username'];
  $mail->Password = $options['password'];
  $mail->FromName = $options['from_name'];//WHo is the email from.
  $mail->WordWrap = $options['word_wrap'];// Set word wrap to 50 characters default.
  $mail->Subject =  $options['subject'];
  $mail->Body    =  $options['message'];        
  $mail->CharSet =  $options['charset'];

  //From is the username on the server, not sender email.
  if(isset($options['from']))
    $mail->From = $options['from'];
  else
    $mail->From = $mail->Username;  //Default From email same as smtp user

  //Add reply to.
  if(isset($options['reply_to']))
    $mail->AddReplyTo($options['reply_to'], $options['from']);

  if(isset($options['sender']))
    $mail->Sender = $options['sender'];

  //Add recipients / to field (required)
  if(is_array($options['to']))
  {
    foreach($options['to'] as $to =>$fn)
      $mail->AddAddress($to, $fn);
  }
  else
  {
    $mail->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 =>$fn)
        $mail->AddCC($to, $fn);
    }
    else
    {
      log_message('debug', '---->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 =>$fn)
        $mail->AddBCC($to, $fn);
    }
    else
    {
      log_message('debug', '---->BCC field must be an array for use with Message_Model.');
    }
  }

  //Alternative text-only body.
  if(isset($options['alt_body']))
    $mail->AltBody=$options['alt_body'];
  else
    $mail->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->IsHTML(true);                                  // set email format to HTML
  }     

  //Send email and set result.
  $return['message']='';

  if(!$mail->Send())
  {
   $return['message'].= "Message could not be sent.<br />\n";
   $return['message'].= "Mailer Error: " . $mail->ErrorInfo."\n";
   $return['result'] = 0;
  }
  else
  {
    $return['message'].= "Message has been sent successfully.\n";
    $return['result'] = 1;
  }
}
catch (phpmailerException $e)
{
  log_message('error', '---->PHPMailer error: '.$e->errorMessage() );
}
catch (Exception $e) 
{
  log_message('error', '---->PHPMailer error: '.$e->errorMessage() );
}
        return $return;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T08:58:20+00:00Added an answer on June 4, 2026 at 8:58 am

    Basically one of two things is happening:

    1. You are “including” your PHP code twice somewhere, causing the 2nd time to generate the redeclaration error

    2. You are using “phpmailerException” somewhere else, besides your model. Have you tried to do a “find all” in your IDE for ALL calls to “phpmailerException” – perhaps you used this name in another area for another exception?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.