Trying to Get Laravel 5 Email to Work

Attempting to get Email to work in Laravel 5

You have a couple issues going on. First off:

'driver' => env('smtp'),

The env method looks to your .env file. My guess is you don't have a "smtp" entry in your .env file. I would simply change it to this:

'driver' => 'smtp',

That should take care of the createDriver() error.

If you still have issues with the driver, or later on have issues authenticating to your SMTP server, do a quick check of your config at runtime:

dd(Config::get("mail"));

Since you have env() checking for .env settings and then falling back to default values, it can be helpful to see what the generated config looks like.

Now you still have an issue with how you are calling Mail::send. This is your code:

Mail::send('Email.test', function ($message)

And this is from the Laravel documentation:

Mail::send('emails.welcome', ['key' => 'value'], function($message)

Notice that the second argument is an array. The callback function should be the third argument.

From the docs:

The second is the data to be passed to the view, often as an associative array where the data items are available to the view by $key.

So do something like this:

Mail::send('Email.test', [], function ($message) { 
$message->to('example@gmail.com', 'example_name')->subject('Welcome!');
});

Trying to get Laravel 5 email to work

I know it's working for you now @Vantheman6 but this is what worked for me in case it's the same for someone else.

I added to my .env file the details of the mail service I am using. So make sure the following details

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=MyUsername@gmail.com
MAIL_PASSWORD=MyPassword

in the .env file are accurate.

NOTE: Don't forget to restart your server after editing the .env file so it will pick the new data that you put in there.

Clear config cache with below command:

php artisan config:cache

If you don't restart your server, the .env file will still continue to present the old mail data to the app even though you have made changes that can cause this error.

Problems trying to send email with Laravel 5.5

There might be multiple reasons:

  • configuration is cached (use php artisan config:cache)
  • you used invalid mailtrap data (you should have log in laravel.log file)
  • your server has somehow blocked 2525 port

Also looking at your code it seems you miss passing $input parameter to constructor. You have:

public function __construct($inputs)

but you run:

->send(new DataManifestationMail());

without passing any value

Laravel 5 contact form not working

To test your emails you can utilize mailtrap.io and add the username/password etc into the .env settings.

Here is generally how I would set it out:

Controller:

use Illuminate\Support\Facades\Input;

public function sendEmail(Request $request)
{
$data = array(
'nombre' => $request->get('nombre'),
'email' => $request->get('email'),
'texto' => $request->get('texto')
);

// php artisan make:mail sendMessage - this needs to be ran in terminal / cmd
Mail::to(config('mail.from.address'))->send(new sendMessage($data));
}

SendMail.php:

private $data;

public function __construct($data)
{
$this->data = $data;
}

/**
* Build the message.
*
* @return $this
*/
public function build()
{
$data = $this->data;
return $this->view('web.emails.contact', compact('data'))
->subject('Web Contact')
->from(Input::get('email'));
}

web/emails/contact.blade (basic example):

<p>
Hey there {{ config('mail.from.name') }}
</p>
<p>
You have an email from {{ $data['nombre'] }} - email address {{ $data['email'] }}
</p>
<p>
{!! $data['texto'] !!}
</p>

config('mail.from.name) & config('mail.from.address) can be adjusted within config/mail.php or you can simply remove these and add your own name / email address.

Laravel mail provide sender detail

try this

Mail::to('xyz@example.com')->send(new InquiryToAdmin($inquery,$user));

class InquiryToAdmin extends Mailable
{
use Queueable, SerializesModels;

public $data;
protected $user;

/**
* Create a new message instance.
*
* @return void
*/
public function __construct($data,$user)
{
$this->data = $data;
$this->user = $user;
}

/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->from($this->user->email)->subject('New Inquiry was sent')->view('emails.inquiry.admin');
}
}

Laravel 6 never emails

Route::get('/sometesturl', function() {
var_dump(Config::get('mail'));
echo config('mail.port');
echo config('mail.driver');
echo config('mail.username');exit;
});

shows that there was no username even. Looking in the .env file I found that the added MAIL_ vars were getting overwritten by some defaults below!



Related Topics



Leave a reply



Submit