laravel with mailgun set up - laravel

I read everything I can
and looks like I set it up and fill in everywhere according to the docs (and internet) but still cant receive any emails.
I need to use MailGun API so I can send emails from my localhost as well as from the test and prod + because 25 port usually closed.
I filled in everything I can just in case some miracle happen (was changing between smtp/mailgun in .env)
I have tried both my real server settings and when I fail tried without any success sandbox as well.
my mailgun data access (some letters hidden):
my mailgun sandbox server
http://i.imgur.com/XCi8Hkn.png
when i click to it i have an option to choose between API and SMTP
2.1 in API tab i have API KEY: http://i.imgur.com/xGSq7aF.png
2.2 in SMTP box i have smtp host, port, login, passwd: http://i.imgur.com/t5Pi6lH.png
my .env:
MAIL_DRIVER=mailgun
MAILGUN_DOMAIN=sandbox8ffe89553e224c468f4ad0cf6b4da3c2.mailgun.org
MAILGUN_SECRET=b3ff30bxxxxxxxxxxxxxxxx087d-f877bd7a-01ecceaa
somewhere in internet found that MAILGUN_DOMAIN & MAILGUN_SECRET
could be added in .env so tried them here as well
config/mail.php
'driver' => env('MAIL_DRIVER', 'mailgun'),
app/mail/Test.php
public function build()
{
return $this->from('support#sandbox8ffe89553e224c468f4ad0cf6b4da3c2.mailgun.org')
->subject('HELLO')
->view('test');
}
sandbox "to" email verified http://i.imgur.com/tDhgLcT.png
TestController
use App\Mail\Test;
use Illuminate\Support\Facades\Mail;
class TestController extends Controller
{
public function test()
{
echo 'hello';
try {
Mail::to('myaddress#gmail.com')->send(new Test());
} catch (\Exception $e) {
dump($e);
}
echo 'hello2';
}
}
resources/views/test.blade.php
hello
was using even so just in case
> artisan cache:clear
> artisan config:cache
> artisan cache:clear
9
please help, what is missing ? what i did wrong ?

The _HOST, _PORT, _USERNAME, _PASSWORD and _ENCRYPTION environment variables apply to SMTP only.
For that, your MAIL_DRIVER should be set to SMTP, with values for the five I just mentioned pointing to your mailgun values.
The _DOMAIN and _SECRET apply only to when you have set MAIL_DRIVER=mailgun (which means the mailgun api), and that means you are not using SMTP driver anymore.
Basically you have a choice: pick SMTP and point it to mailgun (set MAIL_DRIVER=smtp, OR mailgun API (set MAIL_DRIVER=mailgun).
I think if you think carefully till you understand the difference between those two, then you should be able to work it out.
Also, on the sandbox domain, you can't send real email unless you add the recipient address as a verified email. You do that on the mailgun dashboard. That might be what is causing the problem here (it is a sandbox after all - it's meant to be safe so you can't accidentally send to live email addresses you haven't specifically opted in with via an email verification by the specific address).
Another tip, go in to tinker and type config('mail') and you'll see what is getting picked up by the framework. Then you won't have to adjust the actual config files, if you see the values you want to put there are definitely there.
I also just noticed your config file you listed above is wrong. You have 'driver' => env('mailgun', 'mailgun'), That won't work (as you'll see if you do config('mail') in tinker). the env() function takes the name of an environment variable (like MAILGUN_DOMAIN or MAILGUN_SECRET as its first param, the second param is a default value if the shell environment variable of that name doesn't exist).

You need to use the name of your ENV setting. In your case you are for example for the username trying to get an env variable named 'postmaster#sandbox8ffexxxxxxxxxxd0cf6b4da3c2.mailgun.org' that of course does not exist.
'driver' => env('MAIL_DRIVER', 'mailgun'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => [
'address' => env('SUPPORT', 'hello#example.com'), //if SUPPORT env does not exist, default to hello#example.com
'name' => env('NAME', 'Example'),
],
'username' => env('MAIL_USERNAME'),
'password' => env('PASSWORD'),
when using env , first parameter ie. ENV_NAME is the name you have set in your .env file and second (optional) parameter is the value to which it defaults if the first value is not found.

Related

Laravel 8 sending mailgun via api Client error: Unauthorized response: Forbidden

I recently upgraded my Laravel from 5.7.29 to 8.51. I have been using the Mailgun API to send emails for years. The old version of the site is still able to send via mailgun, but the new version keeps returning this message:
Client error: `POST https://api.mailgun.net/v3/mg.clstracking.com/messages.mime` resulted in a `401 Unauthorized` response: Forbidden
I found similar posts that indicate that kind of response if you're working in the EU and fail to change the MAILGUN_ENDPOINT in the services config. The server and site are all in the USA. I have verified I have the correct settings in my .env and that those are passed into my services.php config file and cached using artisan config:cache. I verified this by looking through the /bootstrap/cache/config.php and everything is there - I even checked this against another site with a different domain that is working the same way. I even tried hard-coding my domain and secret into the services.php.
If I change
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
with my username and password, I get no error, but the email isn't sent and checking the logs on mailgun, there is no record. I'm at a loss what else to try.
In env I have:
MAIL_MAILER=mailgun
MAILGUN_DOMAIN=mg.mydomain.com
MAILGUN_SECRET=key-##########################
In config/services.php:
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'),
],
In config/mail.php:
'default' => env('MAIL_MAILER', 'mailgun'),
'mailers' => [
'mailgun' => [
'transport' => 'mailgun',
],
],
I use this config in my projects which are in production with gmail
MAIL_MAILER='sendmail'
MAIL_HOST='sendmail.googlemail.com'
MAIL_PORT=587
MAIL_USERNAME='email#gmail.com'
MAIL_PASSWORD="*********"
MAIL_ENCRYPTION='tls'
MAIL_FROM_ADDRESS='email#gmail.com'
MAIL_FROM_NAME="${APP_NAME}"
Maybe helps you.

SOLUTION for: Expected response code 250 but got code "530", with message "530 5.71 Authentication required

This first code was what solved everything. Use this code exactly inside your config/mail.php file. With your own full Gmail address and Google app password.
Where I got the solution
'username' => env('MAIL_USERNAME','full-gmail-address-here'),
'password' => env('MAIL_PASSWORD','Google-app-password-generated-when-setting-up-2-factor-auth'),
You might have written this 2nd code but here it is ... Goes inside your env file.
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587 (or use 465 for ssl on the sixth line below)
MAIL_USERNAME=Your-full-Gmail-address-here
MAIL_PASSWORD=Your-Google-App-Password-here
MAIL_ENCRYPTION=tls (or ssl)
MAIL_AUTH=YES (<= I made this up, if you're a pro you can check if it's necessary)
MAIL_FROM_ADDRESS=Email-address-of-sender
MAIL_FROM_NAME=Name-of-sender
Thirdly, don't forget to include this inside the config/mail.php file
'stream' => [
'ssl' => [
'allow_self_signed' => true,
'verify_peer' => false,
'verify_peer_name' => false,
],
],
Please pass it on to whoever may need it. Thanks
When change in .env you just clear cache & config
php artisan cache:clear
php artisan config:clear
Then test your .env data
php artisan tinker
config('mail')
check your data is correct or not.Your process is correct here.

Emails keep being sent from Example <hello#example.com>

Already read a lot and even did php artisan config:clear
Checked thourghout all files and hello#example.com is nowhere.
It works from local w/o problems.
Also, have another app in plain PHP that uses PHPMailer and this issue does not happen so I don't think it would be something related to the server.
Any help appreciated hence this script is only waiting this issue to be solved so it can be live.
UPDATE: The issue was with the mail server. We changed the mail server
to a different machine but didn't configure completely the DNS.
Thanks to both for answering.
Email address is configured in config/mail.php file
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello#example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
but how you can see it is using environment vars configured in .env in root directory
MAIL_FROM_ADDRESS=info#blabla.com.ar
MAIL_FROM_NAME="San Marino Pastas"
Remeber if you are using artisan serve or queue listining restart the services to take the new config.
Please try this and let me know how it works :)
1)First of all you have to set the environment variable in .env file
MAIL_DRIVER=mail
MAIL_HOST=mail.abc.com
MAIL_PORT=465
MAIL_USERNAME=info#abc.com
MAIL_PASSWORD=abc123
MAIL_ENCRYPTION=null
2) Then import this in your controller,
use Illuminate\Support\Facades\Mail;
3) Then add this function in your controller,
public function basic_email() {
$data = array('name'=>"Virat Gandhi");
Mail::send(['text'=>'mail'], $data, function($message) {
$message->from('info#abc.com');
$message->subject('Testing');
$message->to('abc#gmail.com');
echo "Basic Email Sent. Check your inbox.";
});
4) I hope this helped you.

