简体   繁体   中英

Codeigniter: Cannot send email Gmail SMTP

I have a problem sending email through google mail SMTP, using email library from Codeigniter.

Here is my Controller's code:

<?php
    class User extends CI_Controller{

        function __construct(){
            parent::__construct();
            $this->load->model('user_model');
        }

function forgot_password($user_id){
            $data = $this->user_model->get_user_detail($user_id);

            if(count($data) == 0){
                $response["error"] = TRUE;
                $response["error_message"] = "No user data was found";
            }else{
                $response["error"] = FALSE;
                $pass = $data->user_password;               

                $ci = get_instance();
                $ci->load->library('email');
                $config['protocol'] = "smtp";
                $config['smtp_host'] = "ssl://smtp.gmail.com";
                $config['smtp_port'] = "465";
                $config['smtp_timeout']="30"; 
                $config['smtp_user'] = "username@gmail.com";
                $config['smtp_pass'] = "*****";
                $config['charset'] = "utf-8";
                $config['mailtype'] = "html";
                $config['newline'] = "\r\n";

                $ci->email->initialize($config);

                $ci->email->from('username@gmail.com', 'Name');
                $list = array($user_id);
                $ci->email->to($list);
                $ci->email->subject('Test: Forgot Password');
                $ci->email->message($pass);
                if ($this->email->send()) {
                    echo 'Email sent!';
                } else {
                    show_error($this->email->print_debugger());
                }
            }
        }   
    } 
?>

And this is my Model code:

<?php
    class User_model extends CI_Model{

function get_user_detail($user_id){
                $this->db->where("user_id", $user_id);
                $query = $this->db->get('user');
                return $query->row();
        }
}
?>

I get this kind of error when I use these functions:

A PHP Error was encountered

Severity: Warning

Message: fsockopen(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

Filename: libraries/Email.php

Line Number: 1959

Backtrace:

File: C:\xampp\htdocs\rest_api\application\controllers\owner.php
Line: 71
Function: send

File: C:\xampp\htdocs\rest_api\index.php
Line: 292
Function: require_once
A PHP Error was encountered

Severity: Warning

Message: fsockopen(): Failed to enable crypto

Filename: libraries/Email.php

Line Number: 1959

Backtrace:

File: C:\xampp\htdocs\rest_api\application\controllers\owner.php
Line: 71
Function: send

File: C:\xampp\htdocs\rest_api\index.php
Line: 292
Function: require_once
A PHP Error was encountered

Severity: Warning

Message: fsockopen(): unable to connect to ssl://smtp.gmail.com:465 (Unknown error)

Filename: libraries/Email.php

Line Number: 1959

Backtrace:

File: C:\xampp\htdocs\rest_api\application\controllers\owner.php
Line: 71
Function: send

File: C:\xampp\htdocs\rest_api\index.php
Line: 292
Function: require_once

Can anyone give me a solution? I appreciate any help :)

download https://github.com/PHPMailer/PHPMailer . Save PHPMailer in application\\libraries.

Add in application\\config\\config.php:

//administrator
$config['admin_email'] = "email@gmail.com";
$config['admin_nome'] = "admin name";

//PHP Mailer | gmail send account
$config['host'] = "smtp.gmail.com";
$config['user'] = "account@gmail.com";
$config['pass'] = "passwordaccount";
$config['port'] = 587;    
$config['to'][0] = array("email"=>"email1@gmail.com","nome"=>"Email 1");
$config['to'][1] = array("email"=>"email2@gmail.com","nome"=>"Email 2");

In your controller:

require_once("application/libraries/PHPMailer/class.phpmailer.php");

$admin_email = $this->config->item('admin_email');
$admin_nome  = $this->config->item('admin_nome');

$mail = new phpmailer();

$mail->IsSMTP(); 
$mail->Host = $this->config->item('host');
$mail->SMTPAuth = true; 
$mail->Username = $this->config->item('user');
$mail->Password = $this->config->item('pass');
$mail->Port= $this->config->item('port');
$mail->SMTPSecure = "tls";//or ssl

$mail->From = $admin_email;
$mail->FromName = $admin_nome;

$arr_address = $this->config->item('to');

for($i=0;$i<sizeof($arr_address);$i++){

    $mail->AddAddress($arr_address[$i]["email"],$arr_address[$i]["nome"]);    
}

$mail->IsHTML(true);

$mail->Subject = utf8_decode("Notification");

$mail->Body = "<p>Test message!</p>";
$mail->Body = utf8_decode($mail->Body);

$mail->Send();

Hey try this it's Working For me smoothly

$config = Array(
    'protocol' => 'smtp',
    'smtp_host' => 'ssl://smtp.googlemail.com',
    'smtp_port' => 465,
    'smtp_user' => 'xxx',
    'smtp_pass' => 'xxx',
    'mailtype'  => 'html', 
    'charset'   => 'iso-8859-1'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('abc@xyz.com'); // change it to yours
$this->email->to(yours@yours.com);// change it to yours
$this->email->subject('A test email from CodeIgniter using Gmail');
$this->email->message('A test email from CodeIgniter using Gmail'); 
$result = $this->email->send();
if($result){
  echo "Mail sent successfully...";
}
else{
 echo "error in mail sending...";
}

Try this code, it is working fine for me. I hope it will also work for you.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Email extends CI_Controller
{
    public function index()
    {
        $config = Array(
            'protocol' => 'smtp',
            'smtp_host' => 'ssl://smtp.gmail.com',
            'smtp_port' => 465,
            'smtp_user' => 'yourEmailAddress@gmail.com',
            'smtp_pass' => 'yourPassword',
            'mailtype' => 'html',
            'charset' => 'iso-8859-1',
            'wordwrap' => TRUE
        );
        $this->load->library('email', $config);
        $this->email->set_newline("\r\n");
        $this->email->from('sendersEmail', 'sendersName');
        $this->email->to('receiverEmail');
        $this->email->subject('emailSubject');
        $this->email->message('emailMessage');
        if ($this->email->send()) {
            echo 'Email sent.';
        } else {
            show_error($this->email->print_debugger());
        }
    }
}

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