如何使用php codeigniter发送电子邮件?

时间:2021-02-08 08:53:21

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

我收到一个错误 - 我在 codeigniter 中执行此操作 PHP 遇到错误 严重性:编译错误

消息:无法重新声明 SendEmail::sendEmail()

文件名:controllers/SendEmail.php

行号:16

回溯:

1 个答案:

答案 0 :(得分:0)

控制器代码

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