Google App Engine上的Mailgun API

时间:2016-05-03 04:03:49

标签: php google-app-engine curl mailgun

我尝试通过Mailgun API Google App Engine上的PHP发送电子邮件,但是,我的请求一直出现401错误。

    define(DOMAIN,'sandbox-----------.mailgun.org');
    define(MAILGUN_API,'key-xxxxxxxxxxxxx');


    function mg_send($to, $subject, $message) {

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($ch, CURLOPT_USERPWD, 'api: '.MAILGUN_API);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        $plain = strip_tags(nl2br($message));

        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v3/'.DOMAIN.'/messages');
        curl_setopt($ch, CURLOPT_POSTFIELDS, array(
            'from' => 'support@'.DOMAIN,
            'to' => $to,
            'subject' => $subject,
            'html' => $message,
            'text' => $plain)
        );

        $j = json_decode(curl_exec($ch));

        $info = curl_getinfo($ch);


        if($info['http_code'] != 200) {
            echo($info['http_code']);
        }
        curl_close($ch);

        return $j;
    }

    $r = mg_send('authorized@email.com','Test Email','This is the test email.');

我已授权我尝试发送的电子邮件,验证了我的帐户,对我的API密钥进行了三次检查,并确保在curl中启用了php.ini。有关为何发生这种情况的任何见解?

0 个答案:

没有答案