Sending Email with mailgun in laravel error

user3718908 picture user3718908 · Feb 2, 2015 · Viewed 19.8k times · Source

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,

);

Answer

srbhattarai picture srbhattarai · Mar 30, 2015

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!