Reputation: 43
Am trying to send an email in codeigniter. I first created an independent function in the model and it's corespondent in the controller and it worked very well. The email was sent and received in both yahoo and gmail. However when I tried to use the same code in another function where am selection the email address (recipient) from the database, it gives me a message that email successfully sent but it actually doesn't deliver to yahoo or gmail. what could be the problem? It' not even delivering into the spam/bulk mail.
the codes for the one that worked and that that failed are bellow.
the model function is
function sendMail()
{
$to = "[email protected]";
$subject = "My Sublect";
$messagetext= "stop distubbing me and work!";
$config=array(
'protocol'=>'smtp',
'smtp_host'=>'mail.mydomail.co.ug',
'smtp_port'=>25,
'smtp_user'=>'[email protected]',
'smtp_pass'=>'mypass'
);
$this->load->library("email",$config);
$this->email->set_newline("\r\n");
$this->email->from("[email protected]","Joyce");
$this->email->to($to);
$this->email->subject($subject);
$this->email->message($messagetext);
if($this->email->send())
{
echo "Mail send successfully!";
}
else
{
show_error($this->email->print_debugger());
}
}
the controller fcn
function sendmail()
{
$this->load->model('customer_model');
$this->customer_model->sendMail();
}
However the one that failed to work is bellow
function customer()
{
$this->load->library('email');
$this->load->database();
$data = array(
'name'=>$this->input->post('name'),
'contact'=>$this->input->post('contact'),
'entity'=>$this->input->post('entity'),
'sector'=>$this->input->post('sector'),
'inquiry'=>$this->input->post('inquiry'),
'nature_of_inquiry'=>$this->input->post('nature_of_inquiry'),
'status'=>$this->input->post('status'),
);
$this->db->insert('customers',$data);
$id = $this->db->insert_id();
$refno = date('d/m/Y').'-C'.str_pad($id, 4, "0", STR_PAD_LEFT);
$this->db->query("UPDATE customers set refno = '".$refno."' WHERE id = '".$id."'");
$query=$this->db->query("select entity,name,status,contact from customers where id ='".$id."'");
foreach ($query->result() as $row)
{
$entity = $row->entity;
$phone = $row->contact;
$name = $row->name;
$status = $row->status;
}
$query1=$this->db->query("select phone, email from services where entity = '".$entity."'");
foreach ($query1->result() as $row)
{
$to = $row->email;
}
$sms ="Dear $name, your request/compaint has been $status";
//$emailtext ="yo company has been refferenced by servicecops.";
//$this->sendSMS(test,$phone,$sms);
//$subject = "Servicecops Reminder";
$config=array(
'protocol'=>'smtp',
'smtp_host'=>'mail.mydomain.co.ug',
'smtp_port'=>25,
'smtp_user'=>'[email protected]',
'smtp_pass'=>'mypass',
'mailtype'=>'html'
);
$this->load->library("email",$config);
$this->email->set_newline("\r\n");
$this->email->from("[email protected]","Joyce");
$this->email->to($to);
$this->email->subject("my subject");
$this->email->message("yo company has been refferenced by me");
if($this->email->send())
{
echo "Mail send successfully!";
echo $to;
}
else
{
show_error($this->email->print_debugger());
}
return $this;
}
Upvotes: 0
Views: 6986
Reputation: 43
Guys i have discovered the problem. I had defined the email library twice in the same function that's why messages where not reaching yahoo and gmail inbox. at the begining of the function, i had put. $this->load->library('email'); and this in the midle. $this->load->library("email",$config);
Upvotes: 1
Reputation: 139
Most of the mails getting spam if you use IP address while running with the URL(ex: 123.117.116.27/test/email/) instead of domain name. So check with spam folder.
Upvotes: 0
Reputation: 672
First try to check if you are fetching the emails well from database. If yes then check in the spam. That would solve the problem I guess.
Upvotes: 4