I’m building a newsletter signup form using PHP and the Mailgun service. I have a weird problem where emails only get delivered to the email address I used when signing up for my Mailgun account. When I test the form with my main email, everything works fine and I receive the confirmation message. But when I try using different email addresses, nothing happens.
Here’s my configuration file:
<?php
require_once 'vendor/autoload.php';
define('MG_API_KEY', 'key-3df51g6f34d91c1e777g4f....');
define('MG_PUBLIC_KEY', 'pubkey-9dg8236007....');
define('MG_DOMAIN', 'sandbox2b4fbff8785d5b0185ggb9e72956eeg6.mailgun.org');
define('SUBSCRIBER_LIST', '[email protected]');
define('MG_SECRET_KEY', '...');
$mgClient = new Mailgun\Mailgun(MG_API_KEY);
$mgValidator = new Mailgun\Mailgun(MG_PUBLIC_KEY);
$mgOptInHandler = $mgClient->OptInHandler();
?>
And here’s my main processing script:
<?php
require_once 'config.php';
if(isset($_POST['user_name'], $_POST['user_email']))
{
$userName = $_POST['user_name'];
$userEmail = $_POST['user_email'];
$emailCheck = $mgValidator->get('address/validate', [
'address' => $userEmail
])->http_response_body;
if($emailCheck->is_valid)
{
$confirmHash = $mgOptInHandler->generateHash(SUBSCRIBER_LIST, MG_SECRET_KEY, $userEmail);
$mgClient->sendMessage(MG_DOMAIN, [
'from' => '[email protected]',
'to' => $userEmail,
'subject' => 'Confirm your newsletter subscription',
'html' => "Hi {$userName}<br><br>Thanks for subscribing! Please click below to confirm"
]);
$mgClient->post('lists/' . SUBSCRIBER_LIST . '/members', [
'name' => $userName,
'address' => $userEmail,
'subscribed' => 'no'
]);
header('Location: http://localhost:8888/project/index.php');
}
}
?>
What could be causing this issue? Is there some setting I’m missing?