How to set the session time out for otp using codeigniter - php

I want to set the session time out for OTP,after set the time OTP will expire
Controlller code as shown in below
$six_digit_random_number = mt_rand(100000, 999999);
$this->session->set_userdata('otp', $six_digit_random_number);
$this->load->model('Login_model');
$pass['pass'] = $this->Login_model->forgot_pass_retrive($emailTo);
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_port'] = '465';
$config['smtp_timeout'] = '7';
$config['smtp_user'] = 'tworkreport#gmail.com';
$config['smtp_pass'] = '87xxxxxxxx';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";
$config['mailtype'] = 'text';
$config['validation'] = TRUE;
$this->email->initialize($config);
$this->email->from('tworkreport#gmail.com', 'Terasukhintrade');
$this->email->to($emailTo);
$this->email->subject('Reset your password');
$this->email->message('Click on link to reset your password '.$this->session->userdata['otp'] );
$this->email->send();
echo $this->email->print_debugger();
$this->load->view('login',$data);
} else {
$this->session->set_flashdata('message', ' Email address not found!');
$this->load->view('login',$data);
}
}
}

you can use this to expire your OTP:
$this->session->set_tempdata('otp', $six_digit_random_number, 300);
Here 300 is seconds, which means your 'otp' session will expire after 5 minute.
For more information please refer https://www.codeigniter.com/user_guide/libraries/sessions.html .

Related

Error Sending Email using smtp.office365.com and PHP Codeigniter

Please help, send email error using SMTP Outlook 365.
previously it was still able to run, but suddenly an error.
these my code
$this->load->library('email');
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'smtp.office365.com';
$config['smtp_user'] = 'XXX#XXXX.com';
$config['smtp_pass'] = '#########';
$config['smtp_port'] = '587';
$config['smtp_crypto'] = 'tls';
$config['starttls'] = FALSE;
$config['smtp_timeout'] = '5';
$config['wordwrap'] = TRUE;
$config['charset']='iso-8859-1';
$config['mailtype'] = 'html';
$config['newline']="\r\n";
$config['crlf']= "\r\n";
$this->email->initialize($config);
//Email content
$htmlContent = 'XXX';
$htmlContent .= 'XXX';
$this->email->to('XXX#XXXX.com');
$this->email->from('XXXXX#XXXXX.com','MyWebsite');
$this->email->subject('How to send email via SMTP server in CodeIgniter');
$this->email->message($htmlContent);
//Send email
$this->email->send();
if($this->email->send())
{
echo "Email sent!!";
} else {
echo "Email is not sent!!";
echo $this->email->print_debugger();
}
}
and these an error result

Can't send email from codeigniter. Just reloading page

I can't send email from my codeigniter. it always reload when i press send button. I don't know what wrong with this code. please help me, so i can send the attached email to destination How can i solve this. The code is below :
function send_quotation($no, $email_to, $id_ticket, $problem, $action, $nama)
{
$this->load->model('service_tracking/trans_service_model');
$this->load->library('email');
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'smtp.gmail.com';
$config['smtp_port'] = 465;
$config['smtp_user'] = 'examplefrom#gmail.com';
$config['smtp_pass'] = 'password123';
$config['priority'] = 1;
$config['mailtype'] = 'html';
$config['charset'] = 'iso-8859-1';
$config['starttls'] = TRUE;
$data = $this->trans_service_model->get_emaildata($id_ticket);
$this->email->initialize($config);
$this->load->library('email', $config);
$this->email->set_newline('\r\n');
$this->email->from('examplefrom#gmail.com', 'Example corporation');
$this->email->to('exampleto#gmail.com');
$data = $this->trans_service_model->get_emaildata($id_ticket);
$this->email->initialize($config);
$this->load->library('email', $config);
$this->email->set_newline('\r\n');
$this->email->from('examplefrom#gmail.com', 'Example Corporation');
$this->email->to('exampleto#gmail.com');
$this->email->subject('['.$no.'] - '.$nama.' - Quotation Repair');
$message = "
This is the email contain quotation";
$this->email->message($message);
$this->email->attach('./quotation/'.$no.'.pdf');
$this->email->send();
echo $this->email->print_debugger();
}
do like this
if (!$this->email->send()) {
echo $this->email->print_debugger();
} else {
echo "Success";
//redirect();
}

