Codeigniter:无法发送电子邮件Gmail SMTP

时间:2016-01-17 16:51:46

标签: php codeigniter email

使用来自Codeigniter的电子邮件库,通过Google邮件SMTP发送电子邮件时遇到问题。

这是我的Controller的代码:

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

这是我的模型代码:

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

使用这些功能时出现这种错误:

遇到PHP错误

严重性:警告

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

任何人都可以给我一个解决方案吗?我感谢任何帮助:)

3 个答案:

答案 0 :(得分:0)

下载https://github.com/PHPMailer/PHPMailer。 将PHPMailer保存在application \ libraries中。

添加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");

在您的控制器中:

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();

答案 1 :(得分:0)

嘿试试这个顺利为我工作

$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...";
}

答案 2 :(得分:0)

试试这段代码,它对我来说很好。我希望它也适合你。

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