简体   繁体   中英

sending email issue in codeigniter

I have config email in codeigniter

    'protocol' => 'smtp',
                    'smtp_host' => 'ssl://smtp.gmail.com',
                    'smtp_port' => '465',
                    'smtp_user' => '-----',
                    'smtp_pass' => '-----',
                    'mailtype' => 'html',
                    'charset' => 'utf-8'

$this->load->library('email', $this->session->userdata('config'));
            $this->email->from('new@gmail.com', 'Rtlx Team');
            $this->email->to($email);
            $message = "Dear ";
            $this->email->subject('Rtlx Team - Account Verification');
            $this->email->message($message);
            $this->email->send();
            $this->email->clear();

But it shows following errors:

Message: mail() [function.mail]: Failed to connect to mail server at "localhost" port 465, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()

Message: mail() expects parameter 1 to be string, array given

Unable to send email using PHP mail(). Your server might not be configured to send mail using this method.

You are loading email library incorrectly as $this->load->library('email',$this->session->userdata('config')); .

so load it as $this->load->library('email'); and initialize it as $this->email->initialize($this->session->userdata('config'));

in below example i have taken $config array but you can take your own configuration in session also as in question you have used. but set all option as i have set in $config you can set it in session's variable.

full code below:

$config = Array(
    'protocol' => 'smtp',
    'smtp_host' => 'ssl://smtp.googlemail.com',
    'smtp_port' => 465,
    'smtp_user' => 'your@mail.com', // change it to yours
    'smtp_pass' => 'yourpassword', // change it to yours
    'mailtype' => 'html',// it can be text or html
    'wordwrap' => TRUE,
    'newline' => "\r\n",
    'charset' => 'utf-8',
    );
    $this->load->library('email');
    $this->email->initialize($config);
    $this->email->from('your@mail.com',"Rtlx Team");
    $this->email->to('receiver@mail.com');
    $this->email->subject('Subject');
    $this->email->message('Sample message');
    if (!$this->email->send()) 
    {
        show_error($this->email->print_debugger()); 
    }
    else 
    {
        echo 'Your e-mail has been sent!';
    }

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