Codeigniter - Receiving Email but not receiving message which I send in Email - 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();
}
}
?>

Related

Mail function is working only once at a time : Codeigniter

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 ?

send mail from depending on the selected user in codeigniter

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";
}
}
}

config email library for send email in codeigniter

I have a code in controller codeigniter framework as follows :
class Welcome extends CI_Controller {
public function index()
{
$this->load->view('welcome_message');
$this->load->library('email');
$this->email->from('example#yahoo.com', 'example');
$this->email->to('example#gmail.com','example');
$this->email->subject('my Subject');
$this->email->message('my Message');
if ($this->email->send())
echo "Mail Sent!";
else
echo "There is error in sending mail!";
}
}
This code correctly sends email . but when that i am config email class setting , this code not work and not send email . for example follow code will not send email :
class Welcome extends CI_Controller {
public function index()
{
$this->load->view('welcome_message');
$this->load->library('email');
$this->email->from('example#yahoo.com', 'example');
$this->email->to('example#gmail.com','example');
$this->email->subject('my Subject');
$this->email->message('my Message');
$config['protocol'] = 'sendmail';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$this->email->initialize($config);
if ($this->email->send())
echo "Mail Sent!";
else
echo "There is error in sending mail!";
}
}
Please tell me where is the problem .
thanks .
Protocol can be Sendmail, SMTP and mail.
You should load library after configuration:
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'xxx',
'smtp_pass' => 'xxx',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('example#yahoo.com', 'example');
$this->email->to('example#gmail.com','example');
$this->email->subject('my Subject');
$this->email->message('my Message');
if ($this->email->send())
{
echo "Mail Sent!";
}
else
{
echo "There is error in sending mail!";
}

Send email by codeigniter

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

Email is not send in users email

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

Resources