使用PHP返回状态代码0在Azure上使用sendgrid发送电子邮件

时间:2018-03-10 12:55:47

标签: php azure sendgrid

我的PHP应用程序托管在Azure上,我正在使用SendGrid PHP client向应用程序用户发送电子邮件。一切都工作正常,直到昨天晚上,电子邮件停止工作后突然间。在进一步调试时,我发现了以下代码

$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();

将statusCode返回为零。我理解的0不是由服务器返回的。为了交叉检查,我通过我当地的XAMPP PHP服务器发送了测试邮件,效果很好。所以我假设Azure无法访问sendgrid服务器。

请注意,相同的代码一直工作到昨天晚上,无论是代码更改还是设置更改都没有触发任何问题。

为了重现这个问题,我创建了一个简单的程序

public function test_sendgrid_email(){  
        $api_key = '***************************'; //Hidden for security reasons
        $request_body = [
            'personalizations' => [
                [
                    'to' => [
                        [
                            'email' => 'test@example.com',
                            'name' => 'Test'
                        ]
                    ],
                    'subject' => "Sending with SendGrid is Fun"
                ]
            ],
            'from' => [
                'email' => 'example@test.com',
                'name' => 'Example'
            ],
            'content' => [
                [
                    'type' => 'text/html',
                    'value' => "and easy to do anywhere, even with PHP"
                ]
            ]
        ];

        $request_body_formatted = json_decode(json_encode(($request_body)));

        $sg = new \SendGrid($api_key);

        try{
            $response = $sg->client->mail()->send()->post($request_body_formatted);                
            return ['status' => 'success', 'message' => 'status code : ' . $response->statusCode()];
        } catch (Exception $ex) {
            return ['status' => 'failure', 'message' => 'Failure'];
        }            
    }

输出:{“状态”:“成功”,“消息”:“状态代码:0”}

Azure设置中是否有任何必须更改才能恢复工作? Azure环境的任何最新更新都会导致此问题?

0 个答案:

没有答案
相关问题