邮件不发送/删除php codeigniter

时间:2017-03-16 12:34:31

标签: php codeigniter email

我正在尝试向Gmail发送忘记密码链接。但是不能取得成功。

这是我的小代码。我这里有配置文件。如果需要任何更改,请建议。我使用两个Gmail帐户将邮件从一个Gmail帐户发送到其他Gmail帐户。

这是我的配置文件email.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config = Array(
         $config['protocol']    = 'smtp',
        $config['smtp_host']    = 'ssl://smtp.gmail.com',
        $config['smtp_port']    = '465',
        $config['smtp_timeout'] = '7',
        $config['smtp_user']    = 'xyz@gmail.com',
        $config['smtp_pass']    = '123',
        $config['charset']    = 'utf-8',
        $config['newline']    = "\r\n",
        $config['mailtype'] = 'text', // or html
        $config['validation'] = TRUE // bool whether to validate email or not 
    );

这是我发送邮件的代码。

if($this->form_validation->run())
            {
                //echo 1;
                //echo validation_errors();
                $this->load->library('email');
                $reset_key = md5(uniqid());             
                $this->load->model('User_Model');
                if($this->User_Model->update_reset_key($reset_key))
                {                   

                    $this->email->from('xyz@gmail.com', 'data-+-');
                    $this->email->to($this->input->post('email'));
                    $this->email->subject('Reset you account password at Mahesh Makwana');
                    $message = "<p><h4>You or someone request to reset your password.</h4></p>";
                    $message.="<a href='".base_url(). "reset_password/".$reset_key."'>Click here to reset your password</a>";
                    $this->email->message($message);
                        if($this->email->send())
                        {
                            echo 'Kindly check your email '.$this->input->post('email').' to reset your password';
                        }
                        else
                        {
                            echo 'Cannot send email! Kindly contact to our customer service to help you.!';
                        }
                    }
                    else{
                        echo 1;
                    }
                }
                else
                {
                    //echo 0;
                    //echo validation_errors();
                    $this->load->view('include/forgetpassword');
                }

2 个答案:

答案 0 :(得分:0)

您的代码没有错。它可能是因为谷歌安全。登录帐户后,尝试允许访问Google帐户设置中的“安全性较低的应用”

https://www.google.com/settings/security/lesssecureapps

答案 1 :(得分:0)

管理您的配置设置,如下所示:

        $config['protocol']    = 'smtp';
        $config['smtp_host']    = 'ssl://smtp.gmail.com';
        $config['smtp_port']    = '465';
        $config['smtp_timeout'] = '7';
        $config['smtp_user']    = 'xyz@gmail.com';
        $config['smtp_pass']    = 'xxxxx';
        $config['charset']    = 'utf-8';
        $config['newline']    = "\r\n";
        $config['mailtype'] = 'text' // or html
        $config['validation'] = TRUE; // bool whether to validate email or not 

然后使用email

加载$this->email->initialize($config);库集配置后
$this->load->library('email');
$this->email->initialize($config);