Laravel 5.1 GuzzleHttp Client error: 404 when using mailgun - php

I'm using the Mailgun service when sending mail with Laravel. However, i've set this up today and it's just stopped working. I have entered all the correct info in .env, config/services.php and config/mail.php. However i'm still getting the below error:
ClientException in Middleware.php line 69:
Client error: 404
It looks like the domain is not getting passed through somehow, even though in my config/services.php file I have:
'mailgun' => [
'domain' => env('mydomain.com'),
'secret' => env('<my-mailgun-key>'),
],
I have hidden the above credentials for safety, but in my real application they are the proper values.
Please help.

I was having a very similar 404 issue and tried the solution mentioned by Rogério. I thought I was doing it right, but gave it a try anyway. But misuse of the env() function wasn't my problem.
I set the config/services.php back to look like so:
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN',''),
'secret' => env('MAILGUN_SECRET',''),
],
This will provide empty strings if the values named MAILGUN_DOMAIN and MAILGUN_SECRET are not found in the .env file. Then, in my .env file, I included the API Base URL and API Key from the Mailgun domain information page. So the .env looked something like this:
MAILGUN_DOMAIN=https://api.mailgun.net/v3/sandbox123abc.mailgun.org
MAILGUN_SECRET=key-123456abcdef
The values were passing along as they should, but still 404. Looking at How do I start sending email documentation at Mailgun, I saw that their API URL included "messages" on the end - which I tried manually adding to the .env setting:
MAILGUN_DOMAIN=https://api.mailgun.net/v3/sandbox123abc.mailgun.org/messages
That didn't work either, but lead me to look more carefully at the stack trace spit out by Laravel. That's when I noticed that the URL it was trying to connect with was:
https://api.mailgun.net/v3https://api.mailgun.net/v3/sandbox123abc.mailgun.org/messages/messages.mime
Ah-ha! Using Mailgun's Base API URL was incorrect! That was made obvious by the repetition of the "https://api.mailgun.net/v3" portion. So the aptly named MAILGUN_DOMAIN setting really just needed to be:
MAILGUN_DOMAIN=sandbox123abc.mailgun.org
Seems obvious now, but I spent way too much time figuring it out. Thought I'd put it out there in case anyone else happened to miss that detail

I hade the same problem as you and solved it by removing the env() call.
Thats because env will return the value of the env variable in the first argument (not the value of the argument) and otherwise return the second argument.
So:
'mailgun' => [
'domain' => 'mydomain.com',
'secret' => '<my-mailgun-key>',
],
Try that.

Related

Laravel not sending email to environment variable

