使用laravel时发送邮件时出错

使用laravel时发送邮件时出错

问题描述:

I was building a website which can send mail to my account on the page view.
I have edited my .env file like this :

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=example@example.com
MAIL_PASSWORD=example
MAIL_ENCRYPTION=null

username and password is set as example for this question only, on original file I have given my original name and email.
And my mail.php file looks like this:

'from' => [
    'address' => 'hello@example.com',
    'name' => 'Example',
],

With other function ^^

MyRoute file looks like this:

Route::get('/send', function() {
    Mail::send('email.send', ['name' => 'Its me'], function($message) {
        $message->to('example@example.com', 'Someone')->from('someone@somewhere.com')->subject('Worked');
    });
});

Here also username and name is set as example || Its me on here only, on original file I have given my original name and email.

And my email/send.blade.php file is having only {{$name}}

I used the tutorial from youtube inorder to study this.

Still I get an error like this

MethodNotAllowedHttpException in RouteCollection.php line 218

How can I fix this error? Please help me.

Try! This will work for sure

// Route::any : it means that it will allow all method like get,post,put,patch...
Route::any('/send', function() 
{
  return Mail::send('email.send', ['name' => 'Its me'], function($message) 
  {
     $name = "Name"; 
     $message->from('no-reply@xxxxx.com',$name); 
     $message->to('kroy.webxpert@gmail.com')->subject('Test Mail'); 
  });
});

Try this it will work :

Route::match(['get','post'],'/send', function() {
    Mail::send('email.send', ['name' => 'Its me'], function($message) {
        $message->to('example@example.com', 'Someone')->from('someone@somewhere.com')->subject('Worked');
    });
});

OR

 Route::any('/send', function() {
        Mail::send('email.send', ['name' => 'Its me'], function($message) {
            $message->to('example@example.com', 'Someone')->from('someone@somewhere.com')->subject('Worked');
        });
    });