PHP Email Sending with Mailgun: Troubleshooting SMTP Connection

I’m working on a contact form for my CS class project using PHP and Mailgun. I’m getting a ‘Could not connect to SMTP host’ error. Here’s my code:

<?php
require_once 'config.php';
require_once 'vendor/autoload.php';
require_once 'class.emailsender.php';

use EmailService\Sender;

if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    displayForm('contact.php', ['page_title' => 'Get in Touch']);
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $sender = new Sender();
    
    $sender->useSMTP();
    $sender->setServer('smtp.emailservice.com', 465);
    $sender->setAuth('[email protected]', 'password123');
    $sender->setEncryption('ssl');
    
    $sender->setFrom('[email protected]', 'Contact Bot');
    $sender->addRecipient('[email protected]');
    $sender->setHTMLContent(true);
    
    $sender->setSubject('New Contact Form Submission');
    $sender->setBody($_POST['user_message']);
    
    if ($sender->send()) {
        redirectTo('thank-you.php');
    } else {
        echo 'Message sending failed: ' . $sender->getError();
    }
}
?>

This is part of my MVC setup. I’m using a sandbox domain to avoid buying a private one for this project. Any ideas on what might be causing the SMTP connection issue? I’ve double-checked my credentials and port settings.

hey alex, i had a similar issue. check if ur php.ini has openssl enabled and confirm mailgun creds. sometimes they update without notice. if issues persist, maybe try another email service like sendgrid; their free tier works well.

Have you verified that your firewall isn’t blocking outgoing connections on port 465? Sometimes university networks or local security settings can interfere with SMTP connections. You might try using port 587 with TLS encryption instead, as it’s often more reliable for outgoing mail.

Also, double-check your Mailgun dashboard to ensure your sandbox domain is properly configured and active. Sometimes there’s a delay in domain verification that can cause connection issues.

Lastly, consider implementing error logging in your Sender class to capture more detailed error messages. This could provide valuable insights into what’s specifically failing during the connection attempt. Good luck with your project!

I ran into a similar issue when setting up email functionality for a client’s website. The SMTP connection error can be tricky to diagnose. Have you tried using Mailgun’s API instead of SMTP? It’s often more reliable and easier to implement.

In my experience, switching to the API solved most connection issues. You’ll need to install the Mailgun PHP SDK via Composer and adjust your code slightly. Here’s a basic example:

$mg = Mailgun::create('your-api-key');
$mg->messages()->send('your-domain.com', [
  'from'    => '[email protected]',
  'to'      => '[email protected]',
  'subject' => 'New Contact Form Submission',
  'html'    => $_POST['user_message']
]);

This approach bypasses SMTP altogether and might resolve your connection problems. Just remember to update your error handling accordingly.