Sending Email with mailgun in laravel error

▼魔方 西西 提交于 2019-11-27 02:50:17

问题


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('xxxxxxxxx@gmail.com', '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' => 'mail@xxxxxx.com', 'name' => 'Xxxxxxxx'),

    'encryption' => 'tls',

    'username' => 'xxxxxxx@gmail.com',

    'password' => 'xxxxxxxxxxx',

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

    'pretend' => true,

);

回答1:


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' => 'mail@xxxxxx.com', '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!




回答2:


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.




回答3:


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.




回答4:


I had this problem as I hadn't activated my mailgun account. Once activated all worked fine




回答5:


I also stucked once in configuring the Mailgun in Laravel 5.1 and what worked for me is the following process. Hope its helps someone:

1) Install guzzle package by adding "guzzlehttp/guzzle": "5.0" line inside composer.json like this:

"require": {
    "guzzlehttp/guzzle": "~5.0"
},

2) Update composer using sudo composer update in the terminal.

3) Restart Apache.

4) Create the account in http://www.mailgun.com. It will create a Sub Domain and API Key.

5) Add the Sub Domain and API Key in the .env like this:

MAILGUN_DOMAIN=sandbox8...........3b.mailgun.org
MAILGUN_SECRET=key-9..................04

6) And in the services.php add these line:

'mailgun' => [
    'domain' => env('MAILGUN_DOMAIN'),
    'secret' => env('MAILGUN_SECRET'),
],

7) Now the mail.php as follows:

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

I hope this works for you all.




回答6:


You will be able to send mail to the persons in the same domain unless you have added them in the authorised recipient category. If you add someone to authorised recipient category then he/she would have to approve the request and then only would be able to receive the emails.



来源:https://stackoverflow.com/questions/28286445/sending-email-with-mailgun-in-laravel-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!