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

时间:2017-04-13 21:12:46

标签: php email mailgun

我一直试图以各种方式解决这个问题,但我肯定错过了一些东西。我尝试使用for循环访问字段,只是调用它,数组,对象等。我只是无法使其工作。

我正在谈论的是检查表格中提供的电子邮件是否已在MailGun订阅我的maillist。我不知道怎么检查这个,我现在在网上搜索大约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: ./');
    }
}

&GT;

如果我使用上面的代码,我得到的错误:

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

任何帮助&amp;感谢提示/技巧!

2 个答案:

答案 0 :(得分:1)

我找到了解决问题的方法。我没有在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: ./');
        }
    }

答案 1 :(得分:0)

我知道这个问题,答案在 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;
}