简体   繁体   中英

Codeigniter Email PHP SMPT Error, cannot send mail through Gmail

I am currently trying to send an email or verification email after I've done signing up. The problem is I cannot send a verification through gmail because of this error:

The following SMTP error was encountered: Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.

在此处输入图片说明

I've already tried searching for solution here in stackoverflow, but I haven't found any solution that works for my problem.i have also done changing the smtp_port to 465 but still, it didn't solve my problem.

I am using Localhost and I configured the php.ini and sendmail.ini on xampp folder.

php.ini

[mail function]
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = qwerty@gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

Send mail

[sendmail]

smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=qwerty@gmail.com
auth_password=qwertyqwerty
force_sender=qwerty@gmail.com

my Code

//get user inputs
$email = $this->input->post('email');
$password = $this->input->post('password');

//generate simple random code
$set = '123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$code = substr(str_shuffle($set), 0, 12);

//insert user to users table and get id
$user['email'] = $email;
$user['password'] = $password;
$user['code'] = $code;
$user['active'] = false;
$id = $this->users_model->insert($user);

//set up email
$config = array(
    'protocol' => 'smtp',
    'smtp_host' => 'smtp.gmail.com',
    'smtp_port' => 587,
    'smtp_user' => 'qwerty@gmail.com', // change it to yours
    'smtp_pass' => 'qwertyqwerty', // change it to yours
    'smtp_timeout' => "",
    'mailtype' => 'html',
    'charset' => 'iso-8859-1',
    'wordwrap' => TRUE,
    'validate' => FALSE
);

$message =  "
            <html>
            <head>
                <title>Verification Code</title>
            </head>
            <body>
                <h2>Thank you for Registering.</h2>
                <p>Your Account:</p>
                <p>Email: ".$email."</p>
                <p>Password: ".$password."</p>
                <p>Please click the link below to activate your account.</p>
                <h4><a href='".base_url()."user/activate/".$id."/".$code."'>Activate My Account</a></h4>
            </body>
            </html>
            ";
    
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from($config['smtp_user']);
$this->email->to($email);
$this->email->subject('Signup Verification Email');
$this->email->message($message);

//sending email
if($this->email->send()){
    $this->session->set_flashdata('message','Activation code sent to email');
}
else{
    $this->session->set_flashdata('message', $this->email->print_debugger());

}

You don't need to change php.ini or sendmail.ini to send SMTP. Please make it default.

After I look at your SMTP configuration, You missed a little thing but seems important: crypto protocol.

You can set crypto protocol inside $config['smtp_host'] like so:

$config['smtp_host'] = 'ssl://smtp.googlemail.com'

Or you can also try this config:

$config['protocol'] = "smtp";
$config['smtp_host'] = "smtp.googlemail.com";
$config['smtp_port'] = "465";   // if you decided to use 465, then set smtp crypto to ssl, else for example 587, you need to set smtp crypto to tls.
$config['smtp_crypto'] = 'ssl';  // this is based on your smtp port -> ssl/tls
$config['smtp_timeout'] = "400";
$config['smtp_user'] = "youruser@gmail.com";
$config['smtp_pass'] = "yourpassword%&^$&$";
$config['validate'] = true;
$config['charset'] = 'utf-8';
$config['mailtype'] = "html";
$config['crlf'] = "\r\n";
$config['newline'] = "\r\n";

Then assign $config to email library: $this->load->library('email', $config) ;

Please do check less security (in gmail setting) was: enabled.

Otherwise, you can create email.php as an email config library inside application/config/email.php

just put it there:

<?php
defined('BASEPATH') or exit('No direct script access allowed');

$config['protocol'] = "smtp";
$config['smtp_host'] = "smtp.googlemail.com";
$config['smtp_port'] = "465";   // if you decided to use 465, then set smtp crypto to ssl, else for example 587, you need to set smtp crypto to tls.
$config['smtp_crypto'] = 'ssl';  // this is based on your smtp port -> ssl/tls
$config['smtp_timeout'] = "400";
$config['smtp_user'] = "youruser@gmail.com";
$config['smtp_pass'] = "yourpassword%&^$&$";
$config['validate'] = true;
$config['charset'] = 'utf-8';
$config['mailtype'] = "html";
$config['crlf'] = "\r\n";
$config['newline'] = "\r\n";

?>

Then inside your controller you don't need to set up your $config anymore :D, but all you have to do just call $this->load->library('email'); in controller.

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