0
<?php
class SendEmail extends Controller
{
    
    function SendEmail()
    {
        parent::Controller();
        $this->load->library('email'); // load the library
    }
    
    function index()
    {
        $this->sendEmail();
    }
    
    public function sendEmail()
    {
        // Email configuration
        $config = Array(
            'protocol' => 'smtp',
            'smtp_host' => 'smtp.gmail.com.',
            'smtp_port' => 465,
            'smtp_user' => 'xxxx', // change it to yours
            'smtp_pass' => 'xxxx', // change it to yours
            'mailtype' => 'html',
            'charset' => 'iso-8859-1',
            'wordwrap' => TRUE
        );
        
        $this->load->library('email', $config);
        $this->email->from('xxxx', "Admin Team");
        $this->email->to("xxxx");
        $this->email->cc("xxxx");
        $this->email->subject("This is test subject line");
        $this->email->message("Mail sent test message...");
        
        $data['message'] = "Sorry Unable to send email...";
        if ($this->email->send()) {
            $data['message'] = "Mail sent...";
        }
        
        // forward to index page
        $this->load->view('index', $data);
    }
    
}
?>

I get an error - I am doing it in codeigniter PHP Error was encountered Severity: Compile Error

Message: Cannot redeclare SendEmail::sendEmail()

Filename: controllers/SendEmail.php

Line Number: 16

Backtrace:

Dharman
  • 30,962
  • 25
  • 85
  • 135

1 Answers1

0

Controller code

<?php
    class SendEmail extends Controller
    {
        
        function __construct()
        {
            parent::__construct();
            $this->load->library('email'); // load the library
        }
        
        function index()
        {
            $this->sendEmail();
        }
        
        public function sendEmail()
        {
            // Email configuration
            $config = Array(
                'protocol' => 'smtp',
                'smtp_host' => 'smtp.gmail.com.',
                'smtp_port' => 465,
                'smtp_user' => 'xxxx', // change it to yours
                'smtp_pass' => 'xxxx', // change it to yours
                'mailtype' => 'html',
                'charset' => 'iso-8859-1',
                'wordwrap' => TRUE
            );
            
            $this->load->library('email', $config);
            $this->email->from('xxxx', "Admin Team");
            $this->email->to("xxxx");
            $this->email->cc("xxxx");
            $this->email->subject("This is test subject line");
            $this->email->message("Mail sent test message...");
            
            $data['message'] = "Sorry Unable to send email...";
            if ($this->email->send()) {
                $data['message'] = "Mail sent...";
            }
            
            // forward to index page
            $this->load->view('index', $data);
        }
        
    }
    ?>
Jimit H.
  • 185
  • 1
  • 11
  • A PHP Error was encountered Severity: Warning Message: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() – Apratim dutta choudhury Feb 08 '21 at 09:26
  • you can not send mail using localhost. so you can use gmail smtp. reference link https://www.gmass.co/blog/gmail-smtp/ or https://support.cloudways.com/configure-gmail-smtp/ – Jimit H. Feb 08 '21 at 09:37
  • Done with cloud ways but still the same error - PHP Error was encountered Severity: Warning Message: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() – – Apratim dutta choudhury Feb 08 '21 at 10:42
  • After setting cloudways do i need to alter any code or just use xampp and use the same way we launch project – Apratim dutta choudhury Feb 08 '21 at 10:46
  • Refre https://stackoverflow.com/questions/1555145/sending-email-with-gmail-smtp-with-codeigniter-email-library – Jimit H. Feb 08 '21 at 11:11