I am using the following working code in Laravel:
Mail::to('mail#example.com')->send(new \App\Mail\CheckinInfo([
'client' => $client,
]));
when I use an email address from the environment, it stopped working:
Mail::to(env('MAIL_FROM_ADDRESS'))->send(new \App\Mail\CheckinInfo([
'client' => $client,
]));
In .env I got:
MAIL_FROM_ADDRESS=mail#example.com
and I tried
MAIL_FROM_ADDRESS="mail#example.com"
The cache and config are cleared.
The error message is:
[2022-07-29 18:44:26] production.ERROR: An email must have a "To", "Cc", or "Bcc" header. {"exception":"[object] (Symfony\\Component\\Mime\\Exception\\LogicException(code: 0): An email must have a \"To\", \"Cc\", or \"Bcc\" header. at /www/htdocs/app/laravel/vendor/symfony/mime/Message.php:128)
As a note when you do a config:cache the settings will not be grabbed from your .env anymore.
You should create a config/settings.php or similar file and store your env vars there, ie:
config/settings.php
return [
'mail_from_address' => env('MAIL_FROM_ADDRESS'),
]
and you should reference it as so
config('settings.mail_from_address')
No your mistake on the first line, maybe you didn't close the bracket
Try this way
Mail::to(env('MAIL_FROM_ADDRESS'))->send(new \App\Mail\CheckinInfo([
'client' => $client,
])); //Notice I added the closing parenthesis for To()
You are trying to access the functions of the env methods. This is not possible
I hope this is just the problem
You can see the documentation for the framework,
about this queueing-mail laravel documentation

"Class 'Nexmo\\Laravel\\Facades\\Nexmo' not found" Error

I am trying to send an sms once a user signs up but it keep getting:
"Class 'Nexmo\\Laravel\\Facades\\Nexmo' not found"
I have this at the top of my controller file:
use Nexmo\Laravel\Facades\Nexmo;
I also have this in my config/app.php file
'Nexmo' => Nexmo\Laravel\Facades\Nexmo::class,
Im still getting an error, does anyone or has the same problem. I know its a class type error but why if I have added the right class and im using it appropriately.
Also, here is my code implementation:
Nexmo::message()->send([
'to' => '1122334455', //not actually using this number
'from' => 'Test',
'text' => 'Hey, test this digit code',
'text' => $request->user()->activation_token
]);
Update 1:
I have done php artisan vendor:publish and published the nexmo config file but it sill gives the error above.
Add Nexmo\Laravel\NexmoServiceProvider to the providers array in your config/app.php:
'providers' => [
// Other service providers...
Nexmo\Laravel\NexmoServiceProvider::class,
],
Thanks guys for answers/suggestions
Some reason it is working now? I did all the suggestions/answers and it did not work, did some other stuff (not working on this) and it suddenly works?
Thanks guys :)
If I found out what I did to make it work I will update this answer.
Found the answer:
Found out when I run php artisan vendor:publish it gives a list to publish or something and i'm guessing I did not do the one that specifically publishes Nexmo. Still should've worked though because I did the one that will publish everything. Anyways that published the Nexmo file in config folder. Somehow that is what made everything else work, that and probably a combination of answer/suggestions
Anyways thanks guys for your help!!
First of all, the Nexmo documentation did not mention about to use with Laravel but there are as a additional package you need to use if you used with Laravel.
There are are a simple solution.You need to install a additional package that already provide by Nextmo to use with Laravel.Just install below one.I hope this will help u.
composer require nexmo/laravel
try it
$nexmo = app('Nexmo\Client');
$nexmo->message()->send([
'to' => 'yournumber',
'from' => 'yournumber',
'text' => "message"
]);

Laravel googlemapper package, Cannot get variables from the config file, "Google maps API key is required." ERROR

I was using your plugin these days trying to implement a googlemap on my site. I followed all installation steps and set a variety of variables. I am on laravel 5.4.
I stuck in this error: "Google maps API key is required.". Even I try to hard set the API in your core function, then it throws another error says "region is required". seems like the config file cannot be reached. I also tried to fix the file permissions, but unfortunately that's not the issue. Do you have any ideas about this? This is part of my controller file
use Cornford\Googlmapper\Mapper;
use Cornford\Googlmapper\Facades\MapperFacade;
use Illuminate\Support\Facades\Route;
public function index(Mapper $mapper)
{
............
$firstMapItem = reset($mapArrays);
$mapper->map(
$firstMapItem->latitude, $firstMapItem->longitude,
[
'zoom' => 10,
'markers' => ['title' => 'My Location', 'animation' => 'DROP'],
'clusters' => ['size' => 10, 'center' => true, 'zoom' => 20]
]
);
// Add information window for each address
foreach ($mapArrays as $mapArray) {
$mapper->marker($mapArray->latitude, $mapArray->longitude);
}
}
Route
Route::get( '/map', 'MapController#index');
view
<div style="width: 100%; height: auto;">
{!! Mapper::render() !!}
</div>
BTW, I already changed the API value there, my problem is this API value has not been referenced. But if I move the Mapper function from the controller to the Route file, everything is fine. But if I do what I did as above, the API value from the googlmapper.php file isn't referenced.
Finally, make it works. Not because of the API issue, the API has been correctly placed.
change
use Cornford\Googlmapper\Mapper;
to
use Mapper;
Very simply Facades things I think.
credit to #zubinkadva on GitHub. Also the package author #bradcornford also mentioned
"however for reference you can also use the following:
use Cornford\Googlmapper\Facades\MapperFacade as Mapper;
"
From the package docs:
You also need to set your Google API Key into the GOOGLE_API_KEY environment variable.
So, get the API key first here.
Then add this line in the .env file in Laravel project root:
GOOGLE_API_KEY=your_real_api_key_here
As Docs said you have to add this
GOOGLE_API_KEY="your google api key here"
in your .env file in the root of your project to get this key you have to go in Google Console

