2016-08-29 49 views
2

这是我发送简报给所有用户的脚本。以下代码对测试有一个限制5。 我目前有这个错误:使用sendgrid发送Php数组错误

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

    }   

} 

下面是利用发送的setTos所有电子邮件循环。

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


$usermail = $usuario['email']; 
$hash = $usuario['hash']; 
$email-> setTos($usermail) 
    ->setFrom("[email protected]") 
    ->setFromName("test") 
    ->setReplyTo("[email protected]") 
    ->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; 
     } 
    } 

} 


?> 

我该如何解决这个问题?我无法弄清楚。 https://github.com/sendgrid/sendgrid-php/tree/master/lib/helpers/mail

回答

1

它已经表示,它期待的数组,因此用它喂它:

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

这是一个公正的创可贴修复,虽然,你可能希望得到所有的电子邮件第一容器内,然后把它:

$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("[email protected]") 
    ->setFromName("test") 
    ->setReplyTo("[email protected]") 
    ->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; 
    } 
} 
+0

我还在与阵列 – Santiago

+0

@Santiago同样的错误我已经作出的编辑'$的电子邮件 - > setTos($ ALL_USERS)' – Ghost