MailGun 检查电子邮件是否已被订阅

MailGun Check if an email already is subscribing

我一直在尝试用我能想到的所有方法来解决这个问题,但肯定漏掉了一些东西。我尝试使用 for 循环访问字段,只是调用它、数组、对象等。我就是无法让它工作。

我说的是检查表单中提供的电子邮件是否已经订阅了我在 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'      => 'noreply@adamastmar.se',
                '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/news@mail...') #2 /home/jivusmc/domains/adamastmar.se/public_html/vendor/mailgun/mailgun-php/src/Mailgun/Mailgun.php(215): 
Mailgun\Connection\RestClient->get('lists/news@mail...', Array) #3 /home/jivusmc/domains/adamastmar.se/public_html/mailinglist.php(16): 
Mailgun\Mailgun->get('lists/news@mail...') #4 {main} thrown in /home/jivusmc/domains/adamastmar.se/public_html/vendor/mailgun/mailgun-php/src/Mailgun/Connection/RestClient.php on line 258

任何帮助 & tips/tricks 不胜感激!

我找到了解决我遇到的问题的方法。我没有在 if 语句中执行所有操作,而是将其包围在一个 try-catch 中。我尝试检查是否可以从 mailgun 列表中提取电子邮件,如果失败,它会捕获错误并将邮件添加到列表中。 (我把它贴在这里是因为几乎不可能找到更好的解决方案)

$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'      => 'noreply@adamastmar.se',
                '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: ./');
        }
    }

我知道这个问题,答案在PHP中。但我刚刚在 NodeJS 中找到了一种方法,希望我早点找到解决方案。也许对某人有帮助。

仅供参考:我正在检查 Mailgun 的邮件列表中是否存在电子邮件。

var DOMAIN = 'YOUR_DOMAIN_NAME';
var mailgun = require('mailgun-js')({ apiKey: "YOUR_API_KEY", domain: DOMAIN});

const array = await mailgun.getList().catch(console.error);

if(checkIfEmailExcistInMailinglist(array.items, dbUser.email)){
     // if then do something
}

checkIfEmailExcistInMailinglist(array, email) {
    for (var i = 0; i < array.length; i++) {
      if (array[i].address === email) {
        return true;
      }
    }
return false;
}