Mailgun emails not sending from Laravel 8 Valet app - php

Here's my relevant .env:
MAIL_MAILER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"
MAILGUN_DOMAIN=https://api.mailgun.net/v3/mail.example.com
MAILGUN_SECRET=fb...a1
Note: I use example.com as an example above, but I've put my actual domain name there. I do not get any errors from the Laravel app, and I do not see anything in the logs on the Mailgun dashboard. My domain is verified. fb...a1 is also the redacted API code, I of course use my full API code I get from the mailgun dashboard.
config/mail.php:
<?php
return [
'default' => env('MAIL_MAILER', 'mailgun'),
'mailers' => [
'mailgun' => [
'transport' => 'mailgun',
],
],
];
config/services.php:
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'),
],
In my controller I have:
$email = $validated['email']; // I've verified this is my actual email
Mail::to($email)->send(new OrderCreated());
app/Mail/OrderCreated.php:
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class OrderCreated extends Mailable
{
use Queueable, SerializesModels;
public function __construct()
{
//
}
public function build()
{
return $this
->from('no-reply#example.com')
->markdown('emails.order-created');
}
}
And finally, resources/views/emails/order-created.blade.php:
#component('mail::message')
# Order Confirmation
This email is test.
#endcomponent
I'm using Laravel 8.14.0 and Valet 2.13.0, so I'm testing it locally with an https://my-app.test domain. The app is using InertiaJS, in case that makes any difference. The controller code runs without error, but I see no logs on my mailgun dashboard and the email never arrives in my inbox. I have no idea what's wrong or how to debug this.
UPDATE:
I've noticed if I set MAILGUN_DOMAIN and MAILGUN_SECRET to null, I get the same behaviour as above. If I set MAILGUN_DOMAIN to a nonsense value like abcd I get the following error:
GuzzleHttp\Exception\ClientException
Client error: `POST https://api.mailgun.net/v3/abcd/messages.mime` resulted in a `401 UNAUTHORIZED` response: Forbidden
And if I set MAILGUN_SECRET to abcd it works as originally described (no error, but also no email).

