简体   繁体   English

向数据库记录发送电子邮件时出现Codeigniter错误

[英]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 消息:mail():SMTP服务器响应:503 5.0.0需要RCPT(收件人)文件名: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(). 无法使用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 任何对errpr的帮助将不胜感激

CodeIgniter's email module expects an array of email addresses in the to() function. CodeIgniter的电子邮件模块在to()函数中需要一个电子邮件地址数组 Your model's emailsend() function adds an email index to that array that isn't expected. 模型的emailsend()函数会向该数组添加emailsend()email索引。 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. 同样,您在foreach循环的每次迭代中都会覆盖该email索引的值,这意味着您的数组将仅包含数据库中的最后一个地址。

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. 我的意思是该数组上的var dump将始终是一个值,因为u总是重新分配该值,因此将返回一封电子邮件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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