Using PHPMailer and Mailgun API to send emails from a shared hosting environment

I’m having trouble sending emails from my GoDaddy shared hosting server. The Mailgun WordPress plugin works fine, but I can’t get PHPMailer or php mail() to function.

My setup includes:

  • A WordPress site and a custom intranet on the same server
  • Mailgun as the mail server with properly configured MX and TXT records
  • An additional Office 365 email server with its own MX record

It appears that the Mailgun plugin uses HTTP instead of SMTP, and I want to replicate that in my PHP code. However, I’m not sure how to achieve this.

Below is a simplified example of what I think the plugin might be doing:

function send_email_via_api($to, $subject, $message) {
    $api_key = 'your_api_key';
    $domain = 'your_domain.com';
    $endpoint = 'https://api.mailgun.net/v3/' . $domain . '/messages';

    $data = array(
        'from' => 'Sender <[email protected]>',
        'to' => $to,
        'subject' => $subject,
        'text' => $message
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $endpoint);
    curl_setopt($ch, CURLOPT_USERPWD, 'api:' . $api_key);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);
    curl_close($ch);

    return $response;
}

Can someone explain how to use the Mailgun API with PHPMailer or suggest an alternative method for sending emails from my shared hosting environment?

I’ve encountered similar challenges with shared hosting environments. One effective approach is to utilize Mailgun’s HTTP API directly, as you’ve outlined. However, to integrate this with PHPMailer, you’ll need to create a custom SMTP transport.

Here’s a concise example:

use PHPMailer\PHPMailer\PHPMailer;

class MailgunTransport {
    private $apiKey;
    private $domain;

    public function __construct($apiKey, $domain) {
        $this->apiKey = $apiKey;
        $this->domain = $domain;
    }

    public function send(PHPMailer $mailer) {
        $endpoint = 'https://api.mailgun.net/v3/' . $this->domain . '/messages';
        $data = [
            'from' => $mailer->From,
            'to' => implode(',', array_keys($mailer->getAllRecipientAddresses())),
            'subject' => $mailer->Subject,
            'text' => $mailer->Body
        ];

        $ch = curl_init($endpoint);
        curl_setopt($ch, CURLOPT_USERPWD, 'api:' . $this->apiKey);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);
        curl_close($ch);

        return $response;
    }
}

// Usage
$mail = new PHPMailer();
$transport = new MailgunTransport('your_api_key', 'your_domain.com');
$mail->setFrom('[email protected]');
$mail->addAddress('[email protected]');
$mail->Subject = 'Test Subject';
$mail->Body = 'Test Message';
$result = $transport->send($mail);

This approach should work reliably in your shared hosting environment.

hey Tom, i’ve faced similar issues. have you tried using the mailgun php library instead of raw curl? it’s easier to work with. just install it via composer and use their official docs. also, make sure your api key is correct and you’re using the right domain. good luck!

I’ve been through the same struggle with shared hosting and email sending. Here’s what worked for me:

Instead of using PHPMailer or the native php mail() function, I switched to using Mailgun’s API directly. It’s much more reliable on shared hosts.

First, I installed the Mailgun PHP SDK via Composer. Then, I set up a simple function to handle email sending:

function sendMailgunEmail($to, $subject, $message) {
    $mg = Mailgun::create('your-api-key');
    $domain = 'your-domain.com';
    
    $result = $mg->messages()->send($domain, [
        'from'    => 'Sender <[email protected]>',
        'to'      => $to,
        'subject' => $subject,
        'text'    => $message
    ]);

    return $result;
}

This approach bypasses any server-level email restrictions and uses Mailgun’s API directly. It’s been rock-solid for me, even on budget shared hosting plans. Just make sure to keep your API key secure!