简体   繁体   中英

Codeigniter Error when sending email to database records

I am trying to send an email to multiple contacts in my database list from an input form, it works when i hard code a specific email address, but when i try to refer to my database, i get the error:

Message: mail(): SMTP server response: 503 5.0.0 Need RCPT (recipient) Filename: libraries/Email.php

I also get another error, which i think may be just my email configuration in general and i am unsure of it to;

Unable to send email using PHP mail(). Your server might not be configured to send mail using this method.

MY Controller:

public function sendmail() {
$this->load->library('email');
$this->load->model('Email_model');
$this->load->library('session');

$this->email->from($this->input->post('email'), $this->input->post('name'));

$sendTo['email'] = $this->Email_model->emailsend();

$this->email->to($sendTo);
$this->email->subject('Hello This is an email');

$this->email->message($this->input->post('message'));



if ($this->email->send()){
$this->session->set_flashdata('success','Email has been sent');
redirect('dashboard');
}else{


echo $this->email->print_debugger();

}

My Model

class Email_model extends CI_Model {


public function emailsend() {

  $query = $this->db->query("SELECT email from contacts");
  $sendTo=array();
  foreach ($query->result() as $row) 
  {
      $sendTo['email']=$row->email; 
  }

My View

<form action="<?php echo site_url('/Dashboard/sendmail');?>" method="post" accept-charset="utf-8">

<?php echo form_open('/Dashboard/sendmail');?>

  <label for="name">Your Name</label><input id="name" type="text" name="name">

  <label for="email">Your Email</label><input id="email" type="text" name="email">

  <label for="message">Your Message</label>
  <textarea id="message" name="message" rows="8" cols="50"></textarea>

  <input id-"submit" type="submit" name="submit" value="submit">
<?php echo form_close(); ?>

<p><?php echo $flash;?></p>

Any help for either errpr will be massively appreciated

CodeIgniter's email module expects an array of email addresses in the to() function. Your model's emailsend() function adds an email index to that array that isn't expected. Also, you're overwriting that email index's value on every iteration of the foreach loop, meaning your array will only ever contain the last address from your database.

It also looks like your function doesn't actually return the array after filling it, but it could be that this part is simply truncated in your question.

public function emailsend() {

    $query = $this->db->query("SELECT email from contacts");
    $sendTo=array();
    foreach ($query->result() as $row) 
    {
        $sendTo[] = $row->email;
        //     ^^ remove the 'email' index
    }

    return $sendTo; // <-- add this line if you don't have it
}

Then, from your controller, you don't need to store the result in a separate variable unless you want to perform some additional logic on it. You can simply do this:

$this->email->from($this->input->post('email'), $this->input->post('name'));
$this->email->to($this->Email_model->emailsend());
$this->email->subject('Hello This is an email');
public function emailsend() {
   $query = $this->db->query("SELECT email from contacts");
   $sendTo=array();
   foreach ($query->result() as $row) 
   {
      //u are not changing the key
      $sendTo['email']=$row->email; 
   }
   //iguess u are returning 
   return $sendTo;

}

i mean a var dump on that array would be always one value, because u are reasigning the value always, then one email would be returned.

I suggest to build the array like this

foreach ($query->result() as $row) 
   {

      $sendTo[]=$row->email;        
   }

then the vardump will look like this

array(11) {
 [0]=>
 string(15) "email0@mail.com"
 [1]=>
 string(15) "email1@mail.com"
 [2]=>
 string(15) "email2@mail.com"
  [3]=>
  string(15) "email3@mail.com"
  [4]=>
  string(15) "email4@mail.com"
  [5]=>
  string(15) "email5@mail.com"
  [6]=>
  string(15) "email6@mail.com"
  [7]=>
  string(15) "email7@mail.com"
  [8]=>
  string(15) "email8@mail.com"
  [9]=>
  string(15) "email9@mail.com"
  [10]=>
  string(16) "email10@mail.com"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM