Santiago Santiago - 3 months ago 15
PHP Question

Php array error with sendgrid

This is my script for sending newsletter to all my users. The code below has a limit 5 for testing.
I'm currently having this error:


Catchable fatal error: Argument 1 passed to SendGrid\Email::setTos() must be of the type array, string given, called in /var/www/web/web/sengrid.php on line 44 and defined in /var/www/web/web/sendgrid-php/lib/SendGrid/Email.php on line 90**


<?php
ini_set("display_errors", "1");
error_reporting(E_ALL);
require_once ('inc/db.php');
require("sendgrid-php/sendgrid-php.php");

$sendgrid = new SendGrid('myapikey');

$email = new SendGrid\Email();


$resultado = mysqli_query($dbc,"SELECT email, hash FROM newsletter WHERE enviado = '0' AND newsletter = '1' LIMIT 5");

$totalRows = mysqli_num_rows($resultado);
if ($totalRows == 0){
echo "<p>No results</p>";
}

$rss = "";

if(!$xml = simplexml_load_file('http://www.test.com.ar/rss.php') ) {
echo 'unable to load XML file';
} else {
foreach( $xml->channel->aviso as $aviso ) {

$rss .= "<div class='box-blanco'>";
$rss .= "<a href='$aviso->link' class='link'><p class='titulo'>$aviso->title</p></a>";
$rss .= "<p class='text-muted-2'>$aviso->provincia $aviso->remuneracion</p>";
$rss .= "</div><div class='col-separador-h'></div>";

}

}


Here is the loop for sending all the email using setTos.

while ($usuario = mysqli_fetch_array($resultado, MYSQLI_ASSOC)) {


$usermail = $usuario['email'];
$hash = $usuario['hash'];
$email-> setTos($usermail)
->setFrom("no_responder@test.com.ar")
->setFromName("test")
->setReplyTo("no_responder@test.com.ar")
->setSubject("Convocatorias Semanales")
->setHtml('<html><body>TEST</body></html>');



try {

$result = $sendgrid->send($email);
mysqli_query($dbc, "UPDATE newsletter SET enviado = '1' WHERE email='$usermail' ");
echo "enviado";

} catch(\SendGrid\Exception $e) {
echo $e->getCode() . "\n";
foreach($e->getErrors() as $er) {
echo $er;
}
}

}


?>


How can i solve this? i can't figure it out.
https://github.com/sendgrid/sendgrid-php/tree/master/lib/helpers/mail

Answer

It already says it expecting an array, so feed it with it:

$email-> setTos(array($usermail));

This is a just a band aid fix though, you might want to get all the emails first inside a container, then send it:

$all_users = array(); // intialzie array

while ($usuario = mysqli_fetch_array($resultado, MYSQLI_ASSOC)) {

    $usermail = $usuario['email']; 
    $hash = $usuario['hash'];
    mysqli_query($dbc, "UPDATE newsletter SET enviado = '1' WHERE email='$usermail' "); 

    $all_users[] = $usermail; // push all emails first
}

// then send

 try {
    $email->setTos($all_users)
    ->setFrom("no_responder@test.com.ar")
    ->setFromName("test")
    ->setReplyTo("no_responder@test.com.ar")
    ->setSubject("Convocatorias Semanales")
    ->setHtml('<html><body>TEST</body></html>');

    $result = $sendgrid->send($email);
    echo "enviado";

} catch(\SendGrid\Exception $e) {
    echo $e->getCode() . "\n";
    foreach($e->getErrors() as $er) {
        echo $er;
    }
}