Send email by Email Class in codeigniter with Gmai

2019-01-11 21:22发布

I want send a email by Email Class in codeigniter with gmail, but i get following error:

Error:

A PHP Error was encountered
Severity: Warning
Message: mail() [function.mail]: Failed to connect to mailserver at "ssl://smtp.googlemail.com" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()
Filename: libraries/Email.php
Line Number: 1553

This is my full function in controll:

function send_mail(){
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.googlemail.com';
$config['smtp_port'] = 465;
$config['smtp_user'] = 'xyz@gmail.com';
$config['smtp_pass'] = 'xxxxxxx';

$this->load->library('email', $config);
$this->email->set_newline("\r\n");

$this->email->from('neginph@gmail.com', 'Negin Phosphate Shomal');
$this->email->to('neginfos@yahoo.com');
$this->email->subject('This is an email test');
$this->email->message('It is working. Great!');

if($this->email->send())
{
    echo 'Your email was sent, successfully.';
}

else
{
    show_error($this->email->print_debugger());
}
}

I changed SMTP in php.ini as this:

SMTP = ssl://smtp.googlemail.com
smtp_port = 25

What do i do?

With respect

3条回答
Summer. ? 凉城
2楼-- · 2019-01-11 21:47

This is working for me on localhost:

<?php
     class Email extends CI_controller
     {
        function index()
        {

         $this->load->library('email');
         $this->load->helper('url');
         $this->load->helper('form');

        $config= Array(
            'protocol'  =>'localhost',
            'smtp_host' => 'localhost',
            'smtp_port' => 'localhost',
            'smtp_user'=>  'root',
            'smtp_pass' =>''
            );

        $this->load->library('email','$config');
        $this->email->set_newline("\r\n");
        $this->email->from('nisha@gmail.com','nisha');
        $this->email->to('cse1473@gmail.com');
        $this->email->subject('this is email with subject');
        $this->email->message('it s working properly');

        if($this->email->send())
        {
            echo "your email send";
        }
        else
        {
            show_error($this->email->print_debugger());
        }
    }
}

?>
查看更多
手持菜刀,她持情操
3楼-- · 2019-01-11 21:49

Have you turned on php_openssl?

Try to uncomment extension=php_openssl.dll in your php.ini file.

查看更多
姐就是有狂的资本
4楼-- · 2019-01-11 22:01

this is what worked for me

$email_config = Array(
            'protocol'  => 'smtp',
            'smtp_host' => 'ssl://smtp.googlemail.com',
            'smtp_port' => '465',
            'smtp_user' => 'someuser@gmail.com',
            'smtp_pass' => 'password',
            'mailtype'  => 'html',
            'starttls'  => true,
            'newline'   => "\r\n"
        );

        $this->load->library('email', $email_config);

        $this->email->from('someuser@gmail.com', 'invoice');
        $this->email->to('test@test.com');
        $this->email->subject('Invoice');
        $this->email->message('Test');

        $this->email->send();
查看更多
登录 后发表回答