Unable to use the dynamic mail configs in Laravel - php

I want to override the default mail settings in config/mail.php or .env which Laravel uses by default. I'm setting up the dynamic configs (coming from DB) but email when sent is still going from the "from" address set in the config/mail.php or .env.
Any suggestion for the above words??
Here's the part of the code I'm using:
$mailConfigs = [
'driver' => 'smtp',
'port' => $dbSettings->smtp_port,
'host' => $dbSettings->smtp_host,
'encryption' => $dbSettings->smtp_encryption,
'username' => $dbSettings->smtp_email,
'password' => $dbSettings->smtp_password,
'sendmail' => '/usr/sbin/sendmail -bs',
'from' => [
'address' => $dbSettings->smtp_email,
'name' => 'XXXXX'
],
];
Config::set('mail', $mailConfigs);
// print_r(Config::get('mail'));
// Config here are reflected correctly if printed but when email is sent, it again uses the "from" address which is setup in .env and totally ignores the one setup above.
$res = Mail::send([], $data, function ($message) use($mailConfigs)
{
// email body, etc...
});

Related

Adding mail configuration in service provider not working in laravel 7.0 it gives Unsupported mail transport Error

I am working in Laravel and have added the mail configuration in the "MailConfigServiceProvider" but it's giving error.
I am currently using Laravel 7.0.
Is there anything missing in the configuration or I have taken a wrong approach?
Here is the error
Error
InvalidArgumentException
Unsupported mail transport [1].
Here is my mail service provider code:
public function boot()
{
$emailServices = EmailServices::where(['active' => 1])->latest()->first();
if ($emailServices) {
$config = array(
'driver' => $emailServices->driver,
'host' => $emailServices->host,
'port' => $emailServices->port,
'username' => $emailServices->username,
'password' => $emailServices->password,
'encryption' => null,
'from' => array('address' => 'codingdriver15#gmail.com', 'name' => $emailServices->name),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
);
Config::set('mail', $config);
}
}
I have also added the provider in the config/add.php file
'providers' => [
App\Providers\MailConfigServiceProvider::class,
],
I am known to the .env file approach but here I am trying to make it dynamic.
Any help or suggestion will be helpful and appreciated.
MAIL_DRIVER: SMTP
MAIL_ENCRYPTION: TLS
PORT:2525

Sending email via Mailtrap in laravel 4.2 not sending

I'm trying to test some emails within a laravel 4.2 app and have configured mail.php to use mailtrap
here's my mail.php config:
return array(
'driver' => 'smtp',
'host' => 'smtp.mailtrap.io',
'port' => 2525,
'from' => array('address' => 'website#domain.co.uk', 'name'=>'Domain'),
'encryption' => 'null',
'username' => '12345XX',
'password' => '12345XX',
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
);
I'm trying to use this within a development environment with a vagrant box (homestead)
I'm using a basic test script as follows to send a test email:
$data = array(
'activationCode' => '123456789',
'id' => 27,
'user' => [
'id'=>27,
'first_name'=>'TEST USER',
'last_name'=>'LAST NAME',
'email'=>'test#domain.org.uk'
]
);
$firstResult = Mail::send('emails.auth.accessPending', $data, function ($message) {
$message
->to('email#domain.com')
->subject('website registration');
});
I get a NULL response and nothing appears in the mailtrap inbox.
What am I missing in either my configuration or script to get the app to send test emails?
Thanks
Can you check if you have .env file those
MAIL_HOST=mailtrap.io
_PORT=2525
MAIL_USERNAME=your username
MAIL_PASSWORD=your pass
MAIL_ENCRYPTION=null
Ok - it was working - sort of. The reason there was no output because the mail config file for local development was set to pretend
Revealed by a quick check of the log file
Ta

Laravel 5.4 SMTP error "send AUTH command first"

