Trouble with Dynamic 'From' in PHP Mailgun Integration

Testing PHP Mailgun integration for different forms. Emails send correctly with a fixed sender but fail with dynamic input. Example code:

<?php
require 'auto_loader.php';

use CustomMail\Mailer;

if ($_GET['action'] === 'submit') {
    $recipient = '[email protected]';
    $sender = '[email protected]';
    $userDetail = "Name: {$_POST['username']}\nMobile: {$_POST['mobile']}";
    
    executeEmail($recipient, $sender, 'Form Submission', $userDetail);
}

function executeEmail($to, $from, $subject, $body) {
    $mailService = new Mailer('new-mailgun-key');
    $domainID = 'mg.customdomain.com';
    $mailService->send($domainID, [
        'from' => $from,
        'to' => $to,
        'subject' => $subject,
        'text' => $body
    ]);
}
?>

In my experience, having a dynamic ‘from’ address often triggers Mailgun’s verification checks and domain restrictions. I found that when switching from a fixed sender to a user input, Mailgun might reject the email if the dynamic address hasn’t been properly verified or doesn’t match the domain settings configured in your account. It was helpful to either sanitize and validate the input thoroughly or use a consistent domain as the sender while dynamically including the user’s email in the reply-to header. This maintains both functionality and compliance with Mailgun’s policies.