These days I've worked with mailgun. It was on Laravel 7, and it worked perfectly. I dont think that it will cause a problem for v8.
Actually I've sent emails from controller, but you can implement the same for you as you want.
So I'll share my experience anyway.
.env
#MAIL_DRIVER=mailgun
MAIL_MAILER=mailgun
MAIL_HOST="smtp.mailgun.org"
MAIL_PORT=587
MAIL_USERNAME="postmaster#sandbox********************************.mailgun.org"
MAIL_PASSWORD="123456"
MAIL_ENCRYPTION=tls
MAILGUN_DOMAIN="sandbox********************************.mailgun.org"
MAILGUN_SECRET="key-********************************"
MAIL_FROM_NAME="ProjectName"
MAIL_FROM_ADDRESS="no-reply#yourfuturesite.com"
MAIL_ENV=test
MAIL_TEST="recipient#yourfuturesite.com"
#MAIL_LOG_CHANNEL
#MAILGUN_ENDPOINT="api.eu.mailgun.net"
config/mail.php
<?php
return [
'default' => env('MAIL_MAILER'),
'mailers' => [
'smtp' => [
'transport' => 'smtp',
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'timeout' => null,
'auth_mode' => null,
],
'ses' => [
'transport' => 'ses',
],
'mailgun' => [
'transport' => 'mailgun',
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'timeout' => null,
'auth_mode' => null,
],
'postmark' => [
'transport' => 'postmark',
],
'sendmail' => [
'transport' => 'sendmail',
'path' => '/usr/sbin/sendmail -bs',
],
'log' => [
'transport' => 'log',
'channel' => env('MAIL_LOG_CHANNEL'),
],
'array' => [
'transport' => 'array',
],
],
'from' => [
'address' => env('MAIL_FROM_ADDRESS'),
'name' => env('MAIL_FROM_NAME'),
],
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
// CUSTOM CONFIGS
'mail_env' => env('MAIL_ENV'), // 'local' for testing via mailgun, 'production' for all mails
'mail_test' => env('MAIL_TEST'), // test email for 'local' testing
// ADDITIONAL UNNECESSARY CONFIGS
// 'sendmail' => '/usr/sbin/sendmail -bs',
// 'sendmail' => '/usr/sbin/sendmail -t-i',
// 'pretend' => false,
// 'log_channel' => env('MAIL_LOG_CHANNEL'),
// 'pretend' => false,
];
config/services.php (the same as you have)
Controller
try {
$name = array_key_exists('first_name', $email_data) ? $email_data['first_name'] : $email_data['name'];
Mail::send('emails.confirm-registration', [
'role' => $email_data['role'],
'name' => $name,
'email' => $email_data['email'],
'confirm_registration' => route('front.auth.confirm_registration', ['registration_token' => $email_data['registration_token']]),
], function ($message) use ($email_data, $name) {
$to = (config('mail.mail_env') == 'prod' || config('mail.mail_env') == 'production') ? $email_data['email'] : config('mail.mail_test');
$message
->subject(config('app.name') . ": Email Confirmation")
->from(config('mail.from.address'), config('mail.from.name'))
->to($to, $name);
});
return true;
}
catch(\Exception $e) {
// TODO: report all the "$e->message"s like this
return false;
}
resources/views/emails/confirm-registration.php
<a href="{{ route('front.main') }}" target="_blank">
<img label="logo" alt="{{ config('app.name') }}"
src="{{ $message->embed(public_path('images/logo.svg')) }}" width="128" height="96">
</a>
<p>{{ $name }}</p>
<p>{{ $confirm_registration }}</p>
<p>© {{ date("Y") == 2020 ? '2020' : '2020-' . date("Y") }} {{ config('app.name') }}</p>
This is just my example, so there you may need to replace as you want.
Just pay attention to "config/mail.php" file. I think there you need to set some additional props. (Also don't forget to reload your cache after these last config changes: "php artisan config:cache")

Related

Mail configuration settings changes in the .env file not taking effect in Laravel 8

I initially had my mail configuration settings in my .env file pointing to my gmail account and contact form was working fine. I have now changed my settings to point to a webmail and run php artisan config:cache but the mail is still being sent to my gmail. Below is my configuration settings and I've also tested the web mail by sending an email to it via gmail and sending back to gmail from the web mail and it's working fine. Any assistance is highly appreciated.
MAIL_MAILER=smtp
MAIL_HOST=mywebmail
MAIL_PORT=465
MAIL_USERNAME=sales#mydomain
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=ssl
MAIL_FROM_ADDRESS=sales#mydomain
MAIL_FROM_NAME="Laravel"
PS: My config/Mail.php
<?php
return [
'default' => env('MAIL_MAILER', 'smtp'),
'mailers' => [
'smtp' => [
'transport' => 'smtp',
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'timeout' => null,
'auth_mode' => null,
],
'ses' => [
'transport' => 'ses',
],
'mailgun' => [
'transport' => 'mailgun',
],
'postmark' => [
'transport' => 'postmark',
],
'sendmail' => [
'transport' => 'sendmail',
'path' => '/usr/sbin/sendmail -bs',
],
'log' => [
'transport' => 'log',
'channel' => env('MAIL_LOG_CHANNEL'),
],
'array' => [
'transport' => 'array',
],
],
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello#example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
];
The problem was in my controller. I was sending email to my gmail instead of my webmail. I forgot to change that when I changed the mail settings. After changing mygmail to my webmail it works just fine.
public function store(Request $request)
{
$data = $request->validate([
//
]);
Mail::to('mygmail.com')
->send(new ContactMe($data));
return redirect(route('contact-us.index'))->with('flash', "Thank you,
we'll be in touch soon");
}

Unable to resolve NULL driver for [Illuminate\Mail\TransportManager] Laravel 6

I'm trying to send an email using a Laravel Mail, following everything on the documentation, below is my mail.php
'default' => env('MAIL_MAILER', 'smtp'),
'mailers' => [
'smtp' => [
'transport' => 'smtp',
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'timeout' => null,
],
'ses' => [
'transport' => 'ses',
],
'mailgun' => [
'transport' => 'mailgun',
],
'postmark' => [
'transport' => 'postmark',
],
'sendmail' => [
'transport' => 'sendmail',
'path' => '/usr/sbin/sendmail -bs',
],
'log' => [
'transport' => 'log',
'channel' => env('MAIL_LOG_CHANNEL'),
],
'array' => [
'transport' => 'array',
],
],
and tried just using the php mail by setting up on .env mail part to NULL
MAIL_DRIVER=smtp
MAIL_HOST=localhost
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=myemail#domain.com
MAIL_FROM_NAME="${APP_NAME}"
and tried to send
Mail::to('drako#domain.com')
->bcc( app('config')->get('mail')['bcc'])
->send(new Get_a_quote($data));
if(count(Mail::failures()) > 0){
return false;
}else{
return true;
}
but my attempt returns an error
Unable to resolve NULL driver for [Illuminate\Mail\TransportManager].
any help, ideas is greatly appreciated.
You need to replace the MAIL_DRIVER with MAIL_MAILER in your .env file.

How send mail in laravel and RollbarNotifier

I develop a laravel app and I want to automatically send emails with remainder to users.
I created command like in this article. But I get this error:
[Illuminate\Contracts\Container\BindingResolutionException]
Unresolvable dependency resolving [Parameter #0 [ <required> $config ]] in
class RollbarNotifier
This is my code:
.env
MAIL_DRIVER=smtp
MAIL_HOST=example.com
MAIL_PORT=465
MAIL_USERNAME=noreply#example.com
MAIL_PASSWORD=PAssWord
MAIL_ENCRYPTION=SSL
MAIL_FROM_ADDRESS=noreply#example.com
MAIL_FROM_NAME="FROM FROM"
ROLLBAR_TOKEN=<my_token>
config/mail.php
<?php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'example.com'),
'port' => env('MAIL_PORT', 465),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'noreply#example.com'),
'name' => env('MAIL_FROM_NAME', 'FROM FROM'),
],
'encryption' => env('MAIL_ENCRYPTION', 'SSL'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
];
It looks like your dependency injection is not working. It is missing a configuration file.
I am not sure which package you are using. But when I look at the installation manual at https://github.com/jenssegers/laravel-rollbar I see you also have to add some configuration in the services.php:
'rollbar' => [
'access_token' => env('ROLLBAR_TOKEN'),
'level' => env('ROLLBAR_LEVEL'),
],

Too few arguments to function Illuminate\Support\Manager::createDriver(), 0 passed in framework/src/Illuminate/Support/Manager.php

I am using smtp protocol to send mails using mailtrap. It is working perfectly in localhost but it is giving error.
Symfony\Component\Debug\Exception\FatalThrowableError Type error: Too
few arguments to function Illuminate\Support\Manager::createDriver(),
0 passed in
public_html/vendor/laravel/framework/src/Illuminate/Support/Manager.php
on line 88 and exactly 1 expected
.env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=name
MAIL_PASSWORD=pass
MAIL_ENCRYPTION=null
mail.php
<?php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailtrap.org'),
'port' => env('MAIL_PORT', 2525,
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello#example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
];
First: Make Sure that you have mail.php and services.php in config folder set
with the following respectively:
mail.php
<?php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailtrap.org'),
'port' => env('MAIL_PORT', 2525,
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello#example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
];
And services.php (note that this includes the mailgun configuration)
<?php
return [
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
],
'ses' => [
'key' => env('SES_KEY'),
'secret' => env('SES_SECRET'),
'region' => 'us-east-1',
],
'sparkpost' => [
'secret' => env('SPARKPOST_SECRET'),
],
'stripe' => [
'model' => App\User::class,
'key' => env('STRIPE_KEY'),
'secret' => env('STRIPE_SECRET'),
],
];
ADD THIS ON YOUR boostrap/app/php file
$app->configure('mail'); // this is to configure the mail
$app->configure('services'); // and to configure the services
//$con = env('MAIL_DRIVER');
// dd($con);
dd(config('mail')); // this is to print the mail configuration so you can know what mail configuration are being read.
return $app;
run php artisan serve within the terminal
just to see the contents of the mail configuration.
PLEASE NOTE
I Noticed that the simple fix is to just UPDATE/MODIFY THE .env file with the credentials(using gmail smtp server as example)
MAIL_DRIVER_=smtp (instead of MAIL_DRIVER, update it the mail.php)
MAIL_HOST_=smtp.gmail.com (instead of MAIL_HOST)
MAIL_PORT_=587
MAIL_USERNAME=youremail#domain.com
MAIL_PASSWORD=yourpassword
MAIL_ENCRYPTION=tls
MAILGUN_DOMAIN=
MAILGUN_SECRET=
After that, if you were already running your server. Kill it and rerun it again. This should solve your errors,
REASON
For reasons I couldn't explain, some of the mail config parameter didn't reflect until I applied the change above.
Secondly I later changed the .env settings to the original name eliminating the extra "_" as well as updated the mail.php, and had to kill the already running server and rerun it again before it reflected and ran well or worked!.
Hope this helps

Mailgun emails not going out

I'm creating a verification email, however the mail isn't going out and I don't receive any errors.
My call to send the email:
public function register(Request $request)
{
$this->validator($request->all())->validate();
event(new Registered($user = $this->create($request->all())));
Mail::to($user->email)
->queue(new VerifyEmail($user));
return $this->registered($request, $user)
?: redirect($this->redirectPath());
}
here's my email build function:
public function build()
{
return $this->view('emails.account.verify_email')
->with([
'id' => $this->user->id,
'firstname' => $this->user->firstname,
'token' => $this->user->email_verification_token,
]);
}
I installed guzzlehttp/guzzle
and changed my files:
ENV (not sure of the port setting)
MAIL_DRIVER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=postmaster#sandbox...655.mailgun.org
MAIL_PASSWORD=Default mailgun sandbox Password
MAIL_ENCRYPTION=tls
config/services
'mailgun' => [
'domain' => env('sandbox...655.mailgun.org'),
'secret' => env('key-...'),
],
config/mail
<?php
return [
'driver' => env('MAIL_DRIVER', 'mailgun'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello#example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
];
I don't receive any errors, however there are no outgoing mails when I check the mailgun dashboard
Your services config file is wrong:
'mailgun' => [
'domain' => env('sandbox...655.mailgun.org'),
'secret' => env('key-...'),
],
You're trying to look up the values from the .env file here. Should reference the keys, not the values. For instance:
'mailgun' => [
'domain' => env('MAIL_DOMAIN'),
'secret' => env('MAIL_SECRET'),
],
And then add them to your .env file:
MAIL_DOMAIN=sandbox...655.mailgun.org
MAIL_KEY=key-...
I'm assuming the rest is correct but I don't know for certain. :)

Categories