I want to send email using template. I have tried this code but it gives this error.
Severity: 4096
Message: Object of class CI_Loader could not be converted to string
My code is:
public function email_submit_change_password(){
$this->load->library('email');
$email = $this->input->post('email');
$check = $this->db->query("SELECT * FROM tbl_profile_main WHERE connect_to_email='".$email."';");
if($check->result()!=NULL){
$test = $this->load->view('includes/test');
$this->email->from('itsthunder07#gmail.com', 'Your Name');
$this->email->to($email);
$this->email->subject('Password Reset');
$this->email->message($test);
if($this->email->send()){
$this->db->query("UPDATE tbl_profile_main SET token_='".$token."' WHERE connect_to_email='".$email."';");
$this->session->set_userdata('email_send','success');
return 1;
// return 5;
}
}else{
$this->session->set_userdata('email_incorrect','error');
return 0;
}
}
If you want to return a view as a string, you have to load it as follows:
$test = $this->load->view('includes/test', '', true);
there should be check like this:
<?php
$check = $this->db->query("SELECT * FROM tbl_profile_main WHERE connect_to_email='".$email."';");
if($check->num_rows() > 0) { // check data exists or not
$r = $check->result_array();
if(!empty($r)) {
// send email process
} else {
$this->session->set_userdata('email_incorrect','error');
return 0;
}
}
?>
Related
I am getting following error from my code written in Codeigniter when i enter a wrong E-mail.
A PHP Error was encountered Severity: Notice
Message: Trying to get property 'email' of non-object
Filename: controllers/forms.php
Line Number: 26
Backtrace:
File:
E:\Software\XAMPP\xampp\htdocs\ciauth\application\controllers\forms.php
Line: 26 Function: _error_handler
File: E:\Software\XAMPP\xampp\htdocs\ciauth\index.php Line: 315
Function: require_once
Below is the controller
<?php
class Forms extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->library('session');
$this->load->helper('url');
$this->load->model('user_model', 'auth');
}
public function forgot_pass()
{
if($this->input->post('forgot_pass'))
{
$email=$this->input->post('email');
$this->load->model('user_model', 'auth');
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'E-mail', 'required');
if ($this->form_validation->run() == TRUE) {
$que=$this->db->query("select password,email from users where email='$email'");
$row=$que->row();
$user_email=$row->email;
if((!strcmp($email, $user_email)))
{
$pass=$row->password;
$to = $user_email;
$subject = "Password";
$txt = "Your password is $pass .";
$headers = "From: user#testdomain.com" . "\r\n" . "CC: hamza_zon#outlook.com ";
mail($to,$subject,$txt,$headers);
$this->load->view('user/header');
$this->load->view('user/confirm');
$this->load->view('user/footer');
}
else{
$data['error']="Invalid Email ID !";
}
}
else{
$data['error']="Email ID is required !";
}
}
$this->load->view('user/header');
$this->load->view('user/forgot_pass',#$data);
$this->load->view('user/footer');
}
}
?>
you should check before $user_email=$row->email; that the as per your criteria record exist or not, if row is not exist then you will get that error
so you should check as below
$row=$que->row();
if($row) {
$user_email=$row->email;
}
Try like this
$que = $this->db->query("select password,email from users where email=".$email);
if(isset($row) && $row != ''){
$txt = "Your password is" . $pass;
//Coding...
}
You need to handle it like this,
$query = $this->db->query("select password,email from users where email=".$email);
if ($query->num_rows() > 0) {
$row = $query->row();
$user_email = $row->email;
} else {
$user_email = '';
}
//then your next if condition in which you are comparing two strings
$row=$que->row();
$row is probably being returned as NULL or a falsy value.
var_dump($row) to check.
if(!$row)
{
die("user not found");
}
or
if(empty($row))
{
die("user not found");
}
Apply condition as, because in your case it may not getting the result from table for that email,
if (isset($row))
{
Note: row method returns a single result row. If your query has more than one row, it returns only the first row. The result is
returned as an object. Here’s a usage example:
$que=$this->db->query("select password,email from users where email like '$email'");
$row = $que->row();
if (isset($row))
{
$user_email = $row->email;
$user_password = $row->password;
}
Here is concise doc for the same.
this is the image of database table.
I'm new at codeigniter PHP
public function ForgotPassword($email)
{
$this->db->select('email');
$this->db->from('member');
$this->db->where('email', $email);
$query=$this->db->get();
$out = $query->result_array();
if (count($out) > 0) {
$email = $out[0]['email'];
return array('status'=>1, 'email'=>$email);
} else {
return array('status'=>0, 'msg'=>'email not found');
}
}
This is my model function., this line giving me blank result $out = $query->result_array(); I don't know why, it should give me email address so that i can proceed and send email. I want to send password to user using forgot password function.
public function ForgotPassword()
{
$email = $this->input->post('email');
$findemail = $this->MY_model->ForgotPassword($email);
if ($findemail['status'] == 1) {
$this->MY_model->sendpassword($findemail);
} else {
echo " $email not found, enter correct email id";
}
}
And this is my controller function. Here I'm getting blank value of $findemail. I don't know why.
public function sendpassword($data)
{
$email = $data['email'];
$query1=$this->db->query("SELECT * from member where email = '".$email."' ");
$row=$query1->result_array();
if ($query1->num_rows() > 0) {
$passwordplain = "";
$passwordplain = rand(10000000,99999999);
$newpass['password'] = md5($passwordplain);
$this->db->where('email', $email);
$this->db->update('member', $newpass);
$mail_message='Dear '.$row[0]['username'].','. "\r\n";
$mail_message.=' Your <b>Password</b> is
<b>'.$passwordplain.'</b>'."\r\n";
require 'C:\xampp\htdocs\phpmailer\class.phpmailer.php';
require 'class.phpmailer.php';
$mail = new PHPMailer;
$mail->IsSendmail();
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com";
$subject = 'Testing Email';
$mail->AddAddress($email);
$mail->IsMail();
$mail->From = 'vishal#gmail.com';
$mail->FromName = 'admin';
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $mail_message;
$mail->Send();
if (!$mail->send()) {
echo "Failed to send password";
} else {
echo "Password sent ! check your mail";
}
} else {
echo "Email not found!!!!";
}
}
And also upper model function , it's not sending email. I'm using phpmailer to sending email. Please help.
If your model name is 'MY_model' then write construct function in your controller file and load the model into it as shown below.
function __construct() {
parent::__construct();
$this->load->model('my_model');
}
And modify your ForgotPassword function of controller as below.
public function ForgotPassword()
{
$email = $this->input->post('email');
$findemail = $this->my_model->ForgotPassword($email);
if ($findemail['status'] == 1) {
$this->my_model->sendpassword($findemail);
} else {
echo " $email not found, enter correct email id";
}
}
I hope this helps.
I have made a function that checks if email exists in db. It is named CheckUser().
Then in ForgotPassword() you call CheckUser() ($email must be given as argument).
<?php
public function CheckUser($email)
{
$response = $bdd->prepare('SELECT email FROM member WHERE email = :email');
$response->execute([
"email" => $$email
]);
$data = $response->fetchAll();
return $data[0];
}
public function ForgotPassword($email)
{
// here you call our new function
** UDPATE BELOW CALLING OF CHECKUSER FUNCTION **
$out = $this->CheckUser($email);
// sizeof will return the lenght of the returned data from our CheckUser() function
if (sizeof($out) > 0){
$email = $out[0]['email'];
return array('status'=>1, 'email'=>$email);
}
else {
return array('status'=>0, 'msg'=>'email not found');
}
}
Let me know if it works.
In my Authentication.php class I hava a code like below;
public function prepareTicket($data){
if(array_key_exists('tickettype', $data))
$this->tickettype = $data['tickettype'];
if(array_key_exists('ticketinfo', $data))
$this->ticketinfo = $data['ticketinfo'];
}
function createTicket(){
$sql = "INSERT INTO ticket_test (tickettype, ticketinfo) VALUES ('$this->tickettype','$this->ticketinfo')";
$result = mysqli_query($this->DB_CONNECTION,$sql);
}
function createTicketControl(){
$sql = "SELECT * FROM `ticket_test` WHERE tickettype = '".$this->tickettype."'AND ticketinfo ='".$this->ticketinfo."'";
$result = mysqli_query($this->DB_CONNECTION,$sql);
if(mysqli_num_rows($result) >0){
return true;
}else{
return false;
}
}
and in the other php file I have a code like this;
<?php
include_once 'Authentication.php';
use user\Authentication;
$auth = new Authentication();
$auth->prepareTicket($_POST);
$ticketStatus = $auth->createTicketControl();
if($ticketStatus){
$json['success'] = 1;
$json['message'] = 'Destek kaydı oluşturuldu';
}else{
$json['success'] = 0;
$json['message'] = 'Error!';
}
echo json_encode($json);
Now my problem is that whenever I try to insert data to my database it returns 'success' = '0' , 'message' = 'Error' and the data couldnt be inserted on the database.I mean the service is not working properly.Any help is appreciated...
P.S.= I am aware of sql injection threat on this code bu it is for android app so no need to worry about it.
Found the solution.I realised that in CreateTicket.php I didint call createTicket function from Authentication.php :/
Good Evening,
I'm using CodeIgniter 2 on IIS 8.5 and MSSQL Server,
When i tried to send an email in the process, it shows the error:
Severity: Notice
Message: A non well formed numeric value encountered
Filename: libraries/Email.php
Line Number: 1689
I've been using this code for some projects in the same server, and it's working properly. But when i tried to implement on this one it always shows this error everytime there's sending email process.
And here's my controller:
function reset_pass() {
if (!IS_AJAX) {
display_nonajax_stop();
exit;
} else {
$settings = $this->m_setting->get_setting_array();
$min_pass_length = (int) $settings["acc_pass_min_length"];
$data = $this->input->post();
$id = $data['id'];
$hasil = array();
$kondisi = array(
"usr_id" => $id
);
$current_time = waktu_sekarang();
$baris = $this->m_ess->get_user_employee_row($kondisi);
$user = $baris;
if ($user->usr_email === NULL) {
$hasil['pesan'] = "User does not have valid email associated . Please contact Administrator.";
$hasil['status'] = 0;
} else {
$this->load->helper('email');
if (!valid_email($user->usr_email)) {
$hasil['pesan'] = "User does not have valid email associated . Please contact Administrator.";
$hasil['status'] = 0;
} else {
$hasil['pesan'] = "Username and email is ok";
$hasil['status'] = 0;
$this->load->library('rndconditionpass');
$tmppass = $this->rndconditionpass->get_random_string($min_pass_length);
$new_password = $this->bcrypt->do_hash($tmppass);
$mail_data['random'] = $tmppass;
$mail_data['username'] = $user->usr_name;
$mail_data['full_name'] = $user->emp_name;
$mail_data['password'] = $tmppass;
//prepare data to be used in leo_mail library
$param = array();
$param['recipient'] = $user->usr_email;
$param['content'] = $this->load->view("template/mail/mail_reset_pass", $mail_data, TRUE);
$param['subject'] = "Reset Password";
$this->load->library('leo_mail');
$kirim = $this->leo_mail->kirim($param);
if ($kirim['status'] == 1) {
#password history
$insert_pass = array(
"pas_usr_name" => $user->usr_name,
"pas_password" => $new_password,
"pas_date" => $current_time,
"pas_opid" => $this->session->userdata("usr_name"),
"pas_update" => $current_time
);
$this->m_pass->insert_pass($insert_pass);
$hasil['pesan'] = "Password has been reset and user has been notified via email. ";
$hasil['status'] = 1;
//update the database, store the reset code
$update = array();
$update['usr_last_cpass'] = date("Y-m-d H:i:s");
$update['usr_needchgpwd'] = "Y";
$update["usr_password"] = $new_password;
$kondisi = array("usr_name" => $user->usr_name);
$q = $this->m_ess->update_user($update, $kondisi);
} else {
$hasil['pesan'] = "Fail to send email your password reset code.";
$hasil['status'] = 0;
}
}
}
echo json_encode($hasil);
}
}
and here's the '$this->leo_mail->kirim function
function kirim($param) {
$q=$this->ci->m_smtp->get_mail_konfig();
$hasil=array();
if($q->num_rows()<1) {
$hasil['pesan']="Your SMTP configuration has not been set up properly";
$hasil['status']=0;
} else {
if(valid_email($param['recipient'])) {
$config=array();
$config_data= $q->row();
$config['protocol'] = "smtp";
$config['useragent'] = "HRISSMTPLIB";
$config['smtp_port'] = $config_data->smt_smtp_port;
$config['smtp_host'] = $config_data->smt_smtp_host;
$config['smtp_user']= $config_data->smt_smtp_user;
$config['smtp_pass']= $this->ci->leo_enkripsi->decrypt($config_data->smt_smtp_pass,$this->key);
$config['newline']="\r\n";
$config['wordwrap'] = FALSE;
$config['starttls']=($config_data->smt_encrypt_type==3)?TRUE:FALSE;
$config['mailtype'] = "html";
$this->ci->email->initialize($config);
$this->ci->email->from($config_data->smt_email,$config_data->smt_name);
$this->ci->email->to($param['recipient']);
$this->ci->email->subject($param['subject']);
$this->ci->email->message($param['content']);
if($this->ci->email->send()) {
$hasil['pesan']="Email sent";
$hasil['status']=1;
} else {
$hasil['pesan']= $this->ci->email->print_debugger();
$hasil['status']=0;
}
} else {
$hasil['pesan']="Recipient email is not valid";
$hasil['status']=0;
}
}
return $hasil;
}
Sorry for my bad english.
Thank you in advance.
I have a form field in my site that I need to know if the email that the user want to use isn't already used by other user.
I am using JavaScript and PHP for that but can make it work.
function validateEmail(){
//testing regular expression
var a = $("#email").val();
var filter = /^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,6}$/;
//if it's valid email
if(filter.test(a)){
if($("#email").val() != ""){
$.getScript("http://localhost/sisdelivery/validaEmail.php?&email="+$("#email").val(), function(){
if(resultadoEmail["email"] != ""){
email.addClass("error");
emailInfo.text("Email ja cadastrado!");
emailInfo.addClass("error");
return false;
}
else{
email.removeClass("error");
emailInfo.text("Email OK");
emailInfo.removeClass("error");
return true;
}
}
}
}
//if it's NOT valid
else{
var valEmail = $("#email").val();
if(valEmail == ""){
email.addClass("error");
emailInfo.text("Favor digitar um email!");
emailInfo.addClass("error");
return false;
}
else{
email.addClass("error");
emailInfo.text("O email digitado e invalido");
emailInfo.addClass("error");
return false;
}
}
}
The PHP code that looks on the database for the data:
<?php
include "conecta.php";
$email = $_GET['email'];
$consulta = "SELECT * FROM usuarios WHERE email = '$email';";
$result = mysql_query($consulta);
$num = mysql_num_rows($result);
if($num == 0)
echo "var resultadoEmail = { 'email' : '' }";
else{
while($row = mysql_fetch_object($result)){
$email = $row -> email;
}
echo "var resultadoEmail = { 'email' : '$email' }";
}
?>
I'm getting the error Uncaught SyntaxError: Unexpected token } but can't find where is the problem.
First of all, I just want to tell you that you have a security risk in your code. This line :
$email = $_GET['email'];
$consulta = "SELECT * FROM usuarios WHERE email = '$email';";
allow a person to run sql against your database and retrieve data. This is know as sql injection. You should validate your email before doing your query at minimum.
use this
return true;
}
});
^^^ // you forgot this