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

thanhmabo's avatar

Mailgun & L 5.1 get a client 400 error

hello everybody, today i try to test mailgun laravel on my hosting and i get 1 error

ClientException in Middleware.php line 69: 
Client error: 400

This is first time i using mailgun, before i used smtp gmail and send okey

Anyone can help us find the cause.

My configs:

.env

MAIL_DRIVER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=tls

services.php

return [
    'mailgun' => [
        'domain' => 'sandboxe################.mailgun.org',
        'secret' => 'key-################',
    ],
    ...............
]   

mail.php

return [
    'driver' => env('MAIL_DRIVER', 'smtp'),
    'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
    'port' => env('MAIL_PORT', 587),
    'from' => ['address' => '######@gmail.com', 'name' => 'Admin'],
    'encryption' => env('MAIL_ENCRYPTION', 'tls'),
    'username' => env('MAIL_USERNAME'),
    'password' => env('MAIL_PASSWORD'),
    'sendmail' => '/usr/sbin/sendmail -bs',
    'pretend' => false,
];

routes.php

Route::get('sendemail', function () {

    Mail::send('emails.master', [], function ($message) {
        $message->to('#######@gmail.com')->subject('Learning Laravel test email');
    });
    return "Your email has been sent successfully";

});
0 likes
3 replies
bobbybouwmann's avatar

Take a look at your .env variables, you need to set a username and password provided by mailgun.

corydemille's avatar

I am getting this same error, my configs are similar and I have the .env username and password for mailgun set.

ClientException in Middleware.php line 69:
Client error: 400
corydemille's avatar

I figured out my problem. I just signed up for the Mailgun account, and I failed to verify the email address on the account. Facepalm

Thanks.

1 like

Please or to participate in this conversation.