I have a ticket system. So I want whenever a user creates a new ticket a mail will be send to all the employees who work on that department.
Following is my model code:
public function ticket_email()
{
$query = $this->db->select('staff_sign_up.email')
->distinct('staff_sign_up.email')
->from('staff_sign_up')
->join('view_ticket','view_ticket.initial_department=staff_sign_up.department_id')
->get();
return $query->result();
}
This model is fetching all the mail id of those employees who are working in that department.
Following is my controller code:
public function ticket_send() {
$subject = 'A Ticket has been added please check & reply asap';
$message = '
<h3 align="center">Ticket Details</h3>
<table border="1" width="100%" cellpadding="5">
<tr>
<td width="30%">Subject</td>
<td width="70%">' . $this->input->post("subject") . '</td>
</tr>
<tr>
<td width="30%">Description</td>
<td width="70%">' . $this->input->post("description") . '</td>
</tr>
</table>
';
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => '******@gmail.com',
'smtp_pass' => '*******',
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$this->load->library('email');
$this->email->initialize($config);
$this->email->set_newline("
");
$this->email->from('manojit.chakraborty58@gmail.com', 'admin');
$this->email->to($this->auth_model->ticket_email());
$this->email->subject($subject);
$this->email->message($message);
if ($this->email->send()) {
$this->session->set_flashdata('message', 'Your ticket has been generated,please wait sometime for reply');
} else {
show_error($this->email->print_debugger());
}
}
So,this is the controller is supposed to send mails to all those employees who work on that department with the ticket details.
I'm having these errors:
object of stdclass could not be converted into string
and
preg_match expects parameter 2 to be string but object given
I'm using CodeIgniter 3 and am a beginner.
Edit after Alex's comment:
result_array is returning the following item:-
Array ( [0] => Array ( [email] => *****@gmail.com ) [1] => Array ( [email] => *******@example.com ) [2] => Array ( [email] => *******@gmail.com ) [3] => Array ( [email] => ******@gmail.com ) )
After the latest update by Alex:
You can try change the smtp host to ssl://smtp.gmail.com
Since you don't seem to be getting it in the comments: $this->auth_model->ticket_email()
according to your scheme returns the result object. You need to get a string (or an array) that looks like:
$this->email->to('one@example.com, two@example.com, three@example.com');
OR
$this->email->to(
array('one@example.com', 'two@example.com', 'three@example.com')
);
https://www.codeigniter.com/user_guide/libraries/email.html#CI_Email::to
So at the very least you should be returning a result_array()
but chances are you'll have to do some work to convert it into an array CI will accept.
Update
public function ticket_email()
{
$query = $this->db->select('staff_sign_up.email')
->distinct('staff_sign_up.email')
->from('staff_sign_up')
->join('view_ticket','view_ticket.initial_department=staff_sign_up.department_id')
->get();
if ($query->num_rows() == 0) {
show_error('whatever');
}
return array_column($query->result_array(), 'email'); // make array CI email lib can understand
}
and you can keep everything else the way it is. Although I would advise to check if num_rows() > 0
and handle circumstances where it isn't accordingly.