MailGun 401响应禁止我哪里出错了

时间:2018-05-04 03:38:48

标签: php curl mailgun php-curl

我有2个用于mailgun api的脚本我写的那个返回401禁止我在网上发现的一个完美我用同一个域/ api键尝试了两个 这个从github gist中复制粘贴

这个就像一个魅力

<?php
function send_mailgun($email, $body) {
    $domain = "SendingDomain";
    $config = array();
    $config['api_key'] = "key-mailgunkey";
    $config['api_url'] = "https://api.mailgun.net/v3/" . $domain . "/messages";
    $message = array();
    $message['from'] = "Mailgun <user@SendingDomain>";
    $message['to'] = $email;
    $message['h:Reply-To'] = "<user@SendingDomain>";
    $message['subject'] = "Eye-Catching Subject Line";
    $message['html'] = $body;
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $config['api_url']);
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($curl, CURLOPT_USERPWD, "api:{$config['api_key']}");
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $message);
    $result = curl_exec($curl);
    curl_close($curl);
    return $result;
}
echo send_mailgun("personal@domain.com", "Body of the message here!");
?>

这个不起作用

<?php
$to = "<personal@domain.com>";
#$bcc = null;
$replyTo = "<dinner@sendingdoamin>";
#$tag = "tag";
$subject = "subject";
#$text = "HI Test \n\r.";
$html ="<h1>hi</h1>";
$fromUser ='hi <user@';
$domain = 'domain';

    #$filePath=!empty($img)?curl_file_create($img):null;
    $dataSubmit=array(
        'from'  => $fromUser.$domain.'>',
        'to'    => $to,
        #'bcc'  => $bcc,
        #'o:tag'    => $tag,
        #'text' => $text,
        'h:Reply-To'    => $replyTo,
        'subject'   => $subject,
        'html'  => $html,
        #'inline[1]'    => $filePath,
        #'attachment[1]'    => $filePath
    );
    echo '<pre>';
    print_r($dataSubmit);
$service_url = 'https://api.mailgun.net/v3/'.$domain.'/messages';
var_dump($service_url);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$service_url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, 'key-mailgunkey'); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 45);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $dataSubmit);
$result = curl_exec($curl);
$info = curl_getinfo($curl);
    curl_close($curl);
    var_dump($result);
    var_dump($info);


 ?>

正如你所看到的,我评论了我发现的差异,确保它们具有相同的卷曲选项而另一个适用于另一个不会。

1 个答案:

答案 0 :(得分:3)

你忘记的这些小事可以让你疯狂。

** (Protocol.UndefinedError) protocol Ecto.Queryable not implemented for [%Statcasters.League{__meta__: #Ecto.Schema.Metadata<:loaded, "leagues">, id: 16, inserted_at: ~N[2018-05-05 22:13:23.675833], name: "Cam's League", player_limit: 10, updated_at: ~N[2018-05-05 22:13:23.675843], users: #Ecto.Association.NotLoaded<association :users is not loaded>, users_leagues: #Ecto.Association.NotLoaded<association :users_leagues is not loaded>}]

应该以“api:”为前缀,如下所示

curl_setopt($curl, CURLOPT_USERPWD, 'key-mailgunkey'); 

这就是打破骆驼的差异