3

On local server, I can send mail with mailgun easily....
Everything works....
But, after taking that Laravel app to a Web Server, mailing system doesn't work anymore. This error shows up.

My .env File

APP_ENV=production

APP_KEY=base64:8K9zZjAKdpoBIpMeW53K24FxrKi38dX/EnUe9+5cMKk=
APP_DEBUG=true APP_LOG_LEVEL=debug     
APP_URL=http://pathofmywebsite.com

DB_CONNECTION=mysql 
DB_HOST=localhost 
DB_PORT=3306 
DB_DATABASE=database
DB_USERNAME=username
DB_PASSWORD=password

BROADCAST_DRIVER=log 
CACHE_DRIVER=file 
SESSION_DRIVER=file 
QUEUE_DRIVER=sync    
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null 
REDIS_PORT=6379

MAIL_DRIVER=mailgun 
MAIL_HOST= 
MAIL_PORT= 
MAIL_USERNAME= 
MAIL_PASSWORD= 
MAIL_ENCRYPTION=tls

MAILGUN_DOMAIN=my.mailgundomain.com
MAILGUN_SECRET=key-9cebd121c16c037597c004c67fd9d0a3

PUSHER_APP_ID= 
PUSHER_KEY= 
PUSHER_SECRET=

In my Controller-

function send(Request $request) {
    $value = 'value';
    Mail::send('path.to.mail.template', ['value' => $value], function ($message) use ($request)
    {
        $message->from('[email protected]', 'JakariaBlaine');
        $message->to($request->email);
    });
}
4
  • "Could not resolve host" means that your webserver can't find mailgun's server. If you have shell access to the server, see if you can ping it, or try nslookup api.mailgun.net. Commented Jan 9, 2017 at 20:07
  • @aynber i dont have any shell access on my server.... is there anyway to fix this except using shell? Commented Jan 9, 2017 at 20:09
  • You might need to talk to your webhost. My suggestion were just some simple troubleshooting, not fixing the issue. They can investigate the issue better or provide an answer. Commented Jan 9, 2017 at 20:11
  • 1
    Restart the server. After two hours that was what worked for me. Commented Mar 21, 2017 at 18:15

2 Answers 2

2

This error is usually due to server issue. Restarting your apache may fix your problem. In my case, the digital ocean was an issue as my server was down unexpectedly. This sends email from local system as your server is up and running properly.

Sign up to request clarification or add additional context in comments.

Comments

1

Other points that are important to make sure this works...

Ensure all the following are defined in your .env

MAIL_FROM_NAME="Company Name"
[email protected]
MAILGUN_DOMAIN=mg.company.com
MAILGUN_SECRET="xxxxxxxxxxxxxxxxxxxxxxxxxx-yyyyyyyy-zzzzzzz"
MAILGUN_ENDPOINT="api.eu.mailgun.net"

Note that if as MailGun recommend you setup a subdomain to work with their system (ie mg.yourcompany.co), THAT is what you need to define as the Mailgun domain variable, even if you actually won't send emails as having come from the subdomain.

Also something vital that may well be the cause of your issue; ensure that you do NOT configure the endpoint as the url that the MailGun website provides. In the MailGun site they will refer to their 'Base API Url' as something like:

https://api.mailgun.net/v3/mg.company.com (where your Mailgun defined domain is suffixed to the end of their api url)

Laravel however is compiling the API url itself from the endpoint and domain variables that you define. If you were to define your endpoint as the url they provide you would end up with a totally invalid url that will cause Guzzle to error.

Make sure you are using the right endpoint. If you defined your domain within mailgun as inside the EU make sure that is defined properly in the endpoint setting.

If still problems, ensure that current Guzzle is installed to your project by running

composer require guzzlehttp/guzzle

Even though I had this error and guzzle was working at some level, it wasn't actually defined within my composer.json file and up to date.

Also I also clarified the settings to the config/services.php file and made sure they were correct there. Note that the file on my personal install came with syntax different to the Laravel manual. As default they were written as:

'mailgun' => [
    'domain' => env('MAILGUN_DOMAIN'),
    'secret' => env('MAILGUN_SECRET'),
    'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'),
],

But I rewrote removing the ENV() to format as follows:

'mailgun' => [
    'domain' => 'MAILGUN_DOMAIN',
    'secret' => 'MAILGUN_SECRET',
    'endpoint' => 'api.eu.mailgun.net',
],

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.