Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending Email with mailgun in laravel error

Hello i've been simply trying to send an email in laravel i read the documentation and they made it seem so easy but whenever i try i keep getting error after error, i tried sendgrid didn't work and now i'm trying to use mailgun but i'm having issues with it as well.

This is my code::

$data = array();

        Mail::send('emails.auth.activate', $data, function($message)
        {
            $message->to('[email protected]', 'John Doe')->subject('This is a demo!');
        });

This is the error i get:

GuzzleHttp \ Exception \ ClientException (400)

Client error response [url] https://api.mailgun.net/v2/mail.xxxxxxx.com/messages.mime [status code] 400 [reason phrase] BAD REQUEST 

Mail Config:

<?php

return array(

    /*
    |--------------------------------------------------------------------------
    | Mail Driver
    |--------------------------------------------------------------------------
    |
    | Laravel supports both SMTP and PHP's "mail" function as drivers for the
    | sending of e-mail. You may specify which one you're using throughout
    | your application here. By default, Laravel is setup for SMTP mail.
    |
    | Supported: "smtp", "mail", "sendmail", "mailgun", "mandrill", "log"
    |
    */

    'driver' => 'mailgun',

    'host' => 'sandboxXXXXXXXXXXXXXXXXXXXXXXXXXXX.mailgun.org',

    'port' => 587,

    'from' => array('address' => '[email protected]', 'name' => 'Xxxxxxxx'),

    'encryption' => 'tls',

    'username' => '[email protected]',

    'password' => 'xxxxxxxxxxx',

    'sendmail' => '/usr/sbin/sendmail -bs',

    'pretend' => true,

);
like image 658
user3718908x100 Avatar asked Feb 02 '15 20:02

user3718908x100


3 Answers

Follow these steps

  1. First of all, install guzzle package by adding "guzzlehttp/guzzle": "~4.0" line inside composer.json
  2. Update composer using composer update
  3. Create your account on mailgun from http://www.mailgun.com/. After creating account, kindly note the mail gun subdomain created like sandboxXXXXXXXXXXXXXXXXXXXXXXXXXXXX.mailgun.org and API key created like key-65c33f1xxxxxxxxxxxxxxxxxxxx
  4. Go to config/services.php file and replace

    'mailgun' => array(
        'domain' => '',
        'secret' => '',
    ),
    

    with

    'mailgun' => array(
        'domain' => 'sandboxXXXXXXXXXXXXXXXXXXXXXXXXXXXX.mailgun.org',
        'secret' => 'key-65c33f1xxxxxxxxxxxxxxxxxxxx',
    ),
    

    If you want to create your own sub-domain, you can create and assign to domain (as an alternative)

  5. Configure config/mail.php like this

    'driver' => 'mailgun',
    'host' => 'smtp.mailgun.org',
    'port' => 587,
    'from' => array('address' => '[email protected]', 'name' => 'Xxxxxxxx'),
    'encryption' => 'tls',
    'username' => null,
    'password' => null,
    'sendmail' => '/usr/sbin/sendmail -bs',
    'pretend' => false
    

Note that you do not need to supply username and password for this. Mailgun will handle this.

Try sending email now. Hope this helps. Good luck!

like image 95
srbhattarai Avatar answered Oct 10 '22 01:10

srbhattarai


Just wanted to add one possible reason for the error. I received this error while using sandbox mode when I hadn't set the authorized recipient yet. When I logged into Mailgun and added the intended recipient to the "Authorized Recipient" list the error went away.

like image 41
MMMTroy Avatar answered Oct 10 '22 02:10

MMMTroy


I had the same issue and kept getting the following error: Client error response [url] https://api.mailgun.net/v3//messages.mime 404 not found

There isn't much written about this error written online for Laravel 5.1 which I'm using. It turns out that in the config->services the default Laravel 5.1 installation comes with :

'domain' => env('');

'secret' => env('');

for some reason if you keep your domain and secret wrapped in env as per default installation, the MailGunTransport doesnt pick it up. So just set it to the following:

domain' =>'yourdomain';

'secret' => 'yoursecret';

Hope this helps since I'm sure i'm not the only one who probably run into this issue.

like image 32
max234435 Avatar answered Oct 10 '22 03:10

max234435