简体   繁体   中英

How to send email using php codeigniter?

<?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:

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);
        }
        
    }
    ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM