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.
Related
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()
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
I'm using PHP 5.6 and Laravel 5.2
My email is running in cron queues. But randomly got the exception timeout.
Connection to tcp://mail.example.com:587 Timed Out
Here is my code:
public function sendNotificationByEMail($information){
Mail::send($information['template'], ['param' => $information['param']], function ($message) use ($information) {
$message->from('admin#example.com', $information['mailAlias']);
$message->to($information['mail']);
if(isset($information['cc']) && !empty($information['cc'])){
$message->cc($information['cc']);
}
$message->subject($information['subject']);
});
}
class SendNotificationEmail extends Job implements ShouldQueue
use InteractsWithQueue, SerializesModels;
try {
$modelHelper->sendNotificationByEMail($result);
} catch(Exception $ex) {
$param['push_notification_note'] = $ex->getMessage();
}
And here is my mail config:
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'mail.example.com'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => env('MAIL_FROM'), 'name' => env('MAIL_NAME')],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
Is my code is wrong, or is something wrong with the server?
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.');
}
I am new to Laravel and I am using Laravel 5.0 in one of my application and I am trying to send an email via smtp in Larvel but I am getting this awkward error everytime, It is also not getting my host, Cannot paste whole error in title so here it is
Connection could not be established with host [php_network_getaddresses: getaddrinfo failed: Name or service not
known #0]
My .env file code:
MAIL_DRIVER=smtp
MAIL_HOST=mail.brownrice.com
MAIL_PORT=587
MAIL_USERNAME=cannot show user due to security
MAIL_PASSWORD=cannot show pass due to security
My app/mail.php
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => 'reservations#rezosystems.com', 'name' => 'Rezosystems'],
'encryption' => 'tls',
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
I am using this mail function in my MailController
Mail::send('EmailTemplates/test_email', ['msg' => 'Congratulations!!! Your first email template sent'], function($message)
{
$message->to('test#rezosystems.com', 'John Doe')->subject('Test!');
});