tmonq
tmonq

Reputation: 21

Laravel 5.2 mailgun error bad request

I'm currently working on some app based on Laravel 5.2. I need to send email with reset password link to the user and I'm using mailgun. Everything is working fine when I send email to the mail on which mailgun is registered. But it fails when it is any other email address.

Error says:

ClientException in RequestException.php line 71: Client error response [url] https://api.mailgun.net/v3/sandbox*******************************.mailgun.org/messages.mime [status code] 400 [reason phrase] BAD REQUEST

I've tried many solutions but none of them works. Any idea why it happens? Maybe is it caused by localhost? I am using:

Controller function:

public function sendEmail() {
    $sent = Mail::send('mails.test', [], function ($message) {
        $message->to('[email protected]','somename');
        $message->subject("Activate Your Account");
        $message->getSwiftMessage();
    });
    if ($sent === 0) {
        return redirect('/')
            ->withErrors('Failed to send activation email.');
    }
    return redirect('/')
        ->withSuccess('Mail was sent.');
}

env data:

MAIL_DRIVER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=postmaster@sandbox*****************************.mailgun.org
MAIL_PASSWORD=**********************
MAIL_ENCRYPTION=null

MAILGUN_DOMAIN=sandbox*****************************.mailgun.org
MAILGUN_SECRET=key-*****************************

File: conf/mail.php is configured as .env data

Route:

Route::get('auth/send','AuthController@sendEmail');

Button:

<a href="{{action('AuthController@sendEmail')}}" class="btn btn-danger">Test mail</a>

Upvotes: 2

Views: 536

Answers (1)

Sid
Sid

Reputation: 5833

Try removing MAIL_HOST=smtp.mailgun.org from the .env and give a try. I faced the same situation in the past and removing MAIL_HOST worked for me. New .env configuration would look like

MAIL_DRIVER=mailgun
MAIL_PORT=587
MAIL_USERNAME=postmaster@sandbox*****************************.mailgun.org
MAIL_PASSWORD=**********************
MAIL_ENCRYPTION=null

MAILGUN_DOMAIN=sandbox*****************************.mailgun.org
MAILGUN_SECRET=key-*****************************

after this, run php artisan config:cache and php artisan config:clear from the command line and check whether mail works or not.

Upvotes: 0

Related Questions