I'm getting the following error trying to send mail from localhost using smtp:
Expected response code 250 but got code "503", with message "503 5.5.4
Error: send AUTH command first. "
.env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.yandex.com
MAIL_PORT=465
MAIL_USERNAME=robot#domain.com
MAIL_PASSWORD=11111111
MAIL_ENCRYPTION=ssl
MAIL_FROM=robot#domain.com
MAIL_NAME=MY.NAME
config/mail.php
<?php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.yandex.com'),
'port' => env('MAIL_PORT', 465),
'from' => [
'address' => 'robot#domain.com',
'name' => 'MY.NAME',
],
'encryption' => env('MAIL_ENCRYPTION', 'ssl'),
'username' => env('robot#domain.com'),
'password' => env('11111111'),
'sendmail' => '/usr/sbin/sendmail -bs',
];
Tried: changing ports, encryption, clearing cache, restarting server in all possible combinations. :)
As I see it there's one more parameter I need to pass to the mailer library. Some thing like
auth_mode=login_first
Can this be done through laravel settings?
I'm posting my working settings. You've got to check how laravel env helper function is used in your config file. Also when using smtp.yandex.com auth email and form email must match.
Laravel Docs for env()
The env function gets the value of an environment variable or returns a default value:
$env = env('APP_ENV');
// Return a default value if the variable doesn't exist...
$env = env('APP_ENV', 'production');
.env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.yandex.com
MAIL_PORT=465
MAIL_USERNAME=robot#mydomain.com
MAIL_PASSWORD=123123123
MAIL_ENCRYPTION=ssl
MAIL_FROM=robot#mydomain.com
MAIL_NAME=MY.NAME
config/mail.php
<?php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.yandex.com'),
'port' => env('MAIL_PORT', 465),
'from' => [
'address' => env('MAIL_FROM','robot#mydomain.com'),
'name' => env('MAIL_NAME','MY.NAME'),
],
'encryption' => env('MAIL_ENCRYPTION', 'ssl'),
'username' => env('MAIL_USERNAME','robot#mydomain.com'),
'password' => env('MAIL_PASSWORD','123123123'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
];
Controller function
public function testmail()
{
$user = Auth::user();
$pathToLogo = config('app.url').'/images/logo/logo_250.png';
Mail::send('emails.testmail', array('user' => $user, 'pathToLogo' => $pathToLogo), function($message) use ($user)
{
$message->to($user->email);
$message->subject('Test message');
});
return redirect()->route('home')->with('message','Test message sent.');
}

Laravel 5.1 Mail::send how to know why it don't sends mails?

I am trying Laravel do a mail send. When I execute the code, nothing happens, no errors, no logs, no returning mails, anything.
Config Env
MAIL_DRIVER=smtp
MAIL_HOST=mail.domain.es
MAIL_PORT=587
MAIL_USERNAME=noreply#domain.es
MAIL_PASSWORD=xxxxxx
MAIL_FROM=noreply#domain.es
MAIL_NAME=Domain Name
MAIL_ENCRYPTION=null
Mail.php
return [
'driver' => 'smtp',
'host' => env('MAIL_HOST', 'mail.domain.es'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => 'noreply#domain.es', 'name' => 'Domain Name'],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME', 'noreply#domain.es'),
'password' => env('MAIL_PASSWORD', 'xxxxx'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => env('MAIL_PRETEND', true),
];
Code in the controller
$accion = Accion::findOrFail($id);
Mail::send('emails.notificar', ['accion' => $accion], function ($m) use ($accion) {
$m->from(env('MAIL_FROM'), env('MAIL_NAME'));
$m->to("jtd#adagal.es", "Jtd")->subject('Nova acción formativa');
});
Did you see any error? I did everything that is marked in the official docs but still no response, no mail, no error.
pretend' => env('MAIL_PRETEND', true) change this to false
When the mailer is in pretend mode, messages will be written to your application's log files instead of being sent to the recipient.

Unable to send email from laravel?

I'm trying to send email using Laravel. I configured mail.php with details of gmail server and used my personal gmail account credentials. It worked fine.
Then i tried to configure mail.php with the Secure SSl/TLS settings that my mail provider has provided me.
I changed host, port, username and password. I also changed encryption to ssl.
But i'm not able to send email. After some loading time, it just shows a blank page. No Error.
here's my config file(have removed doc-blocks):
<?php
return array(
'driver' => 'smtp',
'host' => 'myServerHostAddress',
'port' => portno,
'from' => array('address' => 'registration#gambiadept.com', 'name' => 'Gambia Dept'),
'encryption' => 'ssl',
'username' => 'usernameFromMyServiceProvider',
'password' => 'myPassword',
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
);

Categories