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?