Dynamic SMTP Settings Laravel/Livewire - php

I have a database table for all the values that is needed for an smtp, I want to call it and change the values on the mail.php but can't I can dd(config('mail')) fine but it still gives me an error for some reason
The error
Expected response code 250 but got code "550", with message "550 5.7.1 Relaying denied "
Inside my ServiceProvider
// get email view data in provider class
View::composer('*', function ($view) {
// Get the slug from parameters
$slug = $this->app->request->route('slug');
if(isset($slug)){
$configuration = ContactConfig::where("slug", $slug)->first();
if (!is_null($configuration)) {
$config = array(
'driver' => $configuration->driver,
'host' => $configuration->host,
'port' => $configuration->port,
'username' => $configuration->user_name,
'password' => $configuration->password,
'encryption' => $configuration->encryption,
'from' => array('address' => $configuration->sender_email, 'name' => $configuration->sender_name),
);
Config::set('mail', $config);
return;
}
};
if(isset(Auth::user()->id)) {
$configuration = ContactConfig::where("user_id", Auth::user()->id)->first();
if(!is_null($configuration)) {
$config = array(
'driver' => $configuration->driver,
'host' => $configuration->host,
'port' => $configuration->port,
'username' => $configuration->user_name,
'password' => $configuration->password,
'encryption' => $configuration->encryption,
'from' => array('address' => $configuration->sender_email, 'name' => $configuration->sender_name),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
);
\Config::set('mail', $config);
}
}
});
I followed 2 questions that give the same answers but for some reason it doesn't not work for me, I removed the configurations on my .env file but it's stated that it should'nt be needed and I wanted to test if the dynamic settings works.

The approach that I went with is directly changing the config in my Livewire Component I just put the Config::set() function inside the sendEmail() function before using the function Mail::to()

Related

Unable to use the dynamic mail configs in Laravel

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...
});

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

Laravel mail configuration if using providers

I want do a helper who send mails, i was start by doing provider and a custom class for my helper.
my provider register function:
public function register()
{
$this->app->bind('mailer.helper', function (Application $app){
return new MailerHelper($app->make('mailer'));
});
}
my helper function:
class MailerHelper implements SelfHandling{
/** #var Mailer $mailer */
protected $mailer;
public function __construct(Mailer $mailer) {
$this->mailer=$mailer;
}
public function sendFromContact(array $date){
$this->mailer->send('email_templates/contact', $date, function (Message $message){
$message->setTo('stroia.laurentiu92#gmail.com');
$message->setFrom('contact#dianabotezan.ro','Contact website Diana Botezan');
$message->setSubject('Forumlar contact');
});
}
}
my configuration from config/mail.php:
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => null, 'name' => null],
'encryption' => env('MAIL_ENCRYPTION', 'ssl'),
'username' => env('my_gmal#gmail.com'),
'password' => env('mypassword'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
]'
and the problem is laravel have a bad configuration, here is my track from what he try to do:
Connection could not be established with host 127.0.0.1 [Connection refused #111]
1. in StreamBuffer.php line 265
2. at Swift_Transport_StreamBuffer->_establishSocketConnection() in StreamBuffer.php line 62
3. at Swift_Transport_StreamBuffer->initialize(array('protocol' => null, 'host' => '127.0.0.1', 'port' => '2525', 'timeout' => '30', 'blocking' => '1', 'tls' => false, 'type' => '1')) in AbstractSmtpTransport.php line 113
here you can observe he have other config then I set it. What i do wrong ?
It looks like you've put your configuration in the wrong place.
Instead of editing the config/mail.php file directly, you should put your mail configuration settings in the .env file at the root of your project.
You can see here in the sample file shipped with laravel.

How to send email in laravel

I have tried this.
<?php
return array(
'driver' => 'smtp',
'host' => 'smtp.sendgrid.net',
'port' => 587,
'from' => array('address' => 'from#example.com', 'name' => 'John Smith'),
'encryption' => 'tls',
'username' => 'sendgrid_username',
'password' => 'sendgrid_password',
);
Mail::send('emails.demo', $data, function($message)
{
$message->to('jane#example.com', 'Jane Doe')->subject('This is a demo!');
});
But i am getting this error:
Failed to authenticate on SMTP server with username "sendgrid_username" using 2 possible authenticators
How to resolve this problem.
Please help me.
Make sure the settings are OK, username and password are correct, the function itself should work as you posted.

Sending emails with CakePHP not works using CakeEmail and also SwiftMailer

I'm trying to send emails from CakePHP but without success. I'm trying with CakeEmail and this code:
$email = new CakeEmail();
$email->from(array('reynierpm#gmail.com' => __('Recruitment Job App')))
->to('reynierpm#gmail.com')
->subject(__('Recruitment Status Update'))
->send(__('Dear, ReynierPM this is a testing email'));
And doesn't work because no emails is send. The file /app/Config/email.php have this configuration:
class EmailConfig {
public $default = array(
'transport' => 'Debug',
'host' => 'smtp.gmail.com',
'port' => 25,
'timeout' => 30,
'username' => 'mlrepemi#gmail.com',
'password' => 'secret_password',
'charset' => 'utf-8',
'headerCharset' => 'utf-8',
);
}
I've try also this http://bakery.cakephp.org/articles/sky_l3ppard/2009/11/07/updated-swiftmailer-4-xx-component-with-attachments-and-plugins but in this case I get this error:
Fatal error: Class 'testemailView' not found in /var/www/html/jobapp/app/Controller/Component/swift_mailer.php on line 245
I'm using CakePHP 2.0.6 and SwiftMailer 4.1.5, any help?
Cheers and thanks in advance
You are leaving out some important parts to enable email. You should have something like this:
App::uses('CakeEmail', 'Network/Email');
$email = new CakeEmail('default');
Then in your email.php config, your default configuration for gmail should looke like this:
public $default = array(
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'timeout' => 30,
'username' => 'my#gmail.com',
'password' => 'secret',
'transport' => 'Smtp'
);

Categories