CodeIgniter Forums
email sending issue - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: email sending issue (/showthread.php?tid=65986)



email sending issue - greenarrow - 08-19-2016

I want to echo an email address i am getting from  a model in controller, right now  here's how i'm doing it

$data['records']['email']

but it won't allow me to echo it inside $this->email->to() function.

i have tried  $this->email->to($data['records']['email']) & $this->email->to( echo $data['records']['email'])  

both ways gave me errors.

how can i print the email address.


RE: email sending issue - PaulD - 08-19-2016

?

You do not need to echo it. This should work:

PHP Code:
$this->email->to($data['records']['email']); 

However, if you are getting errors what error are you getting? You probably have a problem with your array. Are you sure it is a valid reference to a valid array?


RE: email sending issue - Wouter60 - 08-19-2016

Please check the documentation about the Email class.
You will find that the to() method will only accept a string or an array.
The string can contain just one single e-mail address, or a comma delimited list of addresses.
The array must contain e-mail addresses as well.

The examples in the documentation page make clear how it works:
PHP Code:
$this->email->to('[email protected]');
$this->email->to('[email protected][email protected][email protected]');
$this->email->to(
       array('[email protected]''[email protected]''[email protected]')
); 



RE: email sending issue - greenarrow - 08-19-2016

(08-19-2016, 12:45 PM)PaulD Wrote: ?

You do not need to echo it. This should work:

PHP Code:
$this->email->to($data['records']['email']); 

However, if you are getting errors what error are you getting? You probably have a problem with your array. Are you sure it is a valid reference to a valid array?

this is my model for this.

PHP Code:
$query $this->db->select('*')
 
     ->from('users')
 
     ->where('name',$this->input->post('service_advisor'))
 
     ->get();


if(
$query->num_rows() > 0) {

 
    foreach ($query->result() as $row){
$data[] = $row;

}

}
return 
$data 

this is ok right? it didn't show any error message but email is not sending.


RE: email sending issue - PaulD - 08-19-2016

Well, that could be a multitude of reasons.

Here are a few to check.

1. Are you actually getting an email address back from your model call?
If not, then the email will not send. Echo the output to a view as a test that you are getting the data you expected.

2. Can you send emails?
Hard code an email address, and send yourself an email. Did that work? If not, then the problem might be in your email configuration.

3. Where are you working?
On a local host sending emails from Xampp or similar will require additional configuration with your apache emulator to work. It can be notoriously difficult and fiddly (if it does not just work straight away). If on a live server, then ideally you should send emails form via SMTP using a real email address. Otherwise your emails could easily all go straight in the spam folder.


By the way,
PHP Code:
if($query->num_rows() > 0
{
 
    foreach ($query->result() as $row)
 
    {
 
         $data[] = $row;
 
    }


Can just be written as
PHP Code:
if($query->num_rows() > 0
{
 
    $data $query->result_array();




RE: email sending issue - greenarrow - 08-19-2016

(08-19-2016, 09:39 PM)PaulD Wrote: Well, that could be a multitude of reasons.

Here are a few to check.

1. Are you actually getting an email address back from your model call?
If not, then the email will not send. Echo the output to a view as a test that you are getting the data you expected.

2. Can you send emails?
Hard code an email address, and send yourself an email. Did that work? If not, then the problem might be in your email configuration.

3. Where are you working?
On a local host sending emails from Xampp or similar will require additional configuration with your apache emulator to work. It can be notoriously difficult and fiddly (if it does not just work straight away). If on a live server, then ideally you should send emails form via SMTP using a real email address. Otherwise your emails could easily all go straight in the spam folder.


By the way,
PHP Code:
if($query->num_rows() > 0
{
 
    foreach ($query->result() as $row)
 
    {
 
         $data[] = $row;
 
    }


Can just be written as
PHP Code:
if($query->num_rows() > 0
{
 
    $data $query->result_array();


2. Can you send emails?

Hard code an email address, and send yourself an email. Did that work? If not, then the problem might be in your email configuration.


Yes when i hard code it does work. i checked on a live server too, with array in "to" nothing works.


RE: email sending issue - PaulD - 08-19-2016

Ok, so have you checked your array item actually contains the data you were expecting in it. Echo it out to a page and see if it is the value you thought it was. Post here what is actually in your $data['records']['email']


RE: email sending issue - Wouter60 - 08-20-2016

Since ['email'] is a field in every record, you can't address $data['records']['email'] as a separate array.
Try this:
PHP Code:
$recipients array_column($data['records'],'email');
$this->email->to($recipients); 

If this doesn't work either, put this in your controller (temporarily), before the code that should send the e-mails:
PHP Code:
echo '<pre>';
print_r($data);
print_r($data['records']);
$recipients array_column($data['records'],'email');
print_r($recipients);
echo 
'</pre>'

And run the application as if you intend to send the e-mails.


RE: email sending issue - InsiteFX - 08-20-2016

How to send email using HTML templates in Codeigniter