SendGrid PHP - Loop not working, only sends one email

荒凉一梦 提交于 2019-12-25 07:46:22

问题


I'm trying to use Sendgrid PHP API (https://github.com/sendgrid/sendgrid-php) to send emails from a php loop. However, the script will only send the first email, and the loop will not continue.

I have a mail function defined like this:

function sendgrid($assoc_number,$to_email,$subject,$message,$HTML_message,$type) {
    require "sendgrid-php/sendgrid-php.php";


    $sendgrid = new SendGrid("api-key-removed");

    $email = new SendGrid\Email();
    $email
    ->setSmtpapiTos($to_email)
    ->setFrom($from_email)
    ->setSubject($subject)
    ->setText($message)
    ->setHtml($HTML_message)
    ->setCategories($type);
    ;

    $sendgrid->send($email);
}

The real loop is much more complicated, but this also stalls after sending the first mail:

$assoc_number = 10;
$to_email = array("me@mydomain.ca");
$subject = "Testing SendGrid loop";
$message = "Testing SendGrid loop.";
$HTML_message = "Testing <strong>SendGrid</strong> loop.";
$type = array("Test");

$i = 0;
while ($i<3) {
    $i++;
    sendgrid($assoc_number,$to_email,$subject,$message,$HTML_message,$type);
    echo $i . "<br>";
}

Any hints on why the loop stalls after the first email is sent?

来源:https://stackoverflow.com/questions/36466830/sendgrid-php-loop-not-working-only-sends-one-email

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!