2017-04-13 4 views
1

私はこれをすべての方法で解決しようとしていますが、確かに何かを逃してしまっています。私はfor-loopsを使ってフィールドにアクセスしようとしましたが、配列、オブジェクトなどを呼び出すだけで動作させることができませんでした。MailGun電子メールがすでに購読中であることを確認してください

私が話しているのは、ある形式の提供された電子メールが既にMailGunのマイリストに購読しているかどうかを確認することです。私はこれをチェックする方法を知らないし、私は今約1-2時間のためにウェブを検索してきました、そして、私は最終的にここでも尋ねています。

私のコード今のところ:?

<?php 
session_start(); 
ini_set('display_errors', 1); 
require_once 'init.php'; 
if (!isset($_POST['email']) && isset($_POST['name'])) { 
    echo 'You have to provide an email!'; 
} else if (!isset($_POST['name']) && isset($_POST['email'])) { 
    echo 'You have to provide a name!'; 
} else if (isset($_POST['name'], $_POST['email'])) { 

    $name = $_POST['name']; 
    $email = $_POST['email']; 

    // This is temporary to test and only works if an email existing is provided. 
    // If an invalid email is provided, an error is cast 
    // See below for the error 
    if (!$mailgun->get('lists/' . MAILGUN_LIST . '/members' . $email)) { 

     echo "Email doesnt exist"; 
     die(); 

     $validate = $mailgunValidate->get('address/validate', [ 
      'address' => $email 
     ])->http_response_body; 

     if ($validate->is_valid) { 
      $hash = $mailgunOptIn->generateHash(MAILGUN_LIST, MAILGUN_SECRET, $email); 

      $mailgun->sendMessage(MAILGUN_DOMAIN, [ 
       'from'  => '[email protected]', 
       'to'  => $email, 
       'subject' => 'Please confirm your subscription to the mailing list', 
       'html'  => " 
        Hello {$name},<br><br> 
        You signed up to our mailing list. Please confirm your subscription below.<br><br> 
        <a href='http://www.adamastmar.se/confirm.php?hash={$hash}'>Click here to confirm</a>" 

      ]); 

      $mailgun->post('lists/' . MAILGUN_LIST . '/members', [ 
       'name'   => $name, 
       'address'  => $email, 
       'subscribed' => 'no' 
      ]); 

      $_SESSION['joined'] = "A message has been sent to the provided email. Please confirm the subscription by clicking the link in the mail."; 
      header('Location: ./'); 
     } 
    } else { 
     $_SESSION['alreadysub'] = "You are already a subscriber to this list!"; 
     header('Location: ./'); 
    } 
} 

>

私は上記のコードを使用している場合、私は取得エラー:すべてのヘルプ&のヒント/トリックが高く評価され

Uncaught exception 'Mailgun\Connection\Exceptions\MissingEndpoint' with message 'The endpoint you've tried to access does not exist. 
Check your URL.' in /home/jivusmc/domains/adamastmar.se/public_html/vendor/mailgun/mailgun-php/src/Mailgun/Connection/RestClient.php:258 
Stack trace: #0 /home/jivusmc/domains/adamastmar.se/public_html/vendor/mailgun/mailgun-php/src/Mailgun/Connection/RestClient.php(110): 
Mailgun\Connection\RestClient->responseHandler(Object(GuzzleHttp\Psr7\Response)) 
#1 /home/jivusmc/domains/adamastmar.se/public_html/vendor/mailgun/mailgun-php/src/Mailgun/Connection/RestClient.php(195): 
Mailgun\Connection\RestClient->send('GET', 'lists/[email protected]') #2 /home/jivusmc/domains/adamastmar.se/public_html/vendor/mailgun/mailgun-php/src/Mailgun/Mailgun.php(215): 
Mailgun\Connection\RestClient->get('lists/[email protected]', Array) #3 /home/jivusmc/domains/adamastmar.se/public_html/mailinglist.php(16): 
Mailgun\Mailgun->get('lists/[email protected]') #4 {main} thrown in /home/jivusmc/domains/adamastmar.se/public_html/vendor/mailgun/mailgun-php/src/Mailgun/Connection/RestClient.php on line 258 

答えて

1

私が持っていた問題の解決策を見つけました。 if文ですべてを行う代わりに、代わりにtry-catchで囲んでいました。私は電子メールが郵便銃リストからフェッチできるかどうかを確認しようとします。失敗した場合は、エラーを捕捉し、メールをリストに追加します。 (私はここにそれを投稿しています。なぜなら、これをより良い方法で解決することはほとんど不可能だからです)

$name = $_POST['name']; 
    $email = $_POST['email']; 

    try { 
     $mailgun->get('lists/' . MAILGUN_LIST . '/members/' . $email); 

     $_SESSION['alreadysub'] = "You are already a subscriber to this list!"; 
     header('Location: ./'); 
    } catch (Exception $e) { 
     $validate = $mailgunValidate->get('address/validate', [ 
      'address' => $email 
     ])->http_response_body; 

     if ($validate->is_valid) { 
      $hash = $mailgunOptIn->generateHash(MAILGUN_LIST, MAILGUN_SECRET, $email); 

      $mailgun->sendMessage(MAILGUN_DOMAIN, [ 
       'from'  => '[email protected]', 
       'to'  => $email, 
       'subject' => 'Please confirm your subscription to the mailing list', 
       'html'  => " 
        Hello {$name},<br><br> 
        You signed up to our mailing list. Please confirm your subscription below.<br><br> 
        <a href='http://www.adamastmar.se/confirm.php?hash={$hash}'>Click here to confirm</a>" 

      ]); 

      $mailgun->post('lists/' . MAILGUN_LIST . '/members', [ 
       'name'   => $name, 
       'address'  => $email, 
       'subscribed' => 'no' 
      ]); 

      $_SESSION['joined'] = "A message has been sent to the provided email. Please confirm the subscription by clicking the link in the mail."; 
      header('Location: ./'); 
     } 
    } 
関連する問題