Mail not sending/ delever 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'] = "\r\n",
$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'] = "\r\n";
$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);

Codeigniter emailer not working

$this->load->library('email');
// set email data
$this->email->from($this->input->post('sender_email'), $this->input->post('sender_name'));
$this -> email -> set_mailtype("html");
$this->email->to('');
$this->email->reply_to($this->input->post('sender_email'), $this->input->post('sender_name'));
$this->email->subject('From: '.$this->input->post('sender_name'));
$this->email->message("<table><tr><td> </td><td>".$this->input->post('sender_name')."</td></tr><tr><td> </td><td>".$this->input->post('sender_email')."</td></tr><tr><td> </td><td>".$this->input->post('phone')."</td></tr><tr><td>Message: </td><td>".$this->input->post('message')."</td></tr></table>");
$this->email->send();
// create a view named "succes_view" and load it at the end
redirect('contact?email_succ=1');
exit();
This is the code I'm using to send email from a form in CI and it works only when I use SMTP login along with this.
$this->load->library('email');
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_port'] = '465';
$config['smtp_timeout'] = '7';
$config['smtp_user'] = 'myemail#example.com';
$config['smtp_pass'] = 'emailpassword';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";
$config['mailtype'] = 'html'; // or html
$config['validation'] = TRUE; // bool whether to validate email or not
$this->email->initialize($config);
Having this code right after loading the library solves the issue. But what's wrong with the default code that has been working all this time but suddenly stopped?
UPDATE: adding just this line $this->email->initialize($config); solves the issue, But why?.
Try the following code.
$config = array();
$config['useragent'] = "CodeIgniter";
$config['mailpath'] = "/usr/bin/sendmail"; // or "/usr/sbin/sendmail"
$config['protocol'] = "smtp";
$config['smtp_host'] = "localhost";
$config['smtp_port'] = "25";
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";
$config['wordwrap'] = TRUE;
$this->load->library('email');
$this->email->initialize($config);
$this->email->message = "Your Message";
$this->email->subject("Message Subject");
$this->email->from("user#gmail.com");
$this->email->to("admin#gmail.com");
$this->email->send();

codeigniter mail - i can send email but i can't received it

I am using this code to send email via localhost.
function send_email(){
date_default_timezone_set('Asia/Manila');
$ci =& get_instance();
$ci->load->library('email');
$config['protocol'] = "mail";
$config['smtp_host'] = "ssl://mail.smsgt.com";
$config['smtp_port'] = "25";
$config['smtp_user'] = "myemail#smsgt.com";
$config['smtp_pass'] = "";
$config['charset'] = "utf-8";
$config['mailtype'] = "text";
$config['newline'] = "\r\n";
$config['crlf'] = "\r\n";
$ci->email->initialize($config);
$ci->email->from('sender#email.com', 'company email');
$ci->email->to('receiver#gmail.com');
$ci->email->subject('test subject');
$ci->email->message("testing email");
$check = $ci->email->send();
//echo $ci->email->print_debugger();
if ($check){
echo "true";
}else{
echo "false";
}
}
It always shows true but still I cant received it. What is problem with my codes? or is there something wrong?
**Your smtp and port is not working**
$config['smtp_host'] = "ssl://mail.smsgt.com";
$config['smtp_port'] = "25";
**Change into this**
$config['smtp_host'] = "ssl://smtp.mail.yahoo.com;ssl://smtp.gmail.com";
$config['smtp_port'] = 465;

Categories