i have one selection form in my application..in that i get users list from the users table in the selection box drop down..i am trying to send mail depending on the selected user from drop down..
i got user_id from the drop down..but i didn't get email..i tried a lot but i didn't found where i got error..
Here is my controller:
public function add_selection()
{
$data["msg"]="";
$this->load->model('SelectionModel');
$data['rolename']=$this->SelectionModel->getrolename();
$data['candidate']=$this->SelectionModel->getcandidates();
$data['usertype']=$this->SelectionModel->getusers();
if($this->input->post())
{
$this->SelectionModel->add_selection_details($this->input->post());
$all_users = $this->input->post('user_id');
print_r($all_users);
foreach($all_users as $key)
{
$get_email = $this->SelectionModel->get_user_email_by_id($key);
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://md-in-42.webhostbox.net',
'smtp_port' => 465,
'smtp_user' => 'test3#clozloop.com',
'smtp_pass' => 'test3'
);
$this->load->library('email',$config);
$this->email->set_mailtype("html");
$this->email->from('test3#clozloop.com', 'bharathi');
$this->email->to($get_email);
$this->email->subject('this is our candidate details pls go through it');
$link = 'Click on this link - Click Here';
$this->email->message($link);
print_r($get_email);
if($this->email->send())
{
echo "email sent";
}
else
{
echo "email failed";
}
}
}
$this->load->view('selection/selection_details',$data);
}
This is my model:
function get_user_email_by_id($user_id)
{
$this->db->select('*');
$this->db->from('users');
$this->db->where('user_id',$user_id);
$query = $this->db->get();
$result = $query->row_array();
return $result['email'];
}
please help me how to do this..
Thank you..
In your model. Try once like this..
function get_user_email_by_id($user_id)
{
$this->db->select('email');
$this->db->from('users');
$this->db->where('user_id',$user_id);
$query = $this->db->get();
return $query->row()->email;
}
In view:
<select class="form-control" multiple class="form-control" data-placeholder="user name" name="user_id[]" >
<?php foreach($usertype as $rows) { ?>
<option value="<?php echo $rows->user_id?>"><?php echo ucfirst($rows->first_name)?></option>
<?php } ?>
try this get the values in an array through select while you're multiple selecting the values! name="select[]"
if(count($this->input->post()) > 0) {
$this->SelectionModel->add_selection_details($this->input->post());
$all_users = $this->input->post('user_id');
print_r($all_users);
foreach($all_users as $key)
{
$get_email = $this->SelectionModel->get_user_email_by_id($key);
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://md-in-42.webhostbox.net',
'smtp_port' => 465,
'smtp_user' => 'test3#clozloop.com',
'smtp_pass' => 'test3',
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => 'TRUE',
'newline' => '\r\n'
);
$this->load->library('email',$config);
$this->email->set_mailtype("html");
$this->email->from('test3#clozloop.com', 'bharathi');
$this->email->to($get_email);
$this->email->subject('this is our candidate details pls go through it');
$link = 'Click on this link - Click Here';
$this->email->message($link);
print_r($get_email);
if($this->email->send())
{
echo "email sent";
}
else
{
echo "email failed";
}
}
}
Related
I'm trying to display the user's firstname after login but it doesn't appear on the page. I tried to display it like this
<h2> Welcome <?php echo $this->session->userdata('firstname'); ?> </h2>
but it didn't display the user's firstname, even though there are data in DB. But when I tried to change the 'firstname' into 'username', it displayed the email of the user(test#gmail.com). How can I display the user firstname? Here's my controller:
function login(){
$this->form_validation->set_rules('username', 'Username', 'required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run() == FALSE) {
redirect('/');
} else {
$user_login = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password'),
);
$login = new UI_model;
$result = $login->login_user($user_login);
if($result){
$auth_details = array(
'firstname' => $result->firstname,
'lastname' => $result->lastname,
'username' => $result->username,
);
$this->session->set_userdata($auth_details);
// $this->session->set_userdata('authenticated', '1');
$this->session->set_flashdata('status', 'User Logged in Successfully!');
redirect(base_url('pages/index'));
} else {
$this->session->set_flashdata('status', 'Invalid Credentials. Please try again');
redirect(base_url('pages/about'));
}
}
}
Here's my Model:
public function login_user($data){
$this->db->select('*');
$this->db->where('username', $data['username']);
$this->db->where('password', $data['password']);
$this->db->from('users');
$this->db->limit(1);
$query = $this->db->get();
if($query->num_rows() == 1){
return $query->row();
} else {
return false;
}
}
Can you attach an image of your database you are using for and echo all data session?
I am using Codeigniter email library to send emails. I have integrated email settings like this in my model
public function savedetails($data) {
$firstmail = $this->sendmail(1, $data);
$secondmail = $this->sendmail(2, $data);
}
public function sendmail($flag, $data) {
$this->load->library('email');
$config = array(
'protocol' => 'smtp',
'smtp_host' => $this->config->item('emailhost'),
'smtp_user' => $this->config->item('emailusername'),
'smtp_pass' => $this->config->item('emailpassword'),
'smtp_port' => 25,
'smtp_crypto' => 'tls',
'mailtype' => 'html'
);
$this->email->initialize($config);
$this->email->set_newline("\r\n");
if($flag == 1) {
$this->email->from('someone#example.com', 'Someone');
$this->email->to('someone#example.com, 'Someone');
$this->email->subject('This is a test message');
$message = $this->load->view('email/firstmail.php', $data, true);
} else {
$this->email->from('someone#example.com', 'someone');
$this->email->to('someone#example.com', 'someone');
$this->email->subject('This is a test message');
$message = $this->load->view('email/secondmail.php', $data, true);
}
$this->email->message($message);
return $this->email->send();
}
So first email goes successfully but the second email fails everytime. Second email is not going delivered and giving false each time but first email delivered successfully. What is the issue here ? How can i get second email delivered successfully too in Codeigniter ?
I am working on sending the email in loop in codeigniter.
What actually happening is I am receveing the email but it doest not shwing the message which I had included. Following is script:
<?php
Class Email extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'myemail',
'smtp_pass' => 'mypass',
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'send_multipart' => FALSE
);
$this->load->library('email', $config);
$this->load->database();
$query = $this->db->query('SELECT email FROM users');
// $this->email->initialize($config);
// $this->email->set_newline('\r\n');
foreach ($query->result() as $row)
{
$this->email->clear();
$this->email->set_newline("\r\n");
$this->load->library('email',$config);
$this->email->from("mymail","myname");
$this->email->to($row->email);
$this->email->subject("THIS IS AN EMAIL TEST");
$this->email->message('Hello, We are <strong>Example Inc.</strong>');
$this->email->set_mailtype("html");
if($this->email->send())
{
echo "Your Mail send";
}
else
{
show_error($this->email->print_debugger());
}
}
echo 'Total Results: ' . $query->num_rows();
}
}
?>
I am expecting Hello, We are <strong>Example Inc.</strong>' this message in email but what I actually getting is
No mesage in email. Where I am making mistake.
Replace your code with this
<?php
Class Email extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'myemail',
'smtp_pass' => 'mypass',
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'send_multipart' => FALSE
);
$this->load->library('email', $config);
$this->load->database();
$query = $this->db->query('SELECT email FROM users');
// $this->email->initialize($config);
// $this->email->set_newline('\r\n');
foreach ($query->result() as $row)
{
$this->email->clear();
$this->email->set_newline("\r\n");
$this->load->library('email',$config);
$message = '<html><body>';
$message .= 'Hello, We are <strong>Example Inc.</strong>';
$this->email->from("mymail","myname");
$this->email->to($row->email);
$this->email->subject("THIS IS AN EMAIL TEST");
$this->email->set_mailtype("html");
$this->email->message($message);
if($this->email->send())
{
echo "Your Mail send";
}
else
{
show_error($this->email->print_debugger());
}
}
echo 'Total Results: ' . $query->num_rows();
}
}
?>
I am using codeigniter to send email.
function forget_pw() {
$this->load->library('email');
$config['smtp_host'] = 'host address';
$config['smtp_user'] = 'username';
$config['smtp_pass'] = 'password';
$this->email->initialize($config);
$this->email->from('admin#email.com', 'Admin');
$this->email->to('test#yahoo.com.hk');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->send();
echo $this->email->print_debugger();
}
No any error show in console. But the email didn't send out.
You can create a function like this:
public function sendEmail($subject,$message)
{
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'xxxxx.xxxxxx.xxxxx',
'smtp_port' => 465,
'smtp_user' => 'xxxxx.xxxxxx.xxxxx',
'smtp_pass' => 'xxxxx.xxxxxx.xxxxx',
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('xxxxx.xxxxxx.xxxxx');
$this->email->to('xxxxx.xxxxxx.xxxxx');
$this->email->subject($subject);
$this->email->message($message);
if($this->email->send())
{
echo 'Email send.';
}
else
{
show_error($this->email->print_debugger());
}
}
And use it like this $this->sendEmail('Title', 'Message'); Good luck
my controller
if (isset($_POST['send']))
{
$email = $this->input->post("email");
$reco=$this->ui_model->check($email);
if($reco){
foreach($reco as $row)
if($row['email'])
{ $username=$row['username'];
$email=$row['email'];
$password=$row['password'];
$this->email->from('systron#micronix.com','Systron');
$this->email->to($email);
$this->email->subject('Email Test');
$this->email->message('"Testing the email class.');
$test=$this->email->send();
echo"Request has been sent plz check your Email for password";
$this->load->view('login_view.php');
}
}else
{
echo "Email id is not exist";
$this->load->view('forget.php');
}
my model
function check($email)
{
$this->db->select('*');
$this->db->from('admin');
$this->db->where('email',$email);
$check=$this->db->get();
$res = $check->result_array();
return $res;
}
Try This:
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => '',
'smtp_pass' => '');
$email=$row['email'];
$msg='your message';
$this->load->library('email', $config);
$this->email->set_mailtype("html");
$this->email->set_newline("\r\n");
$this->email->from('from#gmail.com');
$this->email->to('to#gmail.com');
$this->email->subject('subject');
$this->email->message($msg);
if (!$this->email->send())
{
show_error($this->email->print_debugger());
}