Search code examples
phpcodeigniteremailphpmailercodeigniter-3

sending mail to multiple email address in codeigniter php not working


i am trying to send mails to emails selected using checkbox in codeigniter website using phpmailer,

<input class="acb" type="checkbox"  name="email[]" value="<?= $val->email?>"  />

PHP

$this->load->library('phpmailer_lib');
$mail = $this->phpmailer_lib->load();
$mail->isSMTP();
$mail->Host     = '';
$mail->SMTPAuth = true;
$mail->Username = '';
$mail->Password = '';
$mail->SMTPSecure = 'ssl';
$mail->Port     = 465;
$mail->setFrom('admin@', 'Test');
$mail->addReplyTo('admin@', 'Test');
$mail->addAddress(implode(', ', $this->input->post('email')));
$mail->Subject = 'Important';
$mail->isHTML(true);
$mailContent = $this->input->post('content');
$mail->Body = $mailContent;
$mail->send();

However this does not work; it gives me error.

Can anyone please tell me how to fix it?

Thanks in advance.


Solution

  • This is actually PHPMailer code you're using, and PHPMailer's addAddress does not accept multiple email addresses.

    http://phpmailer.github.io/PHPMailer/classes/PHPMailer-PHPMailer-PHPMailer.html#method_addAddress

    You will need to loop through the list and add each individually if you want to use that method.

    There is no PHP-generated error because passing extra parameters to a method does not generate any kind of error in PHP; they are passed into the method via the $argv array but do not have unique variable names because you do not have parameter placeholders defined for them.

    EDIT

    What I just said is still true but looking at it again I see now you're only passing in a single argument, the resulting string from implode so it doesn't apply to this specifically.