Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tnort's avatar
Level 4

Mailgun api 503 RCPT command expected

Hi all,

I have tried to set up Mailgun with my Laravel app. I managed to configure the SMTP, works just fine. But when trying to set up the API I am not able to deliver emails and actually get an error as such "503 RCPT command expected"

In my .env I have the following setup:

MAIL_MAILER=mailgun
MAILGUN_DOMAIN=https://api.eu.mailgun.net/v3/MY_DOMAIN 
MAILGUN_SECRET=MY_API_SECRET_KEY

[email protected]
MAIL_FROM_NAME="${APP_NAME}"

While in the config/mail.php

'mailgun' => [
            'transport' => 'mailgun',
            'domain' => env('MAILGUN_DOMAIN'),
            'secret' => env('MAILGUN_SECRET'),
            'from' => [
                'address' => env('MAIL_FROM_ADDRESS'),
                'name' => env('MAIL_FROM_NAME'),
            ],
        ],

Am I missing something?

Thank you, Mihail

0 likes
12 replies
tnort's avatar
Level 4

@Sinnbeck just looked at it. Have also installed the Symfony's Mailgun Mailer transport. And updated the MAILGUN_DOMAIN=MY_DOMAIN as shown on Mailgun but still not working.

tnort's avatar
Level 4

I've created a new route that basically does what is described in the documentation, sends an email using PHP.

// First, instantiate the SDK with your API credentials
$mg = Mailgun::create('key-example'); // For US servers
$mg = Mailgun::create('key-example', 'https://api.eu.mailgun.net'); // For EU servers

// Now, compose and send your message.
// $mg->messages()->send($domain, $params);
$mg->messages()->send('example.com', [
  'from'    => '[email protected]',
  'to'      => '[email protected]',
  'subject' => 'The PHP SDK is awesome!',
  'text'    => 'It is so simple to send a message.'
]);

And apparently, it worked. Now my guess is that I have to update all the instances of Mailable with a Mailgun instance instead?

Sinnbeck's avatar

@tudosm No if configured correctly it should just work. What did you configure it to currently?

1 like
tnort's avatar
Level 4

@Sinnbeck,

Basically, my .env looks like this

MAIL_MAILER=mailgun
MAILGUN_DOMAIN=sandbox######.mailgun.org
MAILGUN_SECRET=API_PRIVATE_KEY
MAILGUN_ENDPOINT=api.mailgun.net

while my config/services.php looks like this:

'mailgun' => [
        'domain' => env('MAILGUN_DOMAIN'),
        'secret' => env('MAILGUN_SECRET'),
        'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'),
    ],

and finally my config/mail.php

'mailgun' => [
            'transport' => 'mailgun',
            'domain' => env('MAILGUN_DOMAIN'),
            'secret' => env('MAILGUN_SECRET'),
        ],
tnort's avatar
Level 4

@Sinnbeck I managed to send emails via the test route using Mailgun but I am unable to send via Mail Facade. Now I get "resulted in a 401 Unauthorized response: Forbidden "

Sinnbeck's avatar

@tudosm I will see if I can test it myself later. I am currently just using smtp via mailgun

1 like
tnort's avatar
tnort
OP
Best Answer
Level 4

@Sinnbeck, I figured out what was the issue. Basically in the MailgunTransport class, there is a private $endpoint which if not passed when called it will be set to null and when null it takes api.mailgun.net as default. Since I am in the EU I needed the default to be api.eu.mailgun.net. Which fixed the issue.

tnort's avatar
Level 4

But now the problem is I don't receive back a response (id, message). Which I receive when using Mailgun class.

Sinnbeck's avatar

@tudosm if you decide to just use smtp I can share how I handle getting the id

1 like
Sinnbeck's avatar

@tudosm Sure. I have an event listener for when a message is sent.

    protected $listen = [
        \Illuminate\Mail\Events\MessageSent::class => [
            \App\Listeners\LogSentMail::class,
        ],
    ];

And in my mailable, I attach the Mail model to it, so I can add the message id to it here (probably only should send in the ID, but thats a refactor for another day)

<?php

namespace App\Listeners;

use App\Models\Email;

class LogSentMail
{
    /**
     * Handle the event.
     *
     * @param object $event
     *
     * @return void
     */
    public function handle($event)
    {
        //return;
        if ((isset($event->data['email'])) && $event->data['email'] instanceof Email) {
            /** @var \Illuminate\Mail\SentMessage $message */
            $message = $event->sent;
            $messageId = $message->getSymfonySentMessage()->getMessageId();
            $event->data['email']->update([
                'message_id' => $messageId,
            ]);

        }
    }
}

Please or to participate in this conversation.