邮件不发送/删除php codeigniter

问题描述:

I am trying to send forgot password link to gmail. but can't get success.

Here is my little code. I have my config file here. If any changes needed then please suggest. I am using two gmail account to send mail from one gmail account to other gmail account.

Here is my config file 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']    = "
",
        $config['mailtype'] = 'text', // or html
        $config['validation'] = TRUE // bool whether to validate email or not 
    );

Here is my code for deliver mail.

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');
                }

Nothing wrong in your code. Its might be because of google security. Try to allow access for 'Less secure apps' in google account settings after logging in to your account

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

Manage your configuration setting like below:

        $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']    = "
";
        $config['mailtype'] = 'text' // or html
        $config['validation'] = TRUE; // bool whether to validate email or not 

Then after loading email library set configuration using $this->email->initialize($config);

$this->load->library('email');
$this->email->initialize($config);