Mailgun PHP integration not working with form submissions

I’m having trouble getting Mailgun to work with my contact forms in PHP. The weird thing is that when I test Mailgun by itself, emails go through just fine. But when I try to use it with my actual forms, nothing gets sent.

require 'vendor/autoload.php';
use Mailgun\Mailgun;

if($_GET['action'] == 'contact_form'){
    $recipient = '[email protected]';
    $body = 'Full Name: '.$_POST['fullname'].'<br />';
    $body .= 'Phone: '.$_POST['phone'].'<br />';
    $body .= 'Email Address: '.$_POST['email_addr'].'<br />';
    $sender = $_POST['fullname'].' <'.$_POST['email_addr'].'>';
    $username = $_POST['fullname'];
    
    $email_subject = 'New Contact Form Submission';
    
    $mail_headers = "MIME-Version: 1.0" . "\r\n";
    $mail_headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
    $mail_headers .= 'From: '.$username.' <'.$_POST['email_addr'].'>' . "\r\n";
    
    dispatch_email($recipient, $sender, $email_subject, $body);
}

else if($_GET['action'] == 'quote_form'){
    $recipient = '[email protected]';
    $body = 'Customer Name: '.$_POST['customer_name'].'<br />';
    $body .= 'Phone Number: '.$_POST['phone_num'].'<br />';
    $body .= 'Email: '.$_POST['user_email'].'<br />';
    $sender = $_POST['customer_name'].' <'.$_POST['user_email'].'>';
    $customer = $_POST['customer_name'];
    
    $email_subject = 'Quote Request Received';
    
    $mail_headers = "MIME-Version: 1.0" . "\r\n";
    $mail_headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
    $mail_headers .= 'From: '.$customer.' <'.$_POST['user_email'].'>' . "\r\n";
    
    dispatch_email($recipient, $sender, $email_subject, $body);
}

function dispatch_email($recipient, $sender, $email_subject, $message_body)
{
    $mailgun = new Mailgun('my-mailgun-api-key');
    $mail_domain = "mail.mywebsite.com";
    
    $response = $mailgun->sendMessage($mail_domain, array(
        'from' => $sender,
        'to' => $recipient,
        'subject' => $email_subject,
        'text' => $message_body
    ));
}

I think the problem might be with the ‘from’ parameter because when I hardcode a static email address instead of using the variable, the emails actually get delivered. Anyone know what could be causing this?

yeah, it’s probly the sender verification issue. mailgun wants emails from verified domains only - using personal emails won’t work. try setting the ‘from’ to ‘[email protected]’ and just use the user email in the ‘reply-to’ header. fixed it for me.

Yeah, it’s definitely the sender domain authentication. Mailgun won’t let you use random email addresses from form submissions as the ‘from’ address - they need to be from a domain you’ve verified with proper SPF/DKIM records. I hit this exact same wall last year and wasted hours on it. Here’s what fixed it: use a static from address like ‘[email protected]’ and stick the original sender’s email in a ‘Reply-To’ header instead. Also noticed you’re setting those mail_headers variables but not using them in your sendMessage call - might want to clean that up too.

This is a domain authentication issue with Mailgun. They reject emails when you use form submission addresses directly in the ‘from’ field because those addresses aren’t verified through your DNS records. I hit this exact problem six months ago and wasted hours debugging it. Here’s the fix: use a verified sender address from your authorized domain for ‘from’, then put the original submitter’s email in ‘reply-to’. Also, you’re building those mail_headers but not passing them to sendMessage, so they’re doing nothing. Just add a ‘reply-to’ parameter to your sendMessage array instead.