Laravel keeps using hello#example.com with changed conf

In mail.php I have changed from fields but I still get mails with demo values. There is no reference to hello#example.com in project, yet I still get this in mail.
'from' => [
'address' => 'do-not-reply#mysite.net',
'name' => 'MySite.net',
],
Does anyone have an idea what might be wrong?
Clear the config cache by running the following Artisan command:
php artisan config:clear
If you are using supervisor, ensure you set the right user in the conf file, otherwise you will see the emails are being sent as hello#example.com.
In my case the user is www-data - I set it up as root before and was getting this issue.

laravel 5 unable to migrate

I know this question has been and answered before, but none of the answers apply in my situation. Essentially, I have a Laravel install. I try to run
php artisan migrate
I get the following error:
Access denied for user 'homestead'#'localhost'
Now here's the deal. I am not using homestead. It's a standard LAMP install and I have changed the database settings in config/database.php and in the .env file to reflect the correct username and password for my database. I can find no logical reason for artisan to still be attempting to use the homestead user. My APP_ENV is set to local and there is no production folder in either the app or config folders. I'm at a loss as to where it is pulling the homestead user.
ETA:
I have restarted apache, and run both
php artisan config:clear
and
php artisan cache:clear
It's still pulling the homestead user from something, but I simply cannot figure out where.
This is a good task to practice debugging skills! :)
So, it's obvious that Laravel gets 'homestead' username from somewhere – which could be a config file, a default value in the code (in reality this is unlikely but we are debugging), etc.
So my first step would be to execute this while in the base folder of your Laravel app:
find . -type f -size -30k -exec grep -H "homestead" \{\} \;
Which would look for files smaller than 30 kilobytes containing 'homestead' anywhere inside, and in case if found – show filename and the line containing this pattern. We can safely assume that 'homestead' is not stored anywhere in the database because it's used to access the database, so it's necessary beforehand.
Once you find the place, fixing the bug may be as easy as editing the file!
If find returns no results, there are still possibilities:
If your Linux user is 'homestead' and you try to connect to MySQL with no username specified, 'homestead' will be used automatically. Which would mean that Laravel for some reason cannot access your configuration files (permissions? wrong path?)
Check storage/logs/laravel.log and see the last error in more details. Go through the code file by file (list of files is given in the error, start from the top) and see how it fetches MySQL username.
I truly have no idea what happened with that particular build, but I am closing this question just to be done with it. I trashed the build and started over without any problem whatsoever. What happened there will forever remain a mystery.
You can have multiple database.php config files. The config file that is loaded is dependent upon the context in which the application is executed.
For example, I have three different database.php config files in my project:
app/config/database.php
app/config/local/database.php
app/config/testing/database.php
In my "testing" config file, I have two different connections defined:
return [
'default' => 'sqlite',
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'test',
'username' => 'user',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'sqlite' => array(
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => ''
),
)
];
Notice that the default key references sqlite, so it does not matter what credentials I enter into the mysql definition. (Unless I override the default value at runtime using Config::set('database.default', 'mysql'))
Search your config/ directory for the string 'homestead'. You'll find the config file that your application is loading. Change the default parameter to match the connection that you want to use.

Resources