Laravel 5.1 Send mail to all users in foreach [GuzzleHttp\Exception\ClientException] Client error: 400

I'm trying to send an email to all the users in the database, but I'm getting the following error: [GuzzleHttp\Exception\ClientException] Client error: 400.
Here's my code:
$users = App\User::all();
foreach($users as $user) {
$code = new App\Code();
$code->code = str_random(10);
$code->save();
Mail::send('emails.code', ['code' => $code], function($message) use ($user)
{
$message->to($user->email)->from('foo.bar#gmail.com', 'Foo Bar')->subject('New code, new chances!');
});
}
[GuzzleHttp\Exception\ClientException] Client error: 400
is for URL not found
Check for host url in config/mail.php, host url might be wrong
Not sure if you have got it working but I had the exact same problem today and I had no idea what went wrong as I did the exact same implementation just in my previous Laravel project with mailgun. Then I found the issue in the configuration.
So, here is the glitch..I don't know in which version it was changed, but in config/services.php the mailgun config is now like this,
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
],
where it was like this before as far as I can remember,
'mailgun' => [
'domain' => env('MAIL_DOMAIN'),
'secret' => env('MAIL_SECRET'),
],
so in my .env file, I was only referencing MAIL_DOMAIN and MAIL_SECRET environment variables but I just realised in the services.php file, it's actually referencing different environment variables - MAILGUN_DOMAIN and MAILGUN_SECRET.
So I've just added these environment variables, cleared the config cache and it is working perfectly now.
(I am working with Laravel 5.1.23 version BTW)
Hope this helps. Thanks.

Testing Yii REST application using a second database

I wrote an API using Yii2 and following the REST guide. My API is working and I want to write some tests for it, so I once again followed the guide on how to run tests and got unit tests working. I then looked around Codeception documentation about testing WebServices and got this working too.
My problem is that API calls are not using my test database. I have two databases, one called db and the other testdb. Here is my config.php file in tests/codeception/config/:
return [
'components' => [
'db' => [
'dsn' => 'mysql:host=localhost;port=8889;dbname=testdb;unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock',
],
'mailer' => [
'useFileTransport' => true,
],
'urlManager' => [
'showScriptName' => true,
],
],
];
I wrote a simple test that send a GET request to an endpoint to retrieve data. My test database is empty so I am expecting to receive an empty response, but I get the content of my other database instead.
I then tried to set YII_ENV to test as described in the Environment Constant section here so that I could test against the env variable YII_ENV_TEST and change the db configuration accordingly. I tried to set this variable in the _bootstrap.php file in the tests/codeception/ folder:
defined('YII_ENV') or define('YII_ENV', 'test');
I then logged the value of YII_ENV in the web/index.php file (index-test.php is not called, might be a problem too), and it is undefined.
What am I doing wrong? I tried including the Yii2 module in my api.suite.yml file but requests don't have return code anymore if I do that, it returns N/A. Is there another way to change which database Yii should use?
You can make an test_config.php file and at the end of the config place this
if (file_exists('protected/config/test_config.php'))
{
include 'test_config.php';
}
the file will be included if it exists. And the file test_config.php should contain the overwritten value for the db connection.
Hope this helps!
Keep on coding!
Ares.
Well I found a "solution" by using this other app template: https://github.com/githubjeka/yii2-rest
The file organization fits my needs better and I can easily configure which database to use.

Categories