Mail function is working only once at a time : Codeigniter - 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 ?

Related

Codeigniter - Receiving Email but not receiving message which I send in Email

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

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

Is not receiving email in codeigniter

I cant receive any email but my if statements says that an email was successfully sent, please help, here is my controller
public function index()
{
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => '****', //email
'smtp_pass' => '****' //password
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('//email', '//name');
$this->email->to('//recipient');
$this->email->subject('This is a test email');
$this->email->message('Hi, This is a test email. Please advise!');
if($this->email->send())
{
echo 'Your email was successfully sent.';
}
else
{
show_error($this->email->print_debugger());
}
}
btw i tested it by using localhost/email/index.php/email/index
Your mail sending part is correct. No issues with that.
If you are trying this in local you have to edit you mail setting in php.ini and sendmail.ini.
Or else upload it to live server into new folder name demo(Not root). so you can test